Skip to content

Commit e04bdd7

Browse files
committed
refactor some codes
1 parent ac06e35 commit e04bdd7

File tree

1 file changed

+102
-124
lines changed

1 file changed

+102
-124
lines changed

Store.es6.js

+102-124
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ import mkdirp from 'mkdirp';
1010
import clone from 'clone';
1111

1212
const isJSONFile = f => f.substr(-5) === ".json";
13-
1413
const removeFileExtension = f => f.split(".json")[0];
15-
1614
const getIDs = a => a.filter(isJSONFile).map(removeFileExtension);
17-
1815
const readIDsSync = d => getIDs(fs.readdirSync(d));
19-
20-
const readIDs = (d, cb) => fs.readdir(d, (err, ids) =>
21-
cb(err, getIDs(ids))
22-
);
16+
const readIDs = (d, cb) => fs.readdir(d, (err, ids) => cb(err, getIDs(ids)));
2317

2418
const getObjectFromFileSync = function(id) {
2519
try {
@@ -68,7 +62,7 @@ const saveObjectToFile = function(o, file, cb) {
6862
try {
6963
json = JSON.stringify(o, null, indent);
7064
} catch (error) {
71-
if (cb != null) {
65+
if (typeof cb === "function") {
7266
return cb(error);
7367
} else {
7468
return error;
@@ -77,7 +71,7 @@ const saveObjectToFile = function(o, file, cb) {
7771

7872
const tmpFileName = file + uuid.v4() + ".tmp";
7973

80-
if (cb != null) {
74+
if (typeof cb === "function") {
8175
canWriteToFile(file, (err) => {
8276
if (err) return cb(err);
8377

@@ -101,13 +95,13 @@ const saveObjectToFile = function(o, file, cb) {
10195
const id2fileName = (id, dir) => path.join(dir, id + ".json");
10296

10397
const save = function(id, o, cb) {
104-
let backup, k;
98+
let backup, k, data;
10599
if (typeof id === "object") {
106100
cb = o;
107101
o = id;
108102
id = null;
109103
}
110-
if (id == null) {
104+
if (typeof id !== "string") {
111105
id = uuid.v4();
112106
}
113107
const file = this._getFileName(id);
@@ -120,122 +114,123 @@ const save = function(id, o, cb) {
120114
}
121115
}
122116

123-
const data = this._single ? (backup = this._cache[id], this._cache[id] = o, this._cache) : o;
117+
if (this._single) {
118+
backup = this._cache[id];
119+
this._cache[id] = o;
120+
data = this._cache;
121+
} else {
122+
data = o;
123+
}
124124

125-
const done = (function(_this) {
126-
return (err) => {
127-
if (err) {
128-
if (_this._single) {
129-
_this._cache[id] = backup;
130-
}
131-
if (cb != null) {
132-
cb(err);
133-
} else {
134-
return err;
135-
}
125+
const done = function(err) {
126+
if (err) {
127+
if (this._single) {
128+
this._cache[id] = backup;
129+
}
130+
if (typeof cb === "function") {
131+
cb(err);
136132
} else {
137-
_this._cache[id] = o;
138-
if (cb != null) {
139-
cb(null, id);
140-
} else {
141-
return id;
142-
}
133+
return err;
134+
}
135+
} else {
136+
this._cache[id] = o;
137+
if (typeof cb === "function") {
138+
cb(null, id);
139+
} else {
140+
return id;
143141
}
144-
};
145-
})(this);
142+
}
143+
};
144+
146145

147-
if (this._memory) return done();
146+
if (this._memory) return done.call(this);
148147

149-
if (cb != null) {
150-
saveObjectToFile.call(this, data, file, done);
148+
if (typeof cb === "function") {
149+
saveObjectToFile.call(this, data, file, done.bind(this));
151150
} else {
152-
return done(saveObjectToFile.call(this, data, file));
151+
return done.call(this, saveObjectToFile.call(this, data, file));
153152
}
154153
};
155154

156155
const get = function(id, cb) {
157156
let o = clone(this._cache[id]);
158-
if (o != null) {
159-
return (cb != null ? cb(null, o) : o);
157+
if (typeof o === "object") {
158+
return (typeof cb === "function" ? cb(null, o) : o);
160159
}
161-
const done = ((_this) => {
162-
return (err, o) => {
163-
let e, item;
164-
if (err) {
165-
const e = new Error("could not load data");
166-
if (cb != null) {
167-
return cb(e);
168-
} else {
169-
return e;
170-
}
171-
}
172-
item = _this._single ? o[id] : o;
173-
if (item == null) {
174-
e = new Error("could not load data");
175-
if (cb != null) {
176-
return cb(e);
177-
} else {
178-
return e;
179-
}
160+
const done = function (err, o) {
161+
let e, item;
162+
if (err) {
163+
const e = new Error("could not load data");
164+
if (typeof cb === "function") {
165+
return cb(e);
166+
} else {
167+
return e;
180168
}
181-
_this._cache[id] = item;
182-
if (cb != null) {
183-
return cb(null, item);
169+
}
170+
item = this._single ? o[id] : o;
171+
if (typeof item !== "object") {
172+
e = new Error("could not load data");
173+
if (typeof cb === "function") {
174+
return cb(e);
184175
} else {
185-
return item;
176+
return e;
186177
}
187-
};
188-
})(this);
178+
}
179+
this._cache[id] = item;
180+
if (typeof cb === "function") {
181+
return cb(null, item);
182+
} else {
183+
return item;
184+
}
185+
};
189186

190-
if (this._memory) return done(null, o);
187+
if (this._memory) return done.call(this, null, o);
191188

192-
if (cb != null) return getObjectFromFile.call(this, id, done);
189+
if (typeof cb === "function") return getObjectFromFile.call(this, id, done.bind(this));
193190

194191
const err = (o = getObjectFromFileSync.call(this, id)) instanceof Error;
195192

196-
return done((err ? o : void 0), (!err ? o : void 0));
193+
return done.call(this, (err ? o : void 0), (!err ? o : void 0));
197194
};
198195

199196
const remove = function(id, cb) {
200197
let e, err, notInCache, o;
201198
const file = this._getFileName(id);
202199
const cacheBackup = this._cache[id];
203-
if (cacheBackup == null) {
200+
if (typeof cacheBackup !== "object") {
204201
notInCache = new Error(id + " does not exist");
205202
}
206-
const done = ((_this) => {
207-
return (err) => {
208-
if (err) {
209-
_this._cache[id] = cacheBackup;
210-
return (cb != null ? cb(err) : err);
211-
}
212-
delete _this._cache[id];
213-
return typeof cb === "function" ? cb() : void 0;
214-
};
215-
})(this);
203+
const done = function (err) {
204+
if (err) {
205+
this._cache[id] = cacheBackup;
206+
return (typeof cb === "function" ? cb(err) : err);
207+
}
208+
delete this._cache[id];
209+
return typeof cb === "function" ? cb() : void 0;
210+
};
216211

217212
if (this._single) {
218213
delete this._cache[id];
219-
if (this._memory || (notInCache != null)) {
220-
return done(notInCache);
214+
if (this._memory || (notInCache !== undefined)) {
215+
return done.call(this, notInCache);
221216
}
222217

223-
if (cb != null) {
224-
return saveObjectToFile.call(this, this._cache, file, done);
218+
if (typeof cb === "function") {
219+
return saveObjectToFile.call(this, this._cache, file, done.bind(this));
225220
}
226221

227222
err = (o = saveObjectToFile.call(this, this._cache, file)) instanceof Error;
228-
return done((err ? o : void 0), (!err ? o : void 0));
223+
return done.call(this, (err ? o : void 0), (!err ? o : void 0));
229224
}
230225

231-
if (this._memory) return done(notInCache);
226+
if (this._memory) return done.call(this, notInCache);
232227

233-
if (cb != null) return fs.unlink(file, done);
228+
if (typeof cb === "function") return fs.unlink(file, done.bind(this));
234229

235230
try {
236-
return done(fs.unlinkSync(file));
231+
return done.call(this, fs.unlinkSync(file));
237232
} catch (error) {
238-
return done(error);
233+
return done.call(this, error);
239234
}
240235
};
241236

@@ -287,21 +282,15 @@ class Store {
287282
}
288283
}
289284

290-
save(id, o, cb) {
291-
if (cb == null) {
292-
cb = () => {};
293-
}
285+
save(id, o, cb = () => {}) {
294286
return save.call(this, id, o, cb);
295287
}
296288

297289
saveSync(id, o) {
298290
return save.call(this, id, o);
299291
}
300292

301-
get(id, cb) {
302-
if (cb == null) {
303-
cb = () => {};
304-
}
293+
get(id, cb = () => {}) {
305294
get.call(this, id, cb);
306295
}
307296

@@ -317,52 +306,41 @@ class Store {
317306
return remove.call(this, id);
318307
}
319308

320-
all(cb) {
321-
322-
if (cb == null) cb = () => {};
309+
all(cb = () => {}) {
323310

324311
if (this._memory) return cb(null, this._cache);
325312

326313
if (this._single) {
327314
return getObjectFromFile.call(this, void 0, cb);
328315
}
329-
readIDs(this._dir, ((that) => {
330-
return (err, ids) => {
331-
if (typeof err !== "undefined" && err !== null) {
332-
return cb(err);
333-
}
316+
readIDs(this._dir, function (err, ids) {
317+
if (typeof err !== "undefined" && err !== null) {
318+
return cb(err);
319+
}
334320

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-
})();
321+
let all = {};
322+
const loaders = ids.map((id) => {
323+
return function (cb) {
324+
return this.get(id, (err, o) => {
325+
if (!err) {
326+
all[id] = o;
327+
}
328+
return cb(err);
329+
});
330+
}.bind(this);
331+
});
353332

354-
async.parallel(loaders, (err) => cb(err, all));
333+
async.parallel(loaders, (err) => cb(err, all));
355334

356-
};
357-
})(this));
335+
}.bind(this));
358336
}
359337

360338
allSync() {
361339

362340
if (this._memory) return this._cache;
363341

364342
if (this._single) {
365-
const db = getObjectFromFileSync.apply(this);
343+
const db = getObjectFromFileSync.call(this);
366344
if (typeof db !== "object") {
367345
throw new Error("could not load database");
368346
}
@@ -372,7 +350,7 @@ class Store {
372350
let objects = {};
373351
readIDsSync(this._dir).forEach((f) => {
374352
const item = getObjectFromFileSync.call(this, f);
375-
if (item != null) {
353+
if (item !== undefined) {
376354
objects[f] = item;
377355
} else {
378356
console.error("could not load '" + f + "'");

0 commit comments

Comments
 (0)