Skip to content

Commit ac06e35

Browse files
committed
small refactoring with es6 syntax
1 parent 797c017 commit ac06e35

File tree

1 file changed

+115
-129
lines changed

1 file changed

+115
-129
lines changed

Store.es6.js

+115-129
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Copyright (C) 2012 - 2016 Markus Kohlhase <[email protected]>
33
*/
44

5-
"use strict";
6-
75
import async from 'async';
86
import fs from 'fs';
97
import path from 'path';
@@ -19,9 +17,9 @@ const getIDs = a => a.filter(isJSONFile).map(removeFileExtension);
1917

2018
const readIDsSync = d => getIDs(fs.readdirSync(d));
2119

22-
const readIDs = (d, cb) => fs.readdir(d, (err, ids) => {
23-
cb(err, getIDs(ids));
24-
});
20+
const readIDs = (d, cb) => fs.readdir(d, (err, ids) =>
21+
cb(err, getIDs(ids))
22+
);
2523

2624
const getObjectFromFileSync = function(id) {
2725
try {
@@ -49,10 +47,7 @@ const FILE_IS_WRITABLE = fs.constants ? fs.constants.W_OK : fs.W_OK;
4947

5048
const canWriteToFile = (file, cb) => {
5149
fs.access(file, FILE_EXISTS, (err) => {
52-
if (err) {
53-
return cb(null);
54-
}
55-
50+
if (err) return cb(null);
5651
fs.access(file, FILE_IS_WRITABLE, cb);
5752
});
5853
};
@@ -68,8 +63,8 @@ const canWriteToFileSync = (file) => {
6863
};
6964

7065
const saveObjectToFile = function(o, file, cb) {
71-
var json;
7266
const indent = this._pretty ? 2 : void 0;
67+
let json;
7368
try {
7469
json = JSON.stringify(o, null, indent);
7570
} catch (error) {
@@ -81,16 +76,13 @@ const saveObjectToFile = function(o, file, cb) {
8176
}
8277

8378
const tmpFileName = file + uuid.v4() + ".tmp";
79+
8480
if (cb != null) {
8581
canWriteToFile(file, (err) => {
86-
if (err) {
87-
return cb(err);
88-
}
82+
if (err) return cb(err);
8983

9084
fs.writeFile(tmpFileName, json, 'utf8', (err) => {
91-
if (err) {
92-
return cb(err);
93-
}
85+
if (err) return cb(err);
9486

9587
fs.rename(tmpFileName, file, cb);
9688
});
@@ -109,7 +101,7 @@ const saveObjectToFile = function(o, file, cb) {
109101
const id2fileName = (id, dir) => path.join(dir, id + ".json");
110102

111103
const save = function(id, o, cb) {
112-
var backup, k;
104+
let backup, k;
113105
if (typeof id === "object") {
114106
cb = o;
115107
o = id;
@@ -152,28 +144,25 @@ const save = function(id, o, cb) {
152144
};
153145
})(this);
154146

155-
if (this._memory) {
156-
return done();
147+
if (this._memory) return done();
148+
149+
if (cb != null) {
150+
saveObjectToFile.call(this, data, file, done);
157151
} else {
158-
if (cb != null) {
159-
saveObjectToFile.call(this, data, file, done);
160-
} else {
161-
return done(saveObjectToFile.call(this, data, file));
162-
}
152+
return done(saveObjectToFile.call(this, data, file));
163153
}
164154
};
165155

166156
const get = function(id, cb) {
167-
var err, o;
168-
o = clone(this._cache[id]);
157+
let o = clone(this._cache[id]);
169158
if (o != null) {
170159
return (cb != null ? cb(null, o) : o);
171160
}
172-
const done = (function(_this) {
173-
return function(err, o) {
174-
var e, item;
161+
const done = ((_this) => {
162+
return (err, o) => {
163+
let e, item;
175164
if (err) {
176-
e = new Error("could not load data");
165+
const e = new Error("could not load data");
177166
if (cb != null) {
178167
return cb(e);
179168
} else {
@@ -197,25 +186,25 @@ const get = function(id, cb) {
197186
}
198187
};
199188
})(this);
200-
if (this._memory) {
201-
return done(null, o);
202-
}
203-
if (cb != null) {
204-
return getObjectFromFile.call(this, id, done);
205-
}
206-
err = (o = getObjectFromFileSync.call(this, id)) instanceof Error;
189+
190+
if (this._memory) return done(null, o);
191+
192+
if (cb != null) return getObjectFromFile.call(this, id, done);
193+
194+
const err = (o = getObjectFromFileSync.call(this, id)) instanceof Error;
195+
207196
return done((err ? o : void 0), (!err ? o : void 0));
208197
};
209198

210199
const remove = function(id, cb) {
211-
var cacheBackup, e, err, notInCache, o;
200+
let e, err, notInCache, o;
212201
const file = this._getFileName(id);
213-
cacheBackup = this._cache[id];
202+
const cacheBackup = this._cache[id];
214203
if (cacheBackup == null) {
215204
notInCache = new Error(id + " does not exist");
216205
}
217-
const done = (function(_this) {
218-
return function(err) {
206+
const done = ((_this) => {
207+
return (err) => {
219208
if (err) {
220209
_this._cache[id] = cacheBackup;
221210
return (cb != null ? cb(err) : err);
@@ -224,58 +213,62 @@ const remove = function(id, cb) {
224213
return typeof cb === "function" ? cb() : void 0;
225214
};
226215
})(this);
216+
227217
if (this._single) {
228218
delete this._cache[id];
229219
if (this._memory || (notInCache != null)) {
230220
return done(notInCache);
231221
}
222+
232223
if (cb != null) {
233224
return saveObjectToFile.call(this, this._cache, file, done);
234225
}
226+
235227
err = (o = saveObjectToFile.call(this, this._cache, file)) instanceof Error;
236228
return done((err ? o : void 0), (!err ? o : void 0));
237-
} else {
238-
if (this._memory) {
239-
return done(notInCache);
240-
}
241-
if (cb != null) {
242-
return fs.unlink(file, done);
243-
}
244-
try {
245-
return done(fs.unlinkSync(file));
246-
} catch (error) {
247-
e = error;
248-
return done(e);
249-
}
229+
}
230+
231+
if (this._memory) return done(notInCache);
232+
233+
if (cb != null) return fs.unlink(file, done);
234+
235+
try {
236+
return done(fs.unlinkSync(file));
237+
} catch (error) {
238+
return done(error);
250239
}
251240
};
252241

253242
class Store {
254243

255-
constructor(name, opt) {
256-
this.name = name != null ? name : 'store';
257-
if (opt == null) {
258-
opt = {};
259-
}
244+
constructor(name = 'store', opt = {}) {
245+
246+
this.name = name;
260247
this._single = opt.single === true || opt.type === 'single';
261248
this._pretty = opt.pretty === true;
262249
this._memory = opt.memory === true || opt.type === 'memory';
263250
this._saveId = opt.saveId;
251+
264252
if (isJSONFile(this.name)) {
265253
this.name = this.name.split(".json")[0];
266254
this._single = true;
267255
}
256+
268257
this._dir = path.resolve(this.name);
258+
269259
if (this._single) {
270260
this._dir = path.dirname(this._dir);
271261
}
262+
272263
this._cache = {};
264+
273265
if (!this._memory) {
274266
mkdirp.sync(this._dir);
275267
}
268+
276269
if (this._single) {
277-
const fn = this._getFileName();
278270
if (!this._memory) {
271+
const fn = this._getFileName();
279272
if (!fs.existsSync(fn)) {
280273
if (fs.writeFileSync(fn, "{}", 'utf8')) {
281274
throw new Error("could not create database");
@@ -294,106 +287,99 @@ class Store {
294287
}
295288
}
296289

297-
298-
299290
save(id, o, cb) {
300291
if (cb == null) {
301-
cb = function() {};
292+
cb = () => {};
302293
}
303294
return save.call(this, id, o, cb);
304-
};
295+
}
305296

306297
saveSync(id, o) {
307298
return save.call(this, id, o);
308-
};
299+
}
309300

310301
get(id, cb) {
311302
if (cb == null) {
312-
cb = function() {};
303+
cb = () => {};
313304
}
314-
return get.call(this, id, cb);
315-
};
305+
get.call(this, id, cb);
306+
}
316307

317308
getSync(id) {
318309
return get.call(this, id);
319-
};
310+
}
320311

321312
delete(id, cb) {
322-
return remove.call(this, id, cb);
323-
};
313+
remove.call(this, id, cb);
314+
}
324315

325316
deleteSync(id) {
326317
return remove.call(this, id);
327-
};
318+
}
328319

329320
all(cb) {
330-
if (cb == null) {
331-
cb = function() {};
332-
}
333-
if (this._memory) {
334-
return cb(null, this._cache);
335-
} else if (this._single) {
321+
322+
if (cb == null) cb = () => {};
323+
324+
if (this._memory) return cb(null, this._cache);
325+
326+
if (this._single) {
336327
return getObjectFromFile.call(this, void 0, cb);
337-
} else {
338-
return readIDs(this._dir, (function(_this) {
339-
return function(err, ids) {
340-
var all, id, loaders, that;
341-
if (typeof er !== "undefined" && er !== null) {
342-
return cb(err);
343-
}
344-
that = _this;
345-
all = {};
346-
loaders = (function() {
347-
var i, len, results;
348-
results = [];
349-
for (i = 0, len = ids.length; i < len; i++) {
350-
id = ids[i];
351-
results.push((function(id) {
352-
return function(cb) {
353-
return that.get(id, function(err, o) {
354-
if (!err) {
355-
all[id] = o;
356-
}
357-
return cb(err);
358-
});
359-
};
360-
})(id));
361-
}
362-
return results;
363-
})();
364-
return async.parallel(loaders, function(err) {
365-
return cb(err, all);
366-
});
367-
};
368-
})(this));
369328
}
370-
};
329+
readIDs(this._dir, ((that) => {
330+
return (err, ids) => {
331+
if (typeof err !== "undefined" && err !== null) {
332+
return cb(err);
333+
}
334+
335+
let all = {};
336+
337+
const loaders = (() => {
338+
let i, len;
339+
let results = [];
340+
for (i = 0, len = ids.length; i < len; i++) {
341+
const id = ids[i];
342+
results.push(((id) =>
343+
(cb) => that.get(id, (err, o) => {
344+
if (!err) {
345+
all[id] = o;
346+
}
347+
return cb(err);
348+
})
349+
)(id));
350+
}
351+
return results;
352+
})();
353+
354+
async.parallel(loaders, (err) => cb(err, all));
355+
356+
};
357+
})(this));
358+
}
371359

372360
allSync() {
373-
var db, f, i, item, len, objects, ref;
374-
if (this._memory) {
375-
return this._cache;
376-
}
361+
362+
if (this._memory) return this._cache;
363+
377364
if (this._single) {
378-
db = getObjectFromFileSync.apply(this);
365+
const db = getObjectFromFileSync.apply(this);
379366
if (typeof db !== "object") {
380367
throw new Error("could not load database");
381368
}
382369
return db;
383-
} else {
384-
objects = {};
385-
ref = readIDsSync(this._dir);
386-
for (i = 0, len = ref.length; i < len; i++) {
387-
f = ref[i];
388-
item = getObjectFromFileSync.call(this, f);
389-
if (item != null) {
390-
objects[f] = item;
391-
} else {
392-
console.error("could not load '" + f + "'");
393-
}
394-
}
395-
return objects;
396370
}
371+
372+
let objects = {};
373+
readIDsSync(this._dir).forEach((f) => {
374+
const item = getObjectFromFileSync.call(this, f);
375+
if (item != null) {
376+
objects[f] = item;
377+
} else {
378+
console.error("could not load '" + f + "'");
379+
}
380+
});
381+
382+
return objects;
397383
}
398384

399385
}

0 commit comments

Comments
 (0)