Skip to content

Commit

Permalink
Add createStorage on Storage (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Apr 30, 2020
1 parent 9cd93ee commit ed186be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/util/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ class Storage {
this.set(val);
return val;
}

/**
* Create a child storage.
* @param {String} path - relative path of the key to create a new storage.
* @return {Storage} Returns a new Storage.
*/
createStorage(path) {
const childName = this.name ? `${this.name}.${path}` : path;
return new Storage(childName, this.fs, this.path, true);
}
}

module.exports = Storage;
15 changes: 15 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ describe('Storage', () => {
assert.equal(this.store.get('name').name, 'changed');
});

describe('#getStorage()', () => {
beforeEach(function() {
this.pathStore = this.store.createStorage('path');
});

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');
});
});

describe('.constructor() with lodashPath', () => {
beforeEach(function() {
this.pathStore = new Storage('test.path', this.fs, this.storePath, true);
Expand Down

0 comments on commit ed186be

Please sign in to comment.