Skip to content

Commit ff3cf5f

Browse files
David JollyDavid Jolly
authored andcommitted
Refactored for testability.
1 parent 041ed14 commit ff3cf5f

4 files changed

Lines changed: 90 additions & 69 deletions

File tree

DeepLinkStateMixin.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

DeepLinkedStateLib.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
5+
var DeepLinkedStateLib = {
6+
getValueFromState: function(statePath, options) {
7+
return DeepLinkedStateLib.getValueFromObject(statePath, options, this.state);
8+
},
9+
10+
getValueFromObject: function(statePath, options, valueObject) {
11+
options = options || {};
12+
13+
var value = valueObject;
14+
15+
var havePath = _.all(statePath, function(statePathPart) {
16+
if (!_.isObject(value) || !value.hasOwnProperty(statePathPart)) {
17+
return false;
18+
}
19+
20+
value = value[statePathPart];
21+
22+
return true;
23+
});
24+
25+
if (!havePath) {
26+
value = null;
27+
}
28+
29+
if (options.storeEmptyStringAsNull) {
30+
if (value === null) {
31+
value = '';
32+
}
33+
}
34+
35+
return value;
36+
},
37+
38+
onChange: function(statePath, options, value) {
39+
var partialState = DeepLinkedStateLib.updateValueObject(statePath, options, value, this.state);
40+
41+
this.setState(partialState);
42+
},
43+
44+
updateValueObject: function(statePath, options, value, valueObject) {
45+
options = options || {};
46+
47+
var statePathDepth = [].concat(statePath);
48+
var statePathLast = statePathDepth.pop();
49+
50+
var partialState = _.pick(valueObject, statePathDepth[0]);
51+
var stateSub = partialState;
52+
53+
_.each(statePathDepth, function(statePathPart) {
54+
stateSub[statePathPart] = _.extend({}, stateSub[statePathPart]);
55+
56+
stateSub = stateSub[statePathPart];
57+
});
58+
59+
if (options.storeEmptyStringAsNull) {
60+
if (value === '') {
61+
value = null;
62+
}
63+
}
64+
65+
stateSub[statePathLast] = value;
66+
67+
return partialState;
68+
}
69+
};
70+
71+
module.exports = DeepLinkedStateLib;

DeepLinkedStateMixin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var ReactLink = require('react/lib/ReactLink');
4+
5+
var DeepLinkedStateLib = require('./DeepLinkedStateLib');
6+
7+
var DeepLinkedStateMixin = {
8+
deepLinkState: function(statePath, options) {
9+
return new ReactLink(
10+
DeepLinkedStateLib.getValueFromState.call(this, statePath, options),
11+
DeepLinkedStateLib.onChange.bind(this, statePath, options)
12+
);
13+
}
14+
};
15+
16+
module.exports = DeepLinkedStateMixin;

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"dependencies": {
2828
"lodash": "^3.6.0",
2929
"react": "^0.13.1"
30+
},
31+
"devDependencies": {
32+
"mocha": "^2.2.4"
3033
}
3134
}

0 commit comments

Comments
 (0)