Skip to content

Commit 6fba494

Browse files
committed
use es6
1 parent 7999a1a commit 6fba494

File tree

3 files changed

+86
-94
lines changed

3 files changed

+86
-94
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"],
3+
}

Store.js renamed to Store.es6.js

+77-93
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,50 @@ Copyright (C) 2012 - 2016 Markus Kohlhase <[email protected]>
44

55
"use strict";
66

7-
var async = require('async');
8-
var fs = require('fs');
9-
var path = require('path');
10-
var uuid = require('node-uuid');
11-
var mkdirp = require('mkdirp');
12-
var clone = require('clone');
13-
14-
var isJSONFile = function(f) {
15-
return f.substr(-5) === ".json";
16-
};
7+
import async from 'async';
8+
import fs from 'fs';
9+
import path from 'path';
10+
import uuid from 'node-uuid';
11+
import mkdirp from 'mkdirp';
12+
import clone from 'clone';
1713

18-
var removeFileExtension = function(f) {
19-
return f.split(".json")[0];
20-
};
14+
const isJSONFile = f => f.substr(-5) === ".json";
2115

22-
var getIDs = function(a) {
23-
return a.filter(isJSONFile).map(removeFileExtension);
24-
};
16+
const removeFileExtension = f => f.split(".json")[0];
2517

26-
var readIDsSync = function(d) {
27-
return getIDs(fs.readdirSync(d));
28-
};
18+
const getIDs = a => a.filter(isJSONFile).map(removeFileExtension);
2919

30-
var readIDs = function(d, cb) {
31-
return fs.readdir(d, function(err, ids) {
32-
return cb(err, getIDs(ids));
33-
});
34-
};
20+
const readIDsSync = d => getIDs(fs.readdirSync(d));
3521

