@bbn/bbn 1.0.185 → 1.0.187
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bbn.js +31 -42
- package/dist/bbn.js.map +1 -1
- package/dist/fn/object/mutateArray.js +30 -40
- package/package.json +1 -1
|
@@ -1,54 +1,44 @@
|
|
|
1
1
|
import { isArray } from '../type/isArray.js';
|
|
2
2
|
import { hash } from '../string/hash.js';
|
|
3
|
-
import { isSame } from '../type/isSame.js';
|
|
4
3
|
var mutateArray = function (a1, a2) {
|
|
5
4
|
if (!isArray(a1, a2)) {
|
|
6
5
|
throw new TypeError('mutateArray can only be called with arrays');
|
|
7
6
|
}
|
|
8
7
|
var mapA2 = new Map(a2.map(function (item) { return [hash(item), item]; }));
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
a2Pointer++;
|
|
8
|
+
var a1Ordered = [];
|
|
9
|
+
// Build a1Ordered to have the same order and contents as a2
|
|
10
|
+
a2.forEach(function (item) {
|
|
11
|
+
a1Ordered.push(item);
|
|
12
|
+
});
|
|
13
|
+
// Remove items from a1 that are not in a2
|
|
14
|
+
var i = a1.length;
|
|
15
|
+
while (i--) {
|
|
16
|
+
if (!mapA2.has(hash(a1[i]))) {
|
|
17
|
+
a1.splice(i, 1);
|
|
20
18
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
var
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// If there's no corresponding item in a1 for the current a2 item, insert it.
|
|
36
|
-
if (a1[a1Pointer] === undefined && a2Pointer < a2.length) {
|
|
37
|
-
a1.splice(a1Pointer, 0, a2Item);
|
|
38
|
-
a1Pointer++;
|
|
39
|
-
a2Pointer++;
|
|
19
|
+
}
|
|
20
|
+
var _loop_1 = function (j) {
|
|
21
|
+
if (j >= a1.length || hash(a1[j]) !== hash(a1Ordered[j])) {
|
|
22
|
+
// Find the index of the item in a1, if it exists
|
|
23
|
+
var indexInA1 = a1.findIndex(function (item) { return hash(item) === hash(a1Ordered[j]); });
|
|
24
|
+
if (indexInA1 !== -1) {
|
|
25
|
+
// Move the item to the correct position if it already exists in a1
|
|
26
|
+
var itemToMove = a1.splice(indexInA1, 1)[0];
|
|
27
|
+
a1.splice(j, 0, itemToMove);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// Insert the new item from a2 into a1
|
|
31
|
+
a1.splice(j, 0, a1Ordered[j]);
|
|
32
|
+
}
|
|
40
33
|
}
|
|
41
34
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
// If there are any remaining items in a1 that are not in a2, remove them.
|
|
46
|
-
while (a1.length > a2.length) {
|
|
47
|
-
a1.pop();
|
|
35
|
+
// Insert or move items to match the order of a2
|
|
36
|
+
for (var j = 0; j < a1Ordered.length; j++) {
|
|
37
|
+
_loop_1(j);
|
|
48
38
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
// If a1 has extra items at the end (not present in a2), remove them
|
|
40
|
+
if (a1.length > a1Ordered.length) {
|
|
41
|
+
a1.splice(a1Ordered.length, a1.length - a1Ordered.length);
|
|
52
42
|
}
|
|
53
43
|
return a1;
|
|
54
44
|
};
|