This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
/
data-model.ts
317 lines (251 loc) · 7.78 KB
/
data-model.ts
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
import { Path } from './path';
import { toIEEE754Hex } from './utils/number';
import { base64Sha1 } from './utils/base64';
/**
* @internal
*/
export class DataModel {
private _children: { [k: string]: DataModel } = {};
private _value: boolean | string | number | any[] | null = null;
private _hash: string | null = null;
constructor(public key: string | null = null,
public parent: DataModel | null = null,
data?: boolean | string | number | { [k: string]: any } | any[]) {
// TODO: There should be a check for circular references somewhere in here
if (typeof data !== 'undefined') {
this.setData(data);
}
}
/**
* Returns the root of this data model
* @returns {DataModel}
*/
get root(): DataModel {
let model: DataModel = this;
while (model.parent) model = model.parent;
return model;
}
/**
* Returns the children for this data model
* @returns {[DataModel,DataModel,DataModel,DataModel,DataModel]}
*/
get children(): DataModel[] {
return Object.getOwnPropertyNames(this._children).map((key: string) => this._children[key]);
}
/**
* Returns the current path where this data model resides
* @returns {Path}
*/
get path(): Path {
const parts: string[] = [];
for (let model = <DataModel>this; model && model.key !== null; model = model.parent) {
parts.unshift(model.key)
}
return new Path(parts);
}
/**
* Sets the data for this data model
* @param data
* @param invalidateAncestorHashes
* @returns {DataModel}
*/
setData(data: boolean | string | number | { [k: string]: any } | any[], invalidateAncestorHashes = true): DataModel {
let removeSelf = false;
// TODO: transform Firebase array-like objects to actual arrays: {"0":"abc", "1":"def"} to ["abc", "def"]
if (data && (typeof data['.value'] !== 'undefined')) {
/*
If a priority has been set at a certain path, its format isn't {"some key": "some value"}
but instead it's {"some key": {".priority": "some priority", ".value": "some value"} }
For now we ignore the priority.
TODO: handle priorities... maybe?
*/
data = data['.value'];
}
this._children = {};
if (data === null) {
removeSelf = true;
this._value = null;
} else if (typeof data === 'boolean' || typeof data === 'string' || typeof data === 'number' || data instanceof Array) {
this._value = data;
} else {
this._value = null;
Object.getOwnPropertyNames(data).forEach((key: string) => this.child(key).setData(data[key], false));
}
// Invalidate the hash for this model
if (this._hash) {
this._hash = null;
}
if (invalidateAncestorHashes) {
// Invalidate the hashes for all this model's ancestors
for (let model = this.parent; model; model = model.parent) {
if (model._hash) {
model._hash = null;
}
}
}
if (removeSelf && this.parent) {
this.parent.removeChild(this.key);
}
return this;
}
child(path: Path | string | string[]): DataModel {
let parts: string[];
if (typeof path === 'string') {
parts = Path.getParts(path);
} else if (path instanceof Path) {
parts = path.parts;
} else {
parts = path;
}
if (parts.length === 0)
return this;
const key = parts.shift();
if (typeof this._children[key] === 'undefined') {
this._children[key] = new DataModel(key, this);
}
const childModel = this._children[key];
if (parts.length === 0)
return childModel;
return childModel.child(parts);
}
removeChild(key: string) {
delete this._children[key];
}
clone({parent = this.parent, keepData = true, shallow = false}: CloneOptions = {}): DataModel {
const clone = new DataModel(this.key, parent);
if (keepData) {
if (this.hasValue()) {
clone._value = this._value;
} else if (!shallow) {
this.forEachChild((key: string, child: DataModel) => {
clone._children[key] = child.clone({parent: clone});
});
}
}
return clone;
}
/**
* Clones a node's branch all the way up to the root, and returns the new root
* @returns {DataModel}
*/
cloneToRoot(): DataModel {
const currentParent = this.parent;
if (!currentParent)
return this;
this.parent = currentParent.clone({shallow: true});
this.parent._children[this.key] = this;
currentParent.forEachChild((key: string, child: DataModel) => {
child.parent = this.parent;
if (key !== this.key) {
this.parent._children[key] = child;
}
});
return this.parent.cloneToRoot();
}
toObject(): object | boolean | string | number | any[] | null {
if (this.hasValue()) {
return this._value;
}
const obj = {};
let hasValidChildren = false;
this.forEachChild((key: string, child: DataModel) => {
const value = child.toObject();
if (value !== null) {
obj[key] = value;
hasValidChildren = true;
}
});
return hasValidChildren ? obj : null;
}
exists(): boolean {
if (this.hasValue()) {
return true;
}
if (this.hasChildren()) {
return Object.getOwnPropertyNames(this._children).some((key: string) => this._children[key].exists());
}
return false;
}
hasChild(key: string): boolean {
const child = this._children[key];
return child && child.exists();
}
hasChildren(): boolean {
return this.numChildren() > 0;
}
hasValue(): boolean {
return this._value !== null;
}
numChildren(): number {
const childKeys = Object.getOwnPropertyNames(this._children);
return childKeys.reduce((acc, val) => this._children[val].exists() ? acc + 1 : acc, 0);
}
forEachChild(fn: (key: string, child: DataModel) => any) {
const childKeys = Object.getOwnPropertyNames(this._children).sort();
for (let i = 0, l = childKeys.length; i < l; i++) {
let child = this._children[childKeys[i]];
if (child.exists()) {
fn(childKeys[i], child);
}
}
}
/**
* Returns true if the two models contain the same data, false otherwise
* @param other
* @returns {any}
*/
isEqual(other: DataModel): boolean {
if (!other)
return false;
if (this.hasValue() || other.hasValue())
return this._value === other._value;
const seenKeys: { [k: string]: true } = {};
const thisChildrenEqual = Object.getOwnPropertyNames(this._children).every((key: string) => {
seenKeys[key] = true;
const thisChild = this._children[key];
return !thisChild.exists() || thisChild.isEqual(other._children[key]);
});
if (!thisChildrenEqual)
return false;
return Object.getOwnPropertyNames(other._children).every((key: string) => {
return seenKeys[key] || !other._children[key].exists();
});
}
get hash(): string {
if (!this._hash) {
this._hash = this.hasChildren() ? this._getObjectHash() : this._getValueHash();
}
return this._hash;
}
private _getValueHash(): string {
if (this._value === null) {
return '';
}
const valueType = typeof this._value;
const str = valueType + ':' + ((valueType === 'number') ? toIEEE754Hex(<number>this._value) : this._value);
return base64Sha1(str);
}
private _getObjectHash(): string {
let str = '';
this.forEachChild((key: string, child: DataModel) => {
const hash = child.hash;
if (hash !== '') {
str += `:${key}:${hash}`;
}
});
return str !== '' ? base64Sha1(str) : '';
}
/**
* Return the hash for any given value
* @param value
* @returns {string}
*/
static getHash(value: any) {
return new DataModel(null, null, value).hash;
}
}
interface CloneOptions {
parent?: DataModel;
keepData?: boolean;
shallow?: boolean;
}