Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

electrum-store

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electrum-store - npm Package Compare versions

Comparing version 1.7.0 to 1.8.0

.gitattributes

517

lib/store/state.js
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
require('babel-polyfill');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
const emptyValues = {};

@@ -44,4 +48,6 @@ const secretKey = {};

class State {
constructor(key, id, store, generation, values) {
let State = function () {
function State(key, id, store, generation, values) {
_classCallCheck(this, State);
if (key !== secretKey) {

@@ -66,284 +72,317 @@ throw new Error('Do not call State constructor directly; use State.create instead');

get id() {
return this._id;
}
get key() {
return State.getLeafId(this._id);
}
get parentId() {
return State.getParentId(this._id);
}
get store() {
return this._store;
}
get generation() {
return this._generation;
}
get value() {
return this.get();
}
get keys() {
return this.store.getKeys(this.id);
}
get indexKeys() {
return this.store.getIndexKeys(this.id);
}
contains(id) {
return this.get(id) !== undefined;
}
get(id) {
if (id === undefined) {
id = '';
_createClass(State, [{
key: 'contains',
value: function contains(id) {
return this.get(id) !== undefined;
}
return this._values[id];
}
getInherited(id) {
const value = this.get(id);
if (value !== undefined) {
return value;
}, {
key: 'get',
value: function get(id) {
if (id === undefined) {
id = '';
}
return this._values[id];
}
const parent = this.parentId;
if (parent !== undefined) {
return this.store.find(parent).getInherited(id);
}, {
key: 'getInherited',
value: function getInherited(id) {
const value = this.get(id);
if (value !== undefined) {
return value;
}
const parent = this.parentId;
if (parent !== undefined) {
return this.store.find(parent).getInherited(id);
}
}
}
}, {
key: 'set',
value: function set() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
set() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
if (args.length === 1) {
return this.set('', args[0]);
}
return State.withValues(this, ...args);
}
if (args.length === 1) {
return this.set('', args[0]);
}, {
key: 'add',
value: function add() {
const keys = this.indexKeys;
const next = keys.length === 0 ? 0 : keys[keys.length - 1] + 1;
return this.select('' + next);
}
return State.withValues(this, ...args);
}
add() {
const keys = this.indexKeys;
const next = keys.length === 0 ? 0 : keys[keys.length - 1] + 1;
return this.select('' + next);
}
remove(id) {
if (id === undefined && arguments.length === 0) {
return this.store.remove(this.id);
}, {
key: 'remove',
value: function remove(id) {
if (id === undefined && arguments.length === 0) {
return this.store.remove(this.id);
}
return this.selectOrFind(id, i => this._store.remove(i));
}
return this.selectOrFind(id, i => this._store.remove(i));
}
select(id) {
if (id === undefined && arguments.length === 0) {
return this;
}, {
key: 'select',
value: function select(id) {
if (id === undefined && arguments.length === 0) {
return this;
}
return this.selectOrFind(id, i => this._store.select(i));
}
return this.selectOrFind(id, i => this._store.select(i));
}
find(id) {
if (id === undefined && arguments.length === 0) {
return this;
}, {
key: 'find',
value: function find(id) {
if (id === undefined && arguments.length === 0) {
return this;
}
return this.selectOrFind(id, i => this._store.find(i));
}
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;
}, {
key: 'any',
value: function 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();
});
}
return this.selectOrFind(id, i => {
const state = this._store.find(i);
return !!state && state.any();
});
}
exists(id) {
return this.selectOrFind(id, i => {
const state = this._store.find(i);
return !!state;
});
}
selectOrFind(id, access) {
if (id === '') {
return this;
}, {
key: 'exists',
value: function exists(id) {
return this.selectOrFind(id, i => {
const state = this._store.find(i);
return !!state;
});
}
if (isPositiveInteger(id)) {
id = '' + id;
}, {
key: 'selectOrFind',
value: function selectOrFind(id, access) {
if (id === '') {
return this;
}
if (isPositiveInteger(id)) {
id = '' + id;
}
if (typeof id !== 'string') {
throw new Error('Invalid state id');
}
if (this._id.length === 0) {
return access(id);
} else {
return access(State.join(this._id, id));
}
}
if (typeof id !== 'string') {
throw new Error('Invalid state id');
}, {
key: 'id',
get: function () {
return this._id;
}
if (this._id.length === 0) {
return access(id);
} else {
return access(State.join(this._id, id));
}, {
key: 'key',
get: function () {
return State.getLeafId(this._id);
}
}
static create() {
if (arguments.length === 0) {
throw new Error('No id was provided to State.create()');
}, {
key: 'parentId',
get: function () {
return State.getParentId(this._id);
}
if (arguments.length > 2) {
throw new Error('Too many arguments provided to State.create()');
}, {
key: 'store',
get: function () {
return this._store;
}
const id = arguments.length <= 0 ? undefined : arguments[0];
const values = arguments.length <= 1 ? undefined : arguments[1];
if (typeof id !== 'string' || id.length === 0) {
throw new Error('State expects a valid id');
}, {
key: 'generation',
get: function () {
return this._generation;
}
return new State(secretKey, id, null, 0, values || emptyValues);
}
static createRootState(store, values) {
let initialGeneration = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
return new State(secretKey, '', store, initialGeneration, values || emptyValues);
}
static join() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}, {
key: 'value',
get: function () {
return this.get();
}
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
if (typeof arg !== 'string' || arg.length === 0) {
throw new Error('State.join expects non-empty string ids');
}, {
key: 'keys',
get: function () {
return this.store.getKeys(this.id);
}
}, {
key: 'indexKeys',
get: function () {
return this.store.getIndexKeys(this.id);
}
}], [{
key: 'create',
value: function create() {
if (arguments.length === 0) {
throw new Error('No id was provided to State.create()');
}
}
if (arguments.length > 2) {
throw new Error('Too many arguments provided to State.create()');
}
return args.join('.');
}
const id = arguments.length <= 0 ? undefined : arguments[0];
const values = arguments.length <= 1 ? undefined : arguments[1];
static getLeafId(id) {
if (!id) {
return id;
if (typeof id !== 'string' || id.length === 0) {
throw new Error('State expects a valid id');
}
return new State(secretKey, id, null, 0, values || emptyValues);
}
const pos = id.lastIndexOf('.') + 1;
return pos === 0 ? id : id.substring(pos);
}
}, {
key: 'createRootState',
value: function createRootState(store, values) {
let initialGeneration = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
static getParentId(id) {
if (typeof id !== 'string') {
throw new Error('State.getParentId expects a string id');
return new State(secretKey, '', store, initialGeneration, values || emptyValues);
}
const pos = id.lastIndexOf('.');
if (pos < 0) {
return id !== '' ? '' : undefined;
} else {
return id.substring(0, pos);
}
}
}, {
key: 'join',
value: function join() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
static getAncestorId(id, part) {
if (typeof id !== 'string') {
throw new Error('State.getAncestorId expects a string id');
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
if (typeof arg !== 'string' || arg.length === 0) {
throw new Error('State.join expects non-empty string ids');
}
}
return args.join('.');
}
if (isPositiveInteger(part)) {
part = '' + part;
}, {
key: 'getLeafId',
value: function getLeafId(id) {
if (!id) {
return id;
}
const pos = id.lastIndexOf('.') + 1;
return pos === 0 ? id : id.substring(pos);
}
if (typeof part !== 'string') {
throw new Error('State.getAncestorId expects a string part');
}, {
key: 'getParentId',
value: function getParentId(id) {
if (typeof id !== 'string') {
throw new Error('State.getParentId expects a string id');
}
const pos = id.lastIndexOf('.');
if (pos < 0) {
return id !== '' ? '' : undefined;
} else {
return id.substring(0, pos);
}
}
if (part.indexOf('.') >= 0) {
throw new Error('State.getAncestorId part cannot be a path specification');
}, {
key: 'getAncestorId',
value: function 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('.');
}
}
}
if (part === '') {
return '';
}, {
key: 'withValue',
value: function withValue(state, id, value) {
if (arguments.length !== 3) {
throw new Error('Invalid number of arguments');
}
if (state._values[id] === value) {
return state;
} else {
return State._withValues(state, [id, value]);
}
}
const parts = id.split('.');
for (let i = parts.length; i > 0; --i) {
if (parts[i - 1] === part) {
return parts.slice(0, i).join('.');
}, {
key: 'withValues',
value: function withValues(state) {
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
}
}
static withValue(state, id, value) {
if (arguments.length !== 3) {
throw new Error('Invalid number of arguments');
}
if (state._values[id] === value) {
return state;
} else {
return State._withValues(state, [id, value]);
}
}
if (args.length === 0) {
return state;
}
if (args.length % 2 !== 0) {
throw new Error('Invalid number of arguments');
}
static withValues(state) {
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
return State._withValues(state, args);
}
}, {
key: '_withValues',
value: function _withValues(state, args) {
var values;
if (args.length === 0) {
return state;
}
if (args.length % 2 !== 0) {
throw new Error('Invalid number of arguments');
}
return State._withValues(state, args);
}
static _withValues(state, args) {
var values;
for (let i = 0; i < args.length; i += 2) {
const id = args[i + 0];
const value = args[i + 1];
if (state._values[id] !== value) {
if (!values) {
values = {};
Object.assign(values, state._values);
for (let i = 0; i < args.length; i += 2) {
const id = args[i + 0];
const value = args[i + 1];
if (state._values[id] !== value) {
if (!values) {
values = {};
Object.assign(values, state._values);
}
values[id] = value;
}
values[id] = value;
}
}
if (!values) {
return state;
} else {
return State.with(state, { values: values });
if (!values) {
return state;
} else {
return State.with(state, { values: values });
}
}
}
static with(state, mutation) {
verifyMutationKeys(mutation);
const generation = mutation.generation || state._generation;
const store = mutation.store || state._store;
const values = mutation.values || state._values;
if (state._generation === generation && state._store === store && state._values === values) {
return state;
} else {
state = new State(secretKey, state._id, store, generation, values);
// If the state was already attached to a store, we have to update the
// store so that the new state will be used from now on...
if (state._store) {
// ...however, if the mutation includes a store specification, then
// this means that the store is actually calling us, because it is
// already updating this very state. If so, don't notify the store.
if (!mutation.store) {
return state._store.setState(state);
}, {
key: 'with',
value: function _with(state, mutation) {
verifyMutationKeys(mutation);
const generation = mutation.generation || state._generation;
const store = mutation.store || state._store;
const values = mutation.values || state._values;
if (state._generation === generation && state._store === store && state._values === values) {
return state;
} else {
state = new State(secretKey, state._id, store, generation, values);
// If the state was already attached to a store, we have to update the
// store so that the new state will be used from now on...
if (state._store) {
// ...however, if the mutation includes a store specification, then
// this means that the store is actually calling us, because it is
// already updating this very state. If so, don't notify the store.
if (!mutation.store) {
return state._store.setState(state);
}
}
return state;
}
return state;
}
}
}
}]);
return State;
}();
/******************************************************************************/
module.exports = State;
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _state = require('./state.js');

@@ -11,2 +13,4 @@

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/******************************************************************************/

@@ -57,4 +61,6 @@

class Store {
constructor(id, key, values) {
let Store = function () {
function Store(id, key, values) {
_classCallCheck(this, Store);
if (key !== secretKey) {

@@ -69,131 +75,151 @@ throw new Error('Do not call Store constructor directly; use Store.create instead');

select(id) {
if (arguments.length === 0) {
return this.root;
_createClass(Store, [{
key: 'select',
value: function select(id) {
if (arguments.length === 0) {
return this.root;
}
return this.find(id) || this.setState(_state2.default.create(id));
}
return this.find(id) || this.setState(_state2.default.create(id));
}
remove(id) {
if (arguments.length === 0) {
this._states = {};
this._states[''] = _state2.default.createRootState(this, null, changeGeneration(this));
return true;
} else {
const state = this.find(id);
if (state) {
deleteStates(this, state);
}, {
key: 'remove',
value: function remove(id) {
if (arguments.length === 0) {
this._states = {};
this._states[''] = _state2.default.createRootState(this, null, changeGeneration(this));
return true;
} else {
const state = this.find(id);
if (state) {
deleteStates(this, state);
return true;
}
}
return false;
}
return false;
}
getIds(startId) {
if (arguments.length === 0) {
return [''];
}, {
key: 'getIds',
value: function getIds(startId) {
if (arguments.length === 0) {
return [''];
}
const ids = Object.getOwnPropertyNames(this._states);
if (startId === '') {
// Special case: return all top level nodes
return ids.filter(id => id.length && id.indexOf('.') < 0);
}
const prefix = startId + '.';
const length = prefix.length;
return ids.filter(id => id.startsWith(prefix) && id.indexOf('.', length) < 0);
}
const ids = Object.getOwnPropertyNames(this._states);
if (startId === '') {
// Special case: return all top level nodes
return ids.filter(id => id.length && id.indexOf('.') < 0);
}, {
key: 'getKeys',
value: function getKeys(startId) {
return this.getIds(startId).map(id => _state2.default.getLeafId(id));
}
const prefix = startId + '.';
const length = prefix.length;
return ids.filter(id => id.startsWith(prefix) && id.indexOf('.', length) < 0);
}
getKeys(startId) {
return this.getIds(startId).map(id => _state2.default.getLeafId(id));
}
getIndexIds(startId) {
return this.getIndexKeys(startId).map(key => `${ startId }.${ key }`);
}
getIndexKeys(startId) {
const nums = this.getIds(startId).map(id => (0, _electrumUtils.parsePositiveInt)(_state2.default.getLeafId(id))).filter(num => !isNaN(num));
// Numeric sort required here
nums.sort((a, b) => a - b);
return nums;
}
setState(state) {
if (typeof state === 'string') {
state = _state2.default.create(state);
}, {
key: 'getIndexIds',
value: function getIndexIds(startId) {
return this.getIndexKeys(startId).map(key => `${ startId }.${ key }`);
}
if (!state || !(state instanceof _state2.default)) {
throw new Error('Invalid state');
}, {
key: 'getIndexKeys',
value: function getIndexKeys(startId) {
const nums = this.getIds(startId).map(id => (0, _electrumUtils.parsePositiveInt)(_state2.default.getLeafId(id))).filter(num => !isNaN(num));
// Numeric sort required here
nums.sort((a, b) => a - b);
return nums;
}
}, {
key: 'setState',
value: function setState(state) {
if (typeof state === 'string') {
state = _state2.default.create(state);
}
if (!state || !(state instanceof _state2.default)) {
throw new Error('Invalid state');
}
if (state === this.find(state.id)) {
// No mutation
return state;
} else {
const mutation = {
store: this,
generation: changeGeneration(this)
};
return updateTree(this, state, mutation);
if (state === this.find(state.id)) {
// No mutation
return state;
} else {
const mutation = {
store: this,
generation: changeGeneration(this)
};
return updateTree(this, state, mutation);
}
}
}
find(id) {
if (arguments.length === 0) {
return this.root;
}, {
key: 'find',
value: function find(id) {
if (arguments.length === 0) {
return this.root;
}
if (typeof id !== 'string') {
throw new Error('Invalid state id');
}
if (id.length === 0) {
return this.root;
} else {
return this._states[id];
}
}
if (typeof id !== 'string') {
throw new Error('Invalid state id');
}, {
key: 'id',
get: function () {
return this._id;
}
if (id.length === 0) {
return this.root;
} else {
return this._states[id];
}, {
key: 'generation',
get: function () {
return this._generation;
}
}
}, {
key: 'root',
get: function () {
return this._states[''];
}
}, {
key: 'stateCount',
get: function () {
return Object.keys(this._states).length - 1;
}
get id() {
return this._id;
}
/* static methods */
get generation() {
return this._generation;
}
}], [{
key: 'create',
value: function create(id, values) {
return new Store(id, secretKey, values);
}
}, {
key: 'link',
value: function link(props, id, override) {
const state = props.state;
get root() {
return this._states[''];
}
get stateCount() {
return Object.keys(this._states).length - 1;
}
/* static methods */
static create(id, values) {
return new Store(id, secretKey, values);
}
static link(props, id, override) {
const state = props.state;
let theme = props.theme;
if (override) {
if (override.theme) {
theme = override.theme;
let theme = props.theme;
if (override) {
if (override.theme) {
theme = override.theme;
}
}
return {
state: state.select(id),
theme
};
}
return {
state: state.select(id),
theme
};
}
}, {
key: 'read',
value: function read(props, id) {
const state = props.state;
static read(props, id) {
const state = props.state;
return state.get(id);
}
}]);
return state.get(id);
}
}
return Store;
}();
module.exports = Store;
{
"name": "electrum-store",
"version": "1.7.0",
"version": "1.8.0",
"description": "Electrum store provides a store implementation tailored for Electrum.",
"main": "lib/index.js",
"scripts": {
"compile": "rimraf ./lib && require-self && babel -d lib src",
"babel": "babel --quiet --out-dir lib src && babel --quiet --out-dir lib.test src.test",
"clean": "rimraf ./lib && rimraf ./lib.test",
"compile": "npm run clean && require-self && npm run babel",
"mocha": "mocha lib.test/**/*.js",
"prepublish": "npm run compile",
"test": "mocha src/test/**/*.js"
"test": "npm run compile && npm run mocha"
},

@@ -22,21 +25,9 @@ "repository": {

"devDependencies": {
"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.5.0",
"babel-preset-stage-0": "^6.5.0",
"chai": "^3.5.0",
"jsdom": "^8.0.2",
"mai-chai": "^1.1.2",
"mocha": "^2.4.5",
"path": "^0.12.7",
"babel-env": "^1.1.0",
"generic-js-env": "^1.1.0",
"mai-chai": "^2.4.0",
"react": "^0.14.7",
"react-element-to-jsx-string": "^2.4.0",
"require-self": "^0.1.0",
"rimraf": "^2.5.1"
"rimraf": "^2.5.2"
}
}

@@ -9,2 +9,3 @@ /*globals __dirname */

babelConfig.babel = babel;
module.exports = function (wallaby) {

@@ -14,7 +15,6 @@ return {

{pattern: 'test/test-helper.js'},
{pattern: 'src/store/**/*.js'},
{pattern: 'src/*.js'}
{pattern: 'src/**/*.js'}
],
tests: [
{pattern: 'src/test/**/*.js'},
{pattern: 'src.test/**/*.js'},
],

@@ -29,6 +29,27 @@ compilers: {

},
bootstrap: function () {
bootstrap: function (wallaby) {
// See http://wallabyjs.com/docs/config/bootstrap.html
var path = require ('path');
var sep = path.sep;
console.log ('Setup wallaby');
var sep = require ('path').sep;
// Ensure that we can require self (just like what module 'require-self'
// does), but remapping by default the path to './src' rather than './lib'
// as specified by package "main".
// See https://github.com/wallabyjs/public/issues/453
var packageConfig = require (path.join (wallaby.localProjectDir, 'package.json'));
var packageName = packageConfig.name;
var modulePrototype = require ('module').Module.prototype;
if (!modulePrototype._originalRequire) {
modulePrototype._originalRequire = modulePrototype.require;
modulePrototype.require = function (filePath) {
if (filePath === packageName) {
return modulePrototype._originalRequire.call (this, path.join (wallaby.projectCacheDir, 'src'));
} else {
return modulePrototype._originalRequire.call (this, filePath);
}
};
}
// Remove react from the require.cache, or else some code might not get

@@ -43,2 +64,3 @@ // executed when editing the source code.

});
// Include the test helper, which sets up the document and window objects

@@ -45,0 +67,0 @@ // as globals:

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc