-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
157 lines (142 loc) · 4.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var Baobab = require('baobab');
function deepmerge(target, src) {
var array = Array.isArray(src);
var dst = array && [] || {};
if (array) {
target = target || [];
dst = src.slice();
src.forEach(function(e, i) {
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object') {
dst[i] = deepmerge(target[i], e);
}
});
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
dst[key] = target[key];
})
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
if (!target[key]) {
dst[key] = src[key];
} else {
dst[key] = deepmerge(target[key], src[key]);
}
}
});
}
return dst;
};
var Model = function (initialState, options) {
options = options || {};
var tree = new Baobab(initialState, options);
var model = function (controller) {
controller.on('reset', function () {
tree.set(initialState);
});
controller.on('seek', function (seek, recording) {
recording.initialState.forEach(function (state) {
tree.set(state.path, state.value);
});
});
return {
tree: tree,
logModel: function () {
return tree.get();
},
accessors: {
get: function (path) {
return tree.get(path);
},
toJSON: function () {
return tree.toJSON();
},
toJS: function (path) {
return tree.get(path);
},
serialize: function (path) {
return tree.serialize(path);
},
export: function () {
return tree.serialize();
},
keys: function (path) {
return Object.keys(tree.get(path));
},
findWhere: function (path, obj) {
var keysCount = Object.keys(obj).length;
return tree.get(path).filter(function (item) {
return Object.keys(item).filter(function (key) {
return key in obj && obj[key] === item[key];
}).length === keysCount;
}).pop();
}
},
mutators: {
set: function (path, value) {
tree.set(path, value);
},
import: function (newState) {
var newState = deepmerge(initialState, newState);
tree.set(newState);
},
unset: function (path, keys) {
if (keys) {
keys.forEach(function (key) {
tree.unset(path.concat(key));
})
} else {
tree.unset(path);
}
},
push: function (path, value) {
tree.push(path, value);
},
splice: function () {
var args = [].slice.call(arguments);
tree.splice.call(tree, args.shift(), args);
},
merge: function (path, value) {
tree.merge(path, value);
},
concat: function (path, value) {
tree.apply(path, function (existingValue) {
return existingValue.concat(value);
});
},
pop: function (path) {
var val;
tree.apply(path, function (existingValue) {
var copy = existingValue.slice();
val = copy.pop();
return copy;
});
return val;
},
shift: function (path) {
var val;
tree.apply(path, function (existingValue) {
var copy = existingValue.slice();
val = copy.shift();
return copy;
});
return val;
},
unshift: function (path, value) {
tree.unshift(path, value);
}
}
};
};
model.tree = tree;
return model;
};
Model.monkey = Baobab.monkey;
Model.dynamicNode = Baobab.dynamicNode;
module.exports = Model;