-
Notifications
You must be signed in to change notification settings - Fork 4
/
seamless-immutable-mergers.js
76 lines (61 loc) · 2.34 KB
/
seamless-immutable-mergers.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
"use strict";
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['seamless-immutable'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('seamless-immutable'));
} else {
root.returnExports = factory(root.b);
}
}(this, function (immutable) {
function concatArrayMerger(current, other) {
if (!(current instanceof Array) || !(other instanceof Array)) return;
return current.concat(other);
}
function equalityArrayMerger(current, other) {
if (!(current instanceof Array) || !(other instanceof Array)) return;
if (current.length !== other.length) return;
for (var i = 0; i < current.length; i++) {
if (current[i] !== other[i]) return;
}
return current;
}
function ignoreArrayMerger(current, other) {
if (!(current instanceof Array) || !(other instanceof Array)) return;
return current;
}
function updatingByIdArrayMerger(current, other, config) {
if (!(current instanceof Array) || !(other instanceof Array)) return;
if (current.length === 0) return;
if (other.length === 0) return current;
var identifier = config.mergerObjectIdentifier;
if (current[0] === null || !(typeof current[0] === "object") || !current[0][identifier]) return;
if (other[0] === null || !(typeof other[0] === "object") || !other[0][identifier]) return;
var currentMap = {};
for (var i = 0; i < current.length; i++) {
currentMap[current[i][identifier]] = i;
}
var resultList = current.asMutable();
for (var j = 0; j < other.length; j++) {
var matchingCurrentIndex = currentMap[other[j][identifier]];
if (matchingCurrentIndex === undefined) {
var modifier = config.modifier;
if (modifier !== 'push' && modifier !== 'unshift')
modifier = 'push';
resultList[modifier](other[j]);
} else {
resultList[matchingCurrentIndex] = resultList[matchingCurrentIndex].merge(other[j], config);
}
}
return immutable(resultList);
}
// Export the library
var immutableMergers = {
concatArrayMerger: concatArrayMerger,
equalityArrayMerger: equalityArrayMerger,
ignoreArrayMerger: ignoreArrayMerger,
updatingByIdArrayMerger: updatingByIdArrayMerger
};
Object.freeze(immutableMergers);
return immutableMergers;
}));