@@ -10,16 +10,10 @@ import mkdirp from 'mkdirp';
10
10
import clone from 'clone' ;
11
11
12
12
const isJSONFile = f => f . substr ( - 5 ) === ".json" ;
13
-
14
13
const removeFileExtension = f => f . split ( ".json" ) [ 0 ] ;
15
-
16
14
const getIDs = a => a . filter ( isJSONFile ) . map ( removeFileExtension ) ;
17
-
18
15
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 ) ) ) ;
23
17
24
18
const getObjectFromFileSync = function ( id ) {
25
19
try {
@@ -68,7 +62,7 @@ const saveObjectToFile = function(o, file, cb) {
68
62
try {
69
63
json = JSON . stringify ( o , null , indent ) ;
70
64
} catch ( error ) {
71
- if ( cb != null ) {
65
+ if ( typeof cb === "function" ) {
72
66
return cb ( error ) ;
73
67
} else {
74
68
return error ;
@@ -77,7 +71,7 @@ const saveObjectToFile = function(o, file, cb) {
77
71
78
72
const tmpFileName = file + uuid . v4 ( ) + ".tmp" ;
79
73
80
- if ( cb != null ) {
74
+ if ( typeof cb === "function" ) {
81
75
canWriteToFile ( file , ( err ) => {
82
76
if ( err ) return cb ( err ) ;
83
77
@@ -101,13 +95,13 @@ const saveObjectToFile = function(o, file, cb) {
101
95
const id2fileName = ( id , dir ) => path . join ( dir , id + ".json" ) ;
102
96
103
97
const save = function ( id , o , cb ) {
104
- let backup , k ;
98
+ let backup , k , data ;
105
99
if ( typeof id === "object" ) {
106
100
cb = o ;
107
101
o = id ;
108
102
id = null ;
109
103
}
110
- if ( id == null ) {
104
+ if ( typeof id ! == "string" ) {
111
105
id = uuid . v4 ( ) ;
112
106
}
113
107
const file = this . _getFileName ( id ) ;
@@ -120,122 +114,123 @@ const save = function(id, o, cb) {
120
114
}
121
115
}
122
116
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
+ }
124
124
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 ) ;
136
132
} 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 ;
143
141
}
144
- } ;
145
- } ) ( this ) ;
142
+ }
143
+ } ;
144
+
146
145
147
- if ( this . _memory ) return done ( ) ;
146
+ if ( this . _memory ) return done . call ( this ) ;
148
147
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 ) ) ;
151
150
} else {
152
- return done ( saveObjectToFile . call ( this , data , file ) ) ;
151
+ return done . call ( this , saveObjectToFile . call ( this , data , file ) ) ;
153
152
}
154
153
} ;
155
154
156
155
const get = function ( id , cb ) {
157
156
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 ) ;
160
159
}
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 ;
180
168
}
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 ) ;
184
175
} else {
185
- return item ;
176
+ return e ;
186
177
}
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
+ } ;
189
186
190
- if ( this . _memory ) return done ( null , o ) ;
187
+ if ( this . _memory ) return done . call ( this , null , o ) ;
191
188
192
- if ( cb != null ) return getObjectFromFile . call ( this , id , done ) ;
189
+ if ( typeof cb === "function" ) return getObjectFromFile . call ( this , id , done . bind ( this ) ) ;
193
190
194
191
const err = ( o = getObjectFromFileSync . call ( this , id ) ) instanceof Error ;
195
192
196
- return done ( ( err ? o : void 0 ) , ( ! err ? o : void 0 ) ) ;
193
+ return done . call ( this , ( err ? o : void 0 ) , ( ! err ? o : void 0 ) ) ;
197
194
} ;
198
195
199
196
const remove = function ( id , cb ) {
200
197
let e , err , notInCache , o ;
201
198
const file = this . _getFileName ( id ) ;
202
199
const cacheBackup = this . _cache [ id ] ;
203
- if ( cacheBackup == null ) {
200
+ if ( typeof cacheBackup ! == "object" ) {
204
201
notInCache = new Error ( id + " does not exist" ) ;
205
202
}
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
+ } ;
216
211
217
212
if ( this . _single ) {
218
213
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 ) ;
221
216
}
222
217
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 ) ) ;
225
220
}
226
221
227
222
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 ) ) ;
229
224
}
230
225
231
- if ( this . _memory ) return done ( notInCache ) ;
226
+ if ( this . _memory ) return done . call ( this , notInCache ) ;
232
227
233
- if ( cb != null ) return fs . unlink ( file , done ) ;
228
+ if ( typeof cb === "function" ) return fs . unlink ( file , done . bind ( this ) ) ;
234
229
235
230
try {
236
- return done ( fs . unlinkSync ( file ) ) ;
231
+ return done . call ( this , fs . unlinkSync ( file ) ) ;
237
232
} catch ( error ) {
238
- return done ( error ) ;
233
+ return done . call ( this , error ) ;
239
234
}
240
235
} ;
241
236
@@ -287,21 +282,15 @@ class Store {
287
282
}
288
283
}
289
284
290
- save ( id , o , cb ) {
291
- if ( cb == null ) {
292
- cb = ( ) => { } ;
293
- }
285
+ save ( id , o , cb = ( ) => { } ) {
294
286
return save . call ( this , id , o , cb ) ;
295
287
}
296
288
297
289
saveSync ( id , o ) {
298
290
return save . call ( this , id , o ) ;
299
291
}
300
292
301
- get ( id , cb ) {
302
- if ( cb == null ) {
303
- cb = ( ) => { } ;
304
- }
293
+ get ( id , cb = ( ) => { } ) {
305
294
get . call ( this , id , cb ) ;
306
295
}
307
296
@@ -317,52 +306,41 @@ class Store {
317
306
return remove . call ( this , id ) ;
318
307
}
319
308
320
- all ( cb ) {
321
-
322
- if ( cb == null ) cb = ( ) => { } ;
309
+ all ( cb = ( ) => { } ) {
323
310
324
311
if ( this . _memory ) return cb ( null , this . _cache ) ;
325
312
326
313
if ( this . _single ) {
327
314
return getObjectFromFile . call ( this , void 0 , cb ) ;
328
315
}
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
+ }
334
320
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
+ } ) ;
353
332
354
- async . parallel ( loaders , ( err ) => cb ( err , all ) ) ;
333
+ async . parallel ( loaders , ( err ) => cb ( err , all ) ) ;
355
334
356
- } ;
357
- } ) ( this ) ) ;
335
+ } . bind ( this ) ) ;
358
336
}
359
337
360
338
allSync ( ) {
361
339
362
340
if ( this . _memory ) return this . _cache ;
363
341
364
342
if ( this . _single ) {
365
- const db = getObjectFromFileSync . apply ( this ) ;
343
+ const db = getObjectFromFileSync . call ( this ) ;
366
344
if ( typeof db !== "object" ) {
367
345
throw new Error ( "could not load database" ) ;
368
346
}
@@ -372,7 +350,7 @@ class Store {
372
350
let objects = { } ;
373
351
readIDsSync ( this . _dir ) . forEach ( ( f ) => {
374
352
const item = getObjectFromFileSync . call ( this , f ) ;
375
- if ( item != null ) {
353
+ if ( item !== undefined ) {
376
354
objects [ f ] = item ;
377
355
} else {
378
356
console . error ( "could not load '" + f + "'" ) ;
0 commit comments