-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathArrayReader.js
More file actions
64 lines (52 loc) · 1.96 KB
/
ArrayReader.js
File metadata and controls
64 lines (52 loc) · 1.96 KB
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
import {isNullOrEmpty} from '../common';
export default function ArrayReader(itemTemplate, containsUniqueItems, uniquePropertyKey, createSourceHash, isOrdered) {
this.itemTemplate = itemTemplate;
this.containsUniqueItems = containsUniqueItems;
this.uniquePropertyKey = uniquePropertyKey;
this.containsPrimitiveValues = isNullOrEmpty(uniquePropertyKey);
this.createSourceHash = createSourceHash;
this.isOrdered = isOrdered;
};
ArrayReader.prototype.read = function (target, source, path, context) {
var result = [], resultHash = {},
hash, sequence, resultSequence = {},
sourceHash = {},
item, itemid,
index, len,
newHashObject,
sequencePath = path + "-seq";
/* validate source array */
if (!source || !Array.isArray(source)) {
source = [];
}
/* hash values for tracking changes */
hash = context.hash.hasOwnProperty(path) ? context.hash[path] : {};
sequence = context.hash.hasOwnProperty(sequencePath) ? context.hash[sequencePath] : {};
for (index = 0, len = source.length; index < len; index += 1) {
item = source[index];
itemid = this.containsUniqueItems ? (this.containsPrimitiveValues ? item : item[this.uniquePropertyKey]) : index;
if (!resultHash.hasOwnProperty(itemid)) {
newHashObject = this.itemTemplate.read(hash.hasOwnProperty(itemid) ? hash[itemid] : {}, item, path + "-" + index, context);
result.push(newHashObject);
resultHash[itemid] = newHashObject;
resultSequence[index] = itemid;
if (this.createSourceHash) {
sourceHash[itemid] = item;
}
if (this.isOrdered) {
if (sequence[index] != resultSequence[index]) {
context.isChanged = true;
}
}
}
}
context.hash[path] = resultHash;
context.hash[sequencePath] = resultSequence;
if (this.createSourceHash) {
context.sourceHash[path] = sourceHash;
}
if (target == null || target.length != result.length) {
context.isChanged = true;
}
return result;
};