This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
storage.js
333 lines (261 loc) · 7.34 KB
/
storage.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"use strict";
module.exports = storage;
var slice = require("../slice");
function pass (value) {
return value;
}
function storage (options) {
var split_key = options.keys.split_key;
var merge_key = options.keys.merge_key;
var copy = options.keys.copy;
var new_node = options.keys.new_node;
var new_dump = options.keys.new_dump;
var min_depth = options.keys.min_depth;
var zero = options.keys.zero;
var iterate = options.keys.iterate;
var store = options.store;
var immutable = store.immutable;
var extend = options.extend || function () {};
var calc_props = options.props || function () {};
create.is_it = options.is_it;
Object.freeze(create);
return create;
function create (input) {
var result = api();
iterate(input, function (value, key) {
result = result.set(key, store(value));
});
return result;
}
function api (data, depth, props) {
depth = depth || 1;
props = props || {};
var set = modifier({
name: "set",
descending: copy,
leaf: set_value,
ascending: set_child
});
var update = modifier({
name: "update",
descending: copy,
leaf: process_value,
ascending: set_child
});
var modify_patch = modifier({
name: "patch",
descending: copy,
leaf: patch_value,
ascending: set_child
});
var modify_rm = modifier({
name: "rm",
descending: copy_only_existing,
leaf: delete_key,
ascending: set_child
});
get.data = data;
get.depth = depth;
get.dump = dump;
get.json = json;
// TODO: deepset!
get.set = set;
get.update = update;
get.patch = patch;
get.rm = rm;
get.forEach = forEach;
get.reduce = reduce;
get.map = map;
get.nmap = nmap;
get.filter = filter;
extend(get, store);
for (var name in props) {
get[name] = props[name];
}
Object.freeze(get);
return get;
function get (key) {
var key_depth = min_depth(key);
// The tree never stored keys that big, nothing to return;
if (key_depth > depth) return;
var key_parts = split_key(key, depth);
return key_parts.reduce(function (node, key_part) {
return node && node[key_part];
}, data);
}
function dump () {
return reduce(function (result, value, key) {
value = (value && typeof value.dump === "function"
? value.dump()
: value);
result[key] = value;
return result;
}, new_dump());
}
function json () {
return JSON.stringify(dump());
}
function grow (dest_depth) {
var root = copy(data);
var levels = dest_depth - depth;
var node;
for (var i = 0; i < levels; ++i) {
node = root;
root = new_node();
root[zero] = node;
}
return root;
}
function shrink (root, old_depth, new_props) {
var elements = 0;
iterate(root, function () {
++elements;
});
return ((elements === 1 && old_depth > 1 && zero in root)
// We have only one element with neutral key and we can
// decrease the depth
? shrink(root[zero], old_depth - 1, new_props)
: elements === 0
// We have no elements, we can return empty collection
? api()
// We have more than one element, we cannot decrease depth
: api(root, old_depth, new_props));
}
function patch (name, value) {
if (typeof name === "object") {
// TODO: support diffs as immutable structures
var diff = name;
var result = get;
for (name in diff) {
result = result.patch(name, diff[name]);
}
return result;
} else {
return modify_patch(name, value);
}
return result;
}
function rm (key) {
if (arguments.length > 1) {
var subargs = slice(arguments, 1);
return update(key, function (node) {
return node.rm.apply(null, subargs);
});
}
var key_depth = min_depth(key);
// The tree never stored keys that big, nothing to delete.
if (key_depth > depth) return get;
return modify_rm(key);
}
function modifier (actions) {
var name = actions.name;
var descending_action = actions.descending;
var leaf_action = actions.leaf;
var ascending_action = actions.ascending;
return function modify (key, value) {
var new_depth = Math.max(min_depth(key), depth);
var key_parts = split_key(key, new_depth);
var length = new_depth - 1;
var root = grow(new_depth);
var i, parent;
var path = new Array(length);
var node = root;
// descending
for (i = 0; i < length; ++i) {
path[i] = node;
node = descending_action(node[key_parts[i]]);
}
leaf_action(node, key_parts[i], value);
Object.freeze(node);
// ascending
for (i = length - 1; i >= 0; --i) {
parent = path[i];
ascending_action(parent, key_parts[i], node);
node = parent;
Object.freeze(parent);
}
var new_props = calc_props(props, name, key);
return shrink(root, new_depth, new_props);
}
}
function map (fn) {
return reduce(function (result, value, key) {
return result.set(key, fn(value, key, get));
}, api());
}
function nmap (fn) {
return reduce(function (result, value, key) {
result[key] = fn(value, key, get);
return result;
}, new_dump());
}
function reduce (fn, init) {
var result = init;
forEach(function (value, key) {
result = fn(result, value, key, get);
});
return result;
}
function filter (fn) {
return reduce(function (result, value, key) {
return (fn(value, key)
? result.set(key, value)
: result);
}, api());
}
function forEach (fn) {
for_recur(data, zero, 1, fn);
return get;
}
function for_recur (node, key_pre, level, fn) {
var invoke = level === depth;
iterate(node, function (child, key) {
var key_full = merge_key(key_pre, key);
if (invoke) {
fn(child, key_full, get);
} else {
for_recur(child, key_full, level + 1, fn);
}
});
}
}
function copy_only_existing (node) {
return node ? copy(node) : null
}
function delete_key (node, key_part) {
if (node) delete node[key_part];
}
function set_child (parent, key_part, child) {
if (!parent) return;
if (is_empty(child)) {
delete parent[key_part];
} else {
parent[key_part] = child;
}
}
function set_value (node, key_part, value) {
node[key_part] = store(value);
}
function process_value (node, key_part, fn) {
node[key_part] = store(fn(node[key_part]));
}
function patch_value (node, key_part, value) {
var old = node[key_part];
if (immutable(value)) {
old = store(value);
} else if (old && old.patch) {
old = old.patch(value);
} else {
old = store(value);
}
node[key_part] = old;
}
function is_empty (node) {
if (!node) return true;
var empty = true;
iterate(node, function () {
empty = false;
});
return empty;
}
}