Skip to content

Commit

Permalink
Merge dd2be82 into 35de769
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 7, 2020
2 parents 35de769 + dd2be82 commit a858c33
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
46 changes: 42 additions & 4 deletions lib/util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const _ = require('lodash');
* The `Generator` class instantiate the storage named `config` by default.
*
* @constructor
* @param {String} [name] The name of the new storage (this is a namespace)
* @param {String|Object} [name] The name of the new storage (this is a namespace)
* or options (only when asPath is true)
* @param {String} [name.name] The name of the new storage (this is a namespace)
* @param {boolean} [name.asPath] Use name as path inside the file
* @param {mem-fs-editor} fs A mem-fs editor instance
* @param {String} configPath The filepath used as a storage.
*
Expand All @@ -21,7 +24,10 @@ const _ = require('lodash');
*/
class Storage {
constructor(name, fs, configPath) {
if (name !== undefined && typeof name !== 'string') {
if (typeof name === 'object' && name.asPath) {
this.asPath = name.asPath;
name = name.name;
} else if (name !== undefined && typeof name !== 'string') {
configPath = fs;
fs = name;
name = undefined;
Expand All @@ -43,7 +49,11 @@ class Storage {
*/
get _store() {
const store = this.fs.readJSON(this.path, {});
return (this.name ? store[this.name] : store) || {};
if (!this.name) {
return store || {};
}

return (this.asPath ? _.get(store, this.name) : store[this.name]) || {};
}

/**
Expand All @@ -55,7 +65,11 @@ class Storage {
let fullStore;
if (this.name) {
fullStore = this.fs.readJSON(this.path, {});
fullStore[this.name] = val;
if (this.asPath) {
_.set(fullStore, this.name, val);
} else {
fullStore[this.name] = val;
}
} else {
fullStore = val;
}
Expand All @@ -79,6 +93,15 @@ class Storage {
return this._store[key];
}

/**
* Get a stored value from path
* @param {String} path The path under which the value is stored.
* @return {*} The stored value. Any JSON valid type could be returned
*/
getPath(path) {
return _.get(this._store, path);
}

/**
* Get all the stored values
* @return {Object} key-value object
Expand Down Expand Up @@ -108,6 +131,21 @@ class Storage {
return val;
}

/**
* Assign a path to a value and schedule a save.
* @param {String} path The key under which the value is stored
* @param {*} val Any valid JSON type value (String, Number, Array, Object).
* @return {*} val Whatever was passed in as val.
*/
setPath(path, val) {
assert(!_.isFunction(val), "Storage value can't be a function");

const store = this._store;
_.set(store, path, val);
this._persist(store);
return val;
}

/**
* Delete a key from the store and schedule a save.
* @param {String} key The key under which the value is stored.
Expand Down
28 changes: 28 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,32 @@ describe('Storage', () => {
store.set('test', { bar: 'foo' });
assert.equal(this.store.get('bar'), 'foo');
});

it('#getPath() & #setPath()', function() {
this.store.set('name', { name: 'test' });
assert.ok(this.store.getPath('name'));
assert.equal(this.store.getPath('name.name'), 'test');
assert.equal(this.store.setPath('name.name', 'changed'), 'changed');
assert.equal(this.store.getPath('name.name'), 'changed');
assert.equal(this.store.get('name').name, 'changed');
});

describe('.constructor() with asPath', () => {
beforeEach(function() {
this.pathStore = new Storage(
{ name: 'test.path', asPath: true },
this.fs,
this.storePath
);
});

it('get and set value', function() {
assert.equal(this.pathStore.setPath('name', 'initial'), 'initial');
assert.equal(this.store.get('path').name, 'initial');
this.store.set('path', { name: 'test' });
assert.equal(this.pathStore.get('name'), 'test');
this.pathStore.set('name', 'changed');
assert.equal(this.store.get('path').name, 'changed');
});
});
});

0 comments on commit a858c33

Please sign in to comment.