@bbn/bbn 1.0.181 → 1.0.183

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.
@@ -1,38 +1,54 @@
1
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
- if (ar || !(i in from)) {
4
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
- ar[i] = from[i];
6
- }
7
- }
8
- return to.concat(ar || Array.prototype.slice.call(from));
9
- };
10
1
  import { isArray } from '../type/isArray.js';
11
2
  import { hash } from '../string/hash.js';
12
3
  var mutateArray = function (a1, a2) {
13
- if (!isArray(a1) || !isArray(a2)) {
4
+ if (!isArray(a1, a2)) {
14
5
  throw new TypeError('mutateArray can only be called with arrays');
15
6
  }
16
- // Create a map from the second array using the identity function to get the key
17
7
  var mapA2 = new Map(a2.map(function (item) { return [hash(item), item]; }));
18
- var mapA1 = new Map(a1.map(function (item) { return [hash(item), item]; }));
19
- // Result array to build the correct order
20
- var result = [];
21
- // Iterate over a2 and build the result array
22
- for (var _i = 0, a2_1 = a2; _i < a2_1.length; _i++) {
23
- var item = a2_1[_i];
24
- var key = hash(item);
25
- if (mapA1.has(key)) {
26
- // If the item is in a1, use the item from a2 to preserve the order
27
- result.push(mapA2.get(key));
8
+ var a1Pointer = 0;
9
+ var a2Pointer = 0;
10
+ var _loop_1 = function () {
11
+ var a1Item = a1[a1Pointer];
12
+ var a2Item = a2[a2Pointer];
13
+ var a1Key = a1Item ? hash(a1Item) : undefined;
14
+ var a2Key = hash(a2Item);
15
+ if (a1Key === a2Key) {
16
+ // The items match, move both pointers.
17
+ a1Pointer++;
18
+ a2Pointer++;
19
+ }
20
+ else if (mapA2.has(a1Key)) {
21
+ // The item in a1 exists in a2 but is out of order, so it should be moved.
22
+ // First, find the correct position to move it to.
23
+ var correctIndex = a1.findIndex(function (item) { return hash(item) === a2Key; });
24
+ var itemToMove = a1.splice(correctIndex, 1)[0];
25
+ a1.splice(a1Pointer, 0, itemToMove);
26
+ // Now that the item has been moved to the correct position, move pointers.
27
+ a1Pointer++;
28
+ a2Pointer++;
28
29
  }
29
30
  else {
30
- // If the item is not in a1, it's a new item to be added
31
- result.push(item);
31
+ // The item in a1 does not exist in a2, so it should be removed.
32
+ a1.splice(a1Pointer, 1);
32
33
  }
34
+ // If there's no corresponding item in a1 for the current a2 item, insert it.
35
+ if (a1[a1Pointer] === undefined && a2Pointer < a2.length) {
36
+ a1.splice(a1Pointer, 0, a2Item);
37
+ a1Pointer++;
38
+ a2Pointer++;
39
+ }
40
+ };
41
+ while (a2Pointer < a2.length) {
42
+ _loop_1();
43
+ }
44
+ // If there are any remaining items in a1 that are not in a2, remove them.
45
+ while (a1.length > a2.length) {
46
+ a1.pop();
47
+ }
48
+ if (JSON.stringify(a1) !== JSON.stringify(a2)) {
49
+ bbn.fn.log(a1, a2);
50
+ throw new Error('mutateArray failed');
33
51
  }
34
- // Clear a1 and push the ordered results into it
35
- a1.splice.apply(a1, __spreadArray([0, a1.length], result, false));
36
52
  return a1;
37
53
  };
38
54
  export { mutateArray };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.181",
3
+ "version": "1.0.183",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",