electrum-store
Advanced tools
Comparing version 1.6.0 to 1.7.0
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Store = exports.State = undefined; | ||
var _state = require('./store/state.js'); | ||
@@ -13,5 +18,3 @@ | ||
module.exports = { | ||
State: _state2.default, | ||
Store: _store2.default | ||
}; | ||
exports.State = _state2.default; | ||
exports.Store = _store2.default; |
@@ -8,2 +8,11 @@ 'use strict'; | ||
function isPositiveInteger(value) { | ||
if (typeof value === 'number') { | ||
if (Math.floor(value) === value && value >= 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function verifyMutationKeys(mutation) { | ||
@@ -26,3 +35,5 @@ if (typeof mutation !== 'object') { | ||
function isEmpty(obj) { | ||
/*jshint -W089 */ | ||
for (let x in obj) { | ||
/* jshint unused:false */ | ||
return false; | ||
@@ -169,6 +180,4 @@ } | ||
} | ||
if (typeof id === 'number') { | ||
if (Math.floor(id) === id && id >= 0) { | ||
id = '' + id; | ||
} | ||
if (isPositiveInteger(id)) { | ||
id = '' + id; | ||
} | ||
@@ -243,2 +252,26 @@ if (typeof id !== 'string') { | ||
static getAncestorId(id, part) { | ||
if (typeof id !== 'string') { | ||
throw new Error('State.getAncestorId expects a string id'); | ||
} | ||
if (isPositiveInteger(part)) { | ||
part = '' + part; | ||
} | ||
if (typeof part !== 'string') { | ||
throw new Error('State.getAncestorId expects a string part'); | ||
} | ||
if (part.indexOf('.') >= 0) { | ||
throw new Error('State.getAncestorId part cannot be a path specification'); | ||
} | ||
if (part === '') { | ||
return ''; | ||
} | ||
const parts = id.split('.'); | ||
for (let i = parts.length; i > 0; --i) { | ||
if (parts[i - 1] === part) { | ||
return parts.slice(0, i).join('.'); | ||
} | ||
} | ||
} | ||
static withValue(state, id, value) { | ||
@@ -245,0 +278,0 @@ if (arguments.length !== 3) { |
{ | ||
"name": "electrum-store", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Electrum store provides a store implementation tailored for Electrum.", | ||
@@ -22,21 +22,21 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.3.17", | ||
"babel-core": "^6.3.17", | ||
"babel-plugin-transform-decorators": "^6.3.13", | ||
"babel-plugin-transform-react-display-name": "^6.3.13", | ||
"babel-polyfill": "^6.3.14", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-cli": "^6.5.1", | ||
"babel-core": "^6.5.1", | ||
"babel-plugin-transform-decorators": "^6.5.0", | ||
"babel-plugin-transform-react-display-name": "^6.5.0", | ||
"babel-polyfill": "^6.5.0", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-preset-node5": "^10.5.0", | ||
"babel-preset-react": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"chai": "^3.4.1", | ||
"jsdom": "^7.2.1", | ||
"babel-preset-react": "^6.5.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"chai": "^3.5.0", | ||
"jsdom": "^8.0.2", | ||
"mai-chai": "^1.1.2", | ||
"mocha": "^2.3.4", | ||
"mocha": "^2.4.5", | ||
"path": "^0.12.7", | ||
"react": "^0.14.3", | ||
"react-element-to-jsx-string": "^2.1.4", | ||
"react": "^0.14.7", | ||
"react-element-to-jsx-string": "^2.4.0", | ||
"require-self": "^0.1.0", | ||
"rimraf": "^2.4.4" | ||
"rimraf": "^2.5.1" | ||
} | ||
} |
@@ -155,2 +155,21 @@ # Electrum Store | ||
## Working with state ids | ||
State ids are similar to _paths_ where the elements are separated by `.`. | ||
Class `State` provides some static methods to manipulate these ids: | ||
* `State.join (a, b, c, ...)` → returns the joined path. | ||
* `State.getLeafId (id)` → returns the last element of the path. | ||
* `State.getParentId (id)` → returns the path of the immediate parent. | ||
* `State.getAncestorId (id, part)` → returns the path of the first ancestor | ||
which contains the specified part (path element); if the last element of the | ||
`id` matches `part`, the full `id` will be returned. | ||
```javascript | ||
expect (State.join ('a', 'b', 'c')).to.equal ('a.b.c'); | ||
expect (State.getLeafId ('a.b.c')).to.equal ('c'); | ||
expect (State.getParentId ('a.b.c')).to.equal ('a.b'); | ||
expect (State.getAncestorId ('a.b.c', 'b')).to.equal ('a.b'); | ||
``` | ||
## Adding items to an array | ||
@@ -157,0 +176,0 @@ |
'use strict'; | ||
import State from './store/state.js'; | ||
import Store from './store/store.js'; | ||
module.exports = { | ||
State, | ||
Store | ||
}; | ||
export State from './store/state.js'; | ||
export Store from './store/store.js'; |
@@ -8,2 +8,11 @@ 'use strict'; | ||
function isPositiveInteger (value) { | ||
if (typeof value === 'number') { | ||
if (Math.floor (value) === value && value >= 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function verifyMutationKeys (mutation) { | ||
@@ -25,4 +34,4 @@ if (typeof mutation !== 'object') { | ||
function isEmpty (obj) { | ||
for (let x in obj) { | ||
function isEmpty (obj) { /*jshint -W089 */ | ||
for (let x in obj) { /* jshint unused:false */ | ||
return false; | ||
@@ -165,6 +174,4 @@ } | ||
} | ||
if (typeof id === 'number') { | ||
if (Math.floor (id) === id && id >= 0) { | ||
id = '' + id; | ||
} | ||
if (isPositiveInteger (id)) { | ||
id = '' + id; | ||
} | ||
@@ -234,2 +241,26 @@ if (typeof id !== 'string') { | ||
static getAncestorId (id, part) { | ||
if (typeof id !== 'string') { | ||
throw new Error ('State.getAncestorId expects a string id'); | ||
} | ||
if (isPositiveInteger (part)) { | ||
part = '' + part; | ||
} | ||
if (typeof part !== 'string') { | ||
throw new Error ('State.getAncestorId expects a string part'); | ||
} | ||
if (part.indexOf ('.') >= 0) { | ||
throw new Error ('State.getAncestorId part cannot be a path specification'); | ||
} | ||
if (part === '') { | ||
return ''; | ||
} | ||
const parts = id.split ('.'); | ||
for (let i = parts.length; i > 0; --i) { | ||
if (parts[i - 1] === part) { | ||
return parts.slice (0, i).join ('.'); | ||
} | ||
} | ||
} | ||
static withValue (state, id, value) { | ||
@@ -236,0 +267,0 @@ if (arguments.length !== 3) { |
@@ -68,2 +68,23 @@ 'use strict'; | ||
describe ('State.getAncestorId()', () => { | ||
it ('returns the ancestor id', () => { | ||
expect (State.getAncestorId ('a.b.c', 'c')).to.equal ('a.b.c'); | ||
expect (State.getAncestorId ('a.b.c', 'b')).to.equal ('a.b'); | ||
expect (State.getAncestorId ('a.b.c', 'a')).to.equal ('a'); | ||
expect (State.getAncestorId ('a.b.c', '')).to.equal (''); | ||
}); | ||
it ('accepts numeric part', () => { | ||
expect (State.getAncestorId ('a.1.c', 1)).to.equal ('a.1'); | ||
}); | ||
it ('returns undefined when there is no match', () => { | ||
expect (State.getAncestorId ('a.b.c', 'x')).to.be.undefined (); | ||
}); | ||
it ('rejects part which contains "."', () => { | ||
expect (() => State.getAncestorId ('a.b.c', 'a.b')).to.throw ('cannot be a path specification'); | ||
}); | ||
}); | ||
describe ('State.withValue()', () => { | ||
@@ -70,0 +91,0 @@ it ('mutates the state when the value changes', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75017
1740
261