electrum-field
Advanced tools
Comparing version 1.0.1 to 1.1.0
'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 _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; }; }(); | ||
@@ -31,3 +31,3 @@ Object.defineProperty(exports, "__esModule", { | ||
var FieldStates = (function () { | ||
var FieldStates = function () { | ||
function FieldStates(key, states) { | ||
@@ -48,5 +48,19 @@ _classCallCheck(this, FieldStates); | ||
}, { | ||
key: 'find', | ||
value: function find(fingerprint) { | ||
var match = Utils.findState(this._states, fingerprint); | ||
if (match) { | ||
return Object.assign({}, match); | ||
} | ||
} | ||
}, { | ||
key: 'add', | ||
value: function add(state) { | ||
return this.mutate(Utils.replaceState(this._states, state)); | ||
if (arguments.length === 0) { | ||
return this; | ||
} else if (arguments.length === 1) { | ||
return this.mutate(Utils.replaceState(this._states, state)); | ||
} else { | ||
return this.mutate(Utils.replaceStates.apply(Utils, [this._states].concat(Array.prototype.slice.call(arguments)))); | ||
} | ||
} | ||
@@ -85,14 +99,13 @@ }, { | ||
} | ||
}, { | ||
key: 'fingerprint', | ||
value: function fingerprint(state) { | ||
return Utils.fingerprint(state); | ||
} | ||
}]); | ||
return FieldStates; | ||
})(); | ||
}(); | ||
/******************************************************************************/ | ||
exports.default = FieldStates; | ||
exports.default = FieldStates; | ||
FieldStates.fingerprint = Utils.fingerprint; | ||
FieldStates.clone = clone; | ||
/******************************************************************************/ |
'use strict'; | ||
/******************************************************************************/ | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -14,11 +16,2 @@ value: true | ||
} | ||
}); | ||
var _eventHandlers = require('./event-handlers.js'); | ||
Object.defineProperty(exports, 'EventHandlers', { | ||
enumerable: true, | ||
get: function get() { | ||
return _eventHandlers.default; | ||
} | ||
}); |
{ | ||
"name": "electrum-field", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Electrum Field handles state associated with form fields.", | ||
@@ -25,4 +25,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.3.15", | ||
"babel-core": "^6.3.15", | ||
"babel-cli": "^6.4.5", | ||
"babel-core": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13", | ||
@@ -32,10 +32,10 @@ "babel-preset-react": "^6.3.13", | ||
"chai": "^3.4.1", | ||
"jsdom": "^7.2.0", | ||
"fbjs": "^0.5.0", | ||
"fbjs": "^0.6.1", | ||
"jsdom": "^7.2.2", | ||
"mai-chai": "^1.1.2", | ||
"mocha": "^2.3.4", | ||
"path": "^0.12.7", | ||
"react": "^0.14.3", | ||
"react-dom": "^0.14.3" | ||
"react": "^0.14.6", | ||
"react-dom": "^0.14.6" | ||
} | ||
} |
@@ -27,8 +27,11 @@ # Electrum Field | ||
```javascript | ||
import {FieldStates} from 'electrum-field'; | ||
const state = { | ||
id: '1234', | ||
begin: 12, | ||
end: 23 | ||
end: 23 | ||
}; | ||
expect (fingerprint (state)).to.equal ('begin,end'); | ||
expect (FieldStates.fingerprint (state)).to.equal ('begin,end'); | ||
``` | ||
@@ -35,0 +38,0 @@ |
@@ -27,4 +27,17 @@ 'use strict'; | ||
find (fingerprint) { | ||
const match = Utils.findState (this._states, fingerprint); | ||
if (match) { | ||
return Object.assign ({}, match); | ||
} | ||
} | ||
add (state) { | ||
return this.mutate (Utils.replaceState (this._states, state)); | ||
if (arguments.length === 0) { | ||
return this; | ||
} else if (arguments.length === 1) { | ||
return this.mutate (Utils.replaceState (this._states, state)); | ||
} else { | ||
return this.mutate (Utils.replaceStates (this._states, ...arguments)); | ||
} | ||
} | ||
@@ -55,8 +68,9 @@ | ||
} | ||
static fingerprint (state) { | ||
return Utils.fingerprint (state); | ||
} | ||
} | ||
/******************************************************************************/ | ||
FieldStates.fingerprint = Utils.fingerprint; | ||
FieldStates.clone = clone; | ||
/******************************************************************************/ |
'use strict'; | ||
/******************************************************************************/ | ||
export {default as FieldStates} from './field-states.js'; | ||
export {default as EventHandlers} from './event-handlers.js'; | ||
/******************************************************************************/ |
@@ -29,2 +29,17 @@ 'use strict'; | ||
describe ('find()', () => { | ||
it ('retrieves a copy of the first matching state', () => { | ||
const state1 = {x: 1}; | ||
const state2 = {y: 2}; | ||
const fs = FieldStates.from (state1, state2); | ||
expect (fs.find ('x')).to.have.property ('x', 1); | ||
expect (fs.find ('y')).to.have.property ('y', 2); | ||
expect (fs.find ('x')).to.not.equal (state1); | ||
// Make sure the returned object is a copy of the original; the state | ||
// behaves as if it were immutable: | ||
fs.find ('x').z = 3; | ||
expect (fs.find ('x')).to.not.have.property ('z'); | ||
}); | ||
}); | ||
describe ('add()', () => { | ||
@@ -40,3 +55,13 @@ it ('adds new state', () => { | ||
expect (fs4.get ()).to.deep.equal ([{x: 'X'}]); | ||
expect (fs4.find ('x')).has.property ('x', 'X'); | ||
}); | ||
it ('adds multiple states in one call', () => { | ||
const fs1 = FieldStates.create (); | ||
const fs2 = fs1.add ({x: 'X'}, {y: 'Y'}, {a: 'A'}); | ||
const fs3 = fs2.add ({a: 'A'}, {x: 'X'}); | ||
expect (fs3).to.equal (fs2); | ||
expect (fs2.find ('x')).has.property ('x', 'X'); | ||
expect (fs2.find ('y')).has.property ('y', 'Y'); | ||
expect (fs2.find ('a')).has.property ('a', 'A'); | ||
}); | ||
}); | ||
@@ -43,0 +68,0 @@ |
33795
692
67