-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
306 lines (262 loc) · 10.6 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
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
"use strict";
// @flow
/**
* Copyright (c) Kristian Lyngbaek 2016
*/
/* flow-include type DataTreeList = {[key: string]: any} */
/* flow-include type DataTreeJS = {data?: any, children: {[key: string]: DataTreeJS}} */
function DataTree() {
this._data = undefined; // _data is always defined
this._children = {}; // _children is always defined
}
DataTree.fromJS = function fromJS(inputJS/*: DataTreeJS*/)/*: DataTree*/ {
var result/*: DataTree*/ = new DataTree();
result.setData(inputJS.data);
for (var childName in inputJS.children) {
var childJS = inputJS.children[childName];
var child = DataTree.fromJS(childJS);
result.setChild(childName, child);
}
return result;
};
DataTree.fromList = function fromList(list/*: DataTreeList*/)/*: DataTree*/ {
var result/*: DataTree*/ = new DataTree();
for (var path in list) {
var data = list[path];
var keys = path.split('/').slice(1).map(decode);
result.setDataIn(keys, data);
}
return result;
function decode(key/*: string*/) {
return decodeURIComponent(key);
}
};
DataTree.concat = function concat(thisData/*: ?Array<any>*/, thatData/*: ?Array<any>*/, keys/*: Array<string>*/)/*: ?Array<any>*/ {
if (thatData && thisData) thisData.concat(thatData);
else if (!thisData && thatData) return thatData.concat([]);
else if (!thatData && thisData) return thisData.concat([]);
else return undefined;
};
DataTree.prototype.getData = function getData()/*: any*/ {
return this.getDataIn([]);
};
DataTree.prototype.getDataIn = function getDataIn(keys/*: Array<string>*/)/*: any*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length===0) {
return this._data;
} else {
var key = keys[0];
var child = this._children[key];
if (child) {
return child.getDataIn(keys.slice(1));
} else {
throw new Error('Child not defined');
// return undefined;
}
}
};
DataTree.prototype.setData = function setData(data)/*: void*/ {
return this.setDataIn([], data);
};
DataTree.prototype.setDataIn = function setDataIn(keys/*: Array<string>*/, data/*: any*/)/*: void*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length===0) {
this._data = data;
} else {
var key = keys[0];
if (!this._children[key]) {
this._children[key] = new DataTree();
}
var child = this._children[key];
child.setDataIn(keys.slice(1), data);
}
};
DataTree.prototype.deleteData = function deleteData()/*: void*/ {
return this.deleteDataIn([]);
};
DataTree.prototype.deleteDataIn = function deleteDataIn(keys/*: Array<string>*/)/*: void*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length === 0) delete this._data;
var nextKey = keys[0];
if (nextKey in this._children) {
this._children[nextKey].deleteDataIn(keys.slice(1));
} else {
return;
}
};
DataTree.prototype.hasData = function hasData()/*: boolean*/ {
return this.hasDataIn([]);
};
DataTree.prototype.hasDataIn = function hasDataIn(keys/*: Array<string>*/)/*: boolean*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length === 0) return this._data!==undefined;
var nextKey = keys[0];
if (nextKey in this._children) {
return this._children[nextKey].hasDataIn(keys.slice(1));
} else {
return false;
}
};
DataTree.prototype.getChildren = function getChildren()/*: {[key: string]: DataTree}*/ {
return this.getChildrenIn([]);
};
DataTree.prototype.getChildrenIn = function getChildrenIn(keys/*: Array<string>*/)/*: {[key: string]: DataTree}*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length===0) {
return this._children;
} else {
var key = keys[0];
var child = this._children[key];
if (child) {
return child.getChildrenIn(keys.slice(1));
} else {
throw new Error('Child not defined');
// return undefined;
}
}
};
DataTree.prototype.hasChildren = function hasChildren()/*: boolean*/ {
return this.hasChildrenIn([]);
};
DataTree.prototype.hasChildrenIn = function hasChildrenIn(keys/*: Array<string>*/)/*: boolean*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length === 0) return Object.keys(this._children).length > 0;
var nextKey = keys[0];
if (nextKey in this._children) {
return this._children[nextKey].hasChildrenIn(keys.slice(1));
} else {
return false;
}
};
DataTree.prototype.merge = function merge(thatDataTree/*: DataTree*/, mergeFunc/*: function*/) {
this.mergeIn([], thatDataTree, mergeFunc);
};
DataTree.prototype.mergeIn = function mergeIn(keys/*: Array<string>*/, thatDataTree/*: DataTree*/, mergeFunc/*: function*/) {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if ( !(thatDataTree instanceof DataTree)) throw new Error('thatDataTree argument must be an instance of DataTree');
if (!this.hasChildIn(keys)) {
this.createChildIn(keys);
}
var thisDataTree = this.getChildIn(keys);
merge(thisDataTree, thatDataTree, keys);
function merge(self, that, keys/*: Array<string>*/) {
var dataResult = mergeFunc(self.getData(), that.getData(), keys);
self.setData(dataResult);
var thatChildren = that.getChildren();
for (var thatChildName in thatChildren) {
var thatChild = thatChildren[thatChildName];
if (!self.hasChild(thatChildName)) self.createChild(thatChildName);
var selfChild = self.getChild(thatChildName);
merge(selfChild, thatChild, keys.concat(thatChildName));
}
}
};
DataTree.prototype.hasChild = function hasChild(childName/*: string*/)/*: boolean*/ {
if ( typeof childName !== 'string' ) throw new Error('childName argument must be a string');
return this.hasChildIn([childName]);
};
DataTree.prototype.hasChildIn = function hasChildIn(keys/*: Array<string>*/)/*: boolean*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
var nextKey = keys[0];
if (keys.length === 1) return nextKey in this._children;
if (nextKey in this._children) {
return this._children[nextKey].hasChildIn(keys.slice(1));
} else {
return false;
}
};
DataTree.prototype.getChild = function getChild(childName/*: string*/)/*: DataTree*/ {
if ( typeof childName !== 'string' ) throw new Error('childName argument must be a string');
return this.getChildIn([childName]);
};
DataTree.prototype.getChildIn = function getChildIn(keys/*: Array<string>*/)/*: DataTree*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length === 0) return this;
var nextKey = keys[0];
if (nextKey in this._children) {
return this._children[nextKey].getChildIn(keys.slice(1));
} else {
throw new Error('Child does not exist');
// return undefined;
}
};
DataTree.prototype.setChild = function setChild(childName/*: string*/, childDataTree/*: DataTree*/)/*: void*/ {
if ( typeof childName !== 'string' ) throw new Error('childName argument must be a string');
if ( !(childDataTree instanceof DataTree)) throw new Error('childDataTree argument must be an instance of DataTree');
this.setChildIn([childName], childDataTree);
};
DataTree.prototype.setChildIn = function setChildIn(keys/*: Array<string>*/, childDataTree/*: DataTree*/)/*: void*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length<1) throw new Error('keys array must length of at least 1');
if ( !(childDataTree instanceof DataTree)) throw new Error('childDataTree argument must be an instance of DataTree');
var key = keys[0];
if (keys.length===1) {
this._children[key] = childDataTree;
} else {
if (!this.hasChild(key)) {
this._children[key] = new DataTree();
}
var child = this.getChild(key);
if (child) {
child.setChildIn(keys.slice(1, keys.length), childDataTree);
} else {
throw new Error('Something went really wrong');
}
}
};
DataTree.prototype.createChild = function createChild(childName/*: string*/)/*: void*/ {
if ( typeof childName !== 'string' ) throw new Error('childName argument must be a string');
this.createChildIn([childName]);
};
DataTree.prototype.createChildIn = function createChildIn(keys/*: Array<string>*/)/*: void*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length<1) throw new Error('keys array must length of at least 1');
this.setChildIn(keys, new DataTree());
};
DataTree.prototype.deleteChild = function deleteChild(childName/*: string*/)/*: void*/ {
if ( typeof childName !== 'string' ) throw new Error('childName argument must be a string');
this.deleteChildIn([childName]);
};
DataTree.prototype.deleteChildIn = function deleteChildIn(keys/*: Array<string>*/)/*: void*/ {
if (!Array.isArray(keys)) throw new Error('keys argument must be an array');
if (keys.length<1) throw new Error('keys array must length of at least 1');
if (keys.length === 1) delete this._children[keys[0]];
var nextKey = keys[0];
if (nextKey in this._children) {
return this._children[nextKey].deleteChildIn(keys.slice(1));
}
};
DataTree.prototype.toList = function toList()/*: DataTreeList*/ {
var result/*: DataTreeList*/ = {};
function go(node/*: DataTree*/, pathSoFar/*: string*/)/*: void*/ {
var data = node.getData();
if (data !== undefined) {
result[pathSoFar] = data;
}
var children = node._children;
for (var key in children) {
var child = children[key];
go(child, pathSoFar + ((pathSoFar==='/')?'':'/') + encodeURIComponent(key));
}
}
go(this, '/');
return result;
};
DataTree.prototype.toJS = function toJS()/*: DataTreeJS*/ {
function go(node/*: DataTree*/)/*: DataTreeJS*/ {
var result/*: DataTreeJS*/ = {
children: {}
};
var data = node.getData();
if (data !== undefined) {
result.data = data;
}
for (var key in node.getChildren()) {
var child = node.getChild(key);
result.children[key] = go(child);
}
return result;
}
return go(this);
};
module.exports = DataTree;