electrum-store
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -51,2 +51,9 @@ 'use strict'; | ||
function isEmpty(obj) { | ||
for (var x in obj) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/******************************************************************************/ | ||
@@ -78,2 +85,7 @@ | ||
_createClass(State, [{ | ||
key: 'contains', | ||
value: function contains(id) { | ||
return this.get(id) !== undefined; | ||
} | ||
}, { | ||
key: 'get', | ||
@@ -101,5 +113,39 @@ value: function get(id) { | ||
value: function select(id) { | ||
var _this = this; | ||
if (id === undefined && arguments.length === 0) { | ||
return this; | ||
} | ||
return this.selectOrFind(id, function (i) { | ||
return _this._store.select(i); | ||
}); | ||
} | ||
}, { | ||
key: 'find', | ||
value: function find(id) { | ||
var _this2 = this; | ||
if (id === undefined && arguments.length === 0) { | ||
return this; | ||
} | ||
return this.selectOrFind(id, function (i) { | ||
return _this2._store.find(i); | ||
}); | ||
} | ||
}, { | ||
key: 'any', | ||
value: function any(id) { | ||
var _this3 = this; | ||
if (id === undefined && arguments.length === 0) { | ||
return !isEmpty(this._values) || this.keys.length > 0; | ||
} | ||
return this.selectOrFind(id, function (i) { | ||
var state = _this3._store.find(i); | ||
return !!state && state.any(); | ||
}); | ||
} | ||
}, { | ||
key: 'selectOrFind', | ||
value: function selectOrFind(id, access) { | ||
if (id === '') { | ||
@@ -112,5 +158,5 @@ return this; | ||
if (this._id.length === 0) { | ||
return this._store.select(id); | ||
return access(id); | ||
} else { | ||
return this._store.select(State.join(this._id, id)); | ||
return access(State.join(this._id, id)); | ||
} | ||
@@ -117,0 +163,0 @@ } |
{ | ||
"name": "electrum-store", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Electrum store provides a store implementation tailored for Electrum.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -102,2 +102,10 @@ # Electrum Store | ||
## Read from state | ||
* `get (id)` or `get ()` → the value for `id` (or the default value) if it | ||
exists, otherwise `undefined`. | ||
* `any (id)` or `any ()` → `true` if the state specified by `id` exists | ||
and if it is non-empty. | ||
* `contains (id)` → `true` if a value exists for `id`, otherwise `false`. | ||
## Create state | ||
@@ -104,0 +112,0 @@ |
@@ -24,2 +24,9 @@ 'use strict'; | ||
function isEmpty (obj) { | ||
for (let x in obj) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/******************************************************************************/ | ||
@@ -72,2 +79,6 @@ | ||
contains (id) { | ||
return this.get (id) !== undefined; | ||
} | ||
get (id) { | ||
@@ -91,2 +102,23 @@ if (id === undefined) { | ||
} | ||
return this.selectOrFind (id, i => this._store.select (i)); | ||
} | ||
find (id) { | ||
if ((id === undefined) && (arguments.length === 0)) { | ||
return this; | ||
} | ||
return this.selectOrFind (id, i => this._store.find (i)); | ||
} | ||
any (id) { | ||
if ((id === undefined) && (arguments.length === 0)) { | ||
return !isEmpty (this._values) || this.keys.length > 0; | ||
} | ||
return this.selectOrFind (id, i => { | ||
const state = this._store.find (i); | ||
return !!state && state.any (); | ||
}); | ||
} | ||
selectOrFind (id, access) { | ||
if (id === '') { | ||
@@ -99,5 +131,5 @@ return this; | ||
if (this._id.length === 0) { | ||
return this._store.select (id); | ||
return access (id); | ||
} else { | ||
return this._store.select (State.join (this._id, id)); | ||
return access (State.join (this._id, id)); | ||
} | ||
@@ -104,0 +136,0 @@ } |
@@ -47,4 +47,22 @@ 'use strict'; | ||
}); | ||
it ('returns undefined for unknown id', () => { | ||
const state = State.create ('a', {123: 'X', y: 'Y'}); | ||
expect (state.get ('z')).to.be.undefined (); | ||
}); | ||
}); | ||
describe ('contains()', () => { | ||
it ('returns true for existing id', () => { | ||
const state = State.create ('a', {123: 'X', y: 'Y'}); | ||
expect (state.contains (123)).to.equal (true); | ||
expect (state.contains ('y')).to.equal (true); | ||
}); | ||
it ('returns false for unknown id', () => { | ||
const state = State.create ('a', {123: 'X', y: 'Y'}); | ||
expect (state.contains ('z')).to.equal (false); | ||
}); | ||
}); | ||
describe ('set()', () => { | ||
@@ -79,2 +97,3 @@ it ('produces new instance of state', () => { | ||
expect (root.select ('a.b.c')).to.equal (state3); | ||
expect (root.select ('a.x')).to.exist (); | ||
expect (state1.select ('b')).to.equal (state2); | ||
@@ -100,2 +119,64 @@ expect (state1.select ('b.c')).to.equal (state3); | ||
describe ('find()', () => { | ||
it ('selects child state', () => { | ||
const store = Store.create (); | ||
const state3 = store.select ('a.b.c'); | ||
const state1 = store.select ('a'); | ||
const state2 = store.select ('a.b'); | ||
const root = store.root; | ||
expect (root).to.have.property ('id', ''); | ||
expect (root.find ()).to.equal (root); | ||
expect (root.find ('a')).to.equal (state1); | ||
expect (root.find ('a.b')).to.equal (state2); | ||
expect (root.find ('a.b.c')).to.equal (state3); | ||
expect (root.find ('a.x')).to.not.exist (); | ||
expect (state1.find ('b')).to.equal (state2); | ||
expect (state1.find ('b.c')).to.equal (state3); | ||
expect (state1.find ()).to.equal (state1); | ||
expect (state1.find ('')).to.equal (state1); | ||
}); | ||
it ('without arguments finds self', () => { | ||
const store = Store.create (); | ||
const state1 = store.select ('a'); | ||
const state2 = state1.find (); | ||
expect (state2).to.equal (state1); | ||
}); | ||
it ('throws for invalid ids', () => { | ||
const store = Store.create (); | ||
expect (() => store.root.find (1)).to.throw (Error); | ||
expect (() => store.root.find ({})).to.throw (Error); | ||
}); | ||
}); | ||
describe ('any()', () => { | ||
it ('returns true if state contains children', () => { | ||
const store = Store.create (); | ||
store.select ('a.b.c'); | ||
const root = store.root; | ||
expect (root.any ()).to.be.true (); | ||
expect (root.any ('a')).to.be.true (); | ||
expect (root.any ('a.b')).to.be.true (); | ||
}); | ||
it ('returns false if state contains no children', () => { | ||
const store = Store.create (); | ||
store.select ('a.b.c'); | ||
const root = store.root; | ||
expect (root.any ('a.b.c')).to.be.false (); // exists, but is empty | ||
}); | ||
it ('returns false if state does not exist', () => { | ||
const store = Store.create (); | ||
store.select ('a.b.c'); | ||
const root = store.root; | ||
expect (root.any ('a.b.d')).to.be.false (); // does not exist | ||
}); | ||
it ('returns true if state contains nodes', () => { | ||
const store = Store.create (); | ||
expect (store.root.any ('a.b.c')).to.be.false (); | ||
store.select ('a.b.c').set ('x', 'X'); | ||
expect (store.root.any ('a.b.c')).to.be.true (); | ||
}); | ||
}); | ||
describe ('keys', () => { | ||
@@ -102,0 +183,0 @@ it ('returns keys of children', () => { |
70484
1686
175