36-
var canWrite = function(stat) {
37-
var group, owner;
38-
owner = (typeof process.getuid === "function" ? process.getuid() : void 0) === stat.uid;
39-
group = (typeof process.getgid === "function" ? process.getgid() : void 0) === stat.gid;
22+
const readIDs = (d, cb) => fs.readdir(d, (err, ids) => {
23+
cb(err, getIDs(ids));
24+
});
25+
26+
const canWrite = stat => {
27+
const owner = (typeof process.getuid === "function" ? process.getuid() : void 0) === stat.uid;
28+
const group = (typeof process.getgid === "function" ? process.getgid() : void 0) === stat.gid;
4029
return owner && (stat.mode & 128) || group && (stat.mode & 16) || (stat.mode & 2);
4130
};
4231

43-
var canWriteToFile = function(file, cb) {
44-
return fs.exists(file, function(e) {
32+
const canWriteToFile = (file, cb) => {
33+
fs.exists(file, (e) => {
4534
if (!e) {
4635
return cb(null);
4736
}
48-
return fs.stat(file, function(err, s) {
37+
fs.stat(file, (err, s) => {
4938
if (err) {
5039
return cb(err);
5140
}
5241
if (canWrite(s)) {
53-
return cb(null);
42+
cb(null);
5443
} else {
55-
return cb(new Error("File is protected"));
44+
cb(new Error("File is protected"));
5645
}
5746
});
5847
});
5948
};
6049

61-
var canWriteToFileSync = function(file) {
50+
const canWriteToFileSync = file => {
6251
if (!fs.existsSync(file)) {
6352
return;
6453
}
@@ -69,55 +58,50 @@ var canWriteToFileSync = function(file) {
6958
}
7059
};
7160

72-
var getObjectFromFileSync = function(id) {
73-
var e;
61+
const getObjectFromFileSync = function(id) {
7462
try {
7563
return JSON.parse(fs.readFileSync(this._getFileName(id), "utf8"));
7664
} catch (error) {
77-
e = error;
78-
return e;
65+
return error;
7966
}
8067
};
8168

82-
var getObjectFromFile = function(id, cb) {
83-
return fs.readFile(this._getFileName(id), "utf8", function(err, o) {
84-
var e;
69+
const getObjectFromFile = function(id, cb) {
70+
fs.readFile(this._getFileName(id), "utf8", (err, o) => {
8571
if (err) {
8672
return cb(err);
8773
}
8874
try {
89-
return cb(null, JSON.parse(o));
75+
cb(null, JSON.parse(o));
9076
} catch (error) {
91-
e = error;
92-
return cb(e);
77+
cb(error);
9378
}
9479
});
9580
};
9681

97-
var saveObjectToFile = function(o, file, cb) {
98-
var e, indent, json, tmpFileName;
99-
indent = this._pretty ? 2 : void 0;
82+
const saveObjectToFile = function(o, file, cb) {
83+
var json;
84+
const indent = this._pretty ? 2 : void 0;
10085
try {
10186
json = JSON.stringify(o, null, indent);
10287
} catch (error) {
103-
e = error;
10488
if (cb != null) {
105-
return cb(e);
89+
return cb(error);
10690
} else {
107-
return e;
91+
return error;
10892
}
10993
}
110-
tmpFileName = file + uuid.v4() + ".tmp";
94+
var tmpFileName = file + uuid.v4() + ".tmp";
11195
if (cb != null) {
112-
return canWriteToFile(file, function(err) {
96+
canWriteToFile(file, (err) => {
11397
if (err) {
11498
return cb(err);
11599
}
116-
return fs.writeFile(tmpFileName, json, 'utf8', function(err) {
100+
fs.writeFile(tmpFileName, json, 'utf8', (err) => {
117101
if (err) {
118102
return cb(err);
119103
}
120-
return fs.rename(tmpFileName, file, cb);
104+
fs.rename(tmpFileName, file, cb);
121105
});
122106
});
123107
} else {
@@ -126,18 +110,15 @@ var saveObjectToFile = function(o, file, cb) {
126110
fs.writeFileSync(tmpFileName, json, 'utf8');
127111
return fs.renameSync(tmpFileName, file);
128112
} catch (error) {
129-
e = error;
130-
return e;
113+
return error;
131114
}
132115
}
133116
};
134117

135-
var id2fileName = function(id, dir) {
136-
return path.join(dir, id + ".json");
137-
};
118+
const id2fileName = (id, dir) => path.join(dir, id + ".json");
138119

139-
var save = function(id, o, cb) {
140-
var backup, data, done, file, k;
120+
const save = function(id, o, cb) {
121+
var backup, k;
141122
if (typeof id === "object") {
142123
cb = o;
143124
o = id;
@@ -146,7 +127,7 @@ var save = function(id, o, cb) {
146127
if (id == null) {
147128
id = uuid.v4();
148129
}
149-
file = this._getFileName(id);
130+
const file = this._getFileName(id);
150131
o = clone(o);
151132
if (this._saveId) {
152133
if ((typeof (k = this._saveId)) === 'string' && k.length > 0) {
@@ -155,46 +136,49 @@ var save = function(id, o, cb) {
155136
o.id = id;
156137
}
157138
}
158-
data = this._single ? (backup = this._cache[id], this._cache[id] = o, this._cache) : o;
159-
done = (function(_this) {
160-
return function(err) {
139+
140+
const data = this._single ? (backup = this._cache[id], this._cache[id] = o, this._cache) : o;
141+
142+
const done = (function(_this) {
143+
return (err) => {
161144
if (err) {
162145
if (_this._single) {
163146
_this._cache[id] = backup;
164147
}
165148
if (cb != null) {
166-
return cb(err);
149+
cb(err);
167150
} else {
168151
return err;
169152
}
170153
} else {
171154
_this._cache[id] = o;
172155
if (cb != null) {
173-
return cb(null, id);
156+
cb(null, id);
174157
} else {
175158
return id;
176159
}
177160
}
178161
};
179162
})(this);
163+
180164
if (this._memory) {
181165
return done();
182166
} else {
183167
if (cb != null) {
184-
return saveObjectToFile.call(this, data, file, done);
168+
saveObjectToFile.call(this, data, file, done);
185169
} else {
186170
return done(saveObjectToFile.call(this, data, file));
187171
}
188172
}
189173
};
190174

191-
var get = function(id, cb) {
192-
var done, err, o;
175+
const get = function(id, cb) {
176+
var err, o;
193177
o = clone(this._cache[id]);
194178
if (o != null) {
195179
return (cb != null ? cb(null, o) : o);
196180
}
197-
done = (function(_this) {
181+
const done = (function(_this) {
198182
return function(err, o) {
199183
var e, item;
200184
if (err) {
@@ -232,14 +216,14 @@ var get = function(id, cb) {
232216
return done((err ? o : void 0), (!err ? o : void 0));
233217
};
234218

235-
var remove = function(id, cb) {
236-
var cacheBackup, done, e, err, file, notInCache, o;
237-
file = this._getFileName(id);
219+
const remove = function(id, cb) {
220+
var cacheBackup, e, err, notInCache, o;
221+
const file = this._getFileName(id);
238222
cacheBackup = this._cache[id];
239223
if (cacheBackup == null) {
240224
notInCache = new Error(id + " does not exist");
241225
}
242-
done = (function(_this) {
226+
const done = (function(_this) {
243227
return function(err) {
244228
if (err) {
245229
_this._cache[id] = cacheBackup;
@@ -275,9 +259,9 @@ var remove = function(id, cb) {
275259
}
276260
};
277261

278-
var Store = (function() {
279-
function Store(name, opt) {
280-
var fn;
262+
class Store {
263+
264+
constructor(name, opt) {
281265
this.name = name != null ? name : 'store';
282266
if (opt == null) {
283267
opt = {};
@@ -299,7 +283,7 @@ var Store = (function() {
299283
mkdirp.sync(this._dir);
300284
}
301285
if (this._single) {
302-
fn = this._getFileName();
286+
const fn = this._getFileName();
303287
if (!this._memory) {
304288
if (!fs.existsSync(fn)) {
305289
if (fs.writeFileSync(fn, "{}", 'utf8')) {
@@ -311,45 +295,47 @@ var Store = (function() {
311295
}
312296
}
313297

314-
Store.prototype._getFileName = function(id) {
298+
_getFileName(id) {
315299
if (this._single) {
316300
return path.join(this._dir, (path.basename(this.name)) + ".json");
317301
} else {
318302
return id2fileName(id, this._dir);
319303
}
320-
};
304+
}
321305

322-
Store.prototype.save = function(id, o, cb) {
306+
307+
308+
save(id, o, cb) {
323309
if (cb == null) {
324310
cb = function() {};
325311
}
326312
return save.call(this, id, o, cb);
327313
};
328314

329-
Store.prototype.saveSync = function(id, o) {
315+
saveSync(id, o) {
330316
return save.call(this, id, o);
331317
};
332318

333-
Store.prototype.get = function(id, cb) {
319+
get(id, cb) {
334320
if (cb == null) {
335321
cb = function() {};
336322
}
337323
return get.call(this, id, cb);
338324
};
339325

340-
Store.prototype.getSync = function(id) {
326+
getSync(id) {
341327
return get.call(this, id);
342328
};
343329

344-
Store.prototype["delete"] = function(id, cb) {
330+
delete(id, cb) {
345331
return remove.call(this, id, cb);
346332
};
347333

348-
Store.prototype.deleteSync = function(id) {
334+
deleteSync(id) {
349335
return remove.call(this, id);
350336
};
351337

352-
Store.prototype.all = function(cb) {
338+
all(cb) {
353339
if (cb == null) {
354340
cb = function() {};
355341
}
@@ -392,7 +378,7 @@ var Store = (function() {
392378
}
393379
};
394380

395-
Store.prototype.allSync = function() {
381+
allSync() {
396382
var db, f, i, item, len, objects, ref;
397383
if (this._memory) {
398384
return this._cache;
@@ -417,10 +403,8 @@ var Store = (function() {
417403
}
418404
return objects;
419405
}
420-
};
421-
422-
return Store;
406+
}
423407

424-
})();
408+
}
425409

426410
module.exports = Store;

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"clone": "~2.0.0"
1818
},
1919
"devDependencies": {
20+
"babel": "^6.5.2",
21+
"babel-cli": "^6.18.0",
22+
"babel-preset-es2015": "^6.18.0",
2023
"chai": "~3.5.0",
2124
"coveralls": "~2.11.14",
2225
"istanbul": "~0.4.5",
@@ -35,7 +38,9 @@
3538
],
3639
"scripts": {
3740
"lint": "echo \"WARN: no linting process specified\"",
38-
"test": "npm run lint && ./node_modules/.bin/mocha --reporter spec *.spec.js",
41+
"build": "babel Store.es6.js -o Store.js",
42+
"prepublish": "npm run build",
43+
"test": "npm run lint && npm run build && ./node_modules/.bin/mocha --reporter spec *.spec.js",
3944
"coveralls": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
4045
}
4146
}

0 commit comments

Comments
 (0)