@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.
@@ -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 a1Pointer = 0;
10
- var a2Pointer = 0;
11
- var _loop_1 = function () {
12
- var a1Item = a1[a1Pointer];
13
- var a2Item = a2[a2Pointer];
14
- var a1Key = a1Item ? hash(a1Item) : undefined;
15
- var a2Key = hash(a2Item);
16
- if (a1Key === a2Key) {
17
- // The items match, move both pointers.
18
- a1Pointer++;
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
- else if (mapA2.has(a1Key)) {
22
- // The item in a1 exists in a2 but is out of order, so it should be moved.
23
- // First, find the correct position to move it to.
24
- var correctIndex = a1.findIndex(function (item) { return hash(item) === a2Key; });
25
- var itemToMove = a1.splice(correctIndex, 1)[0];
26
- a1.splice(a1Pointer, 0, itemToMove);
27
- // Now that the item has been moved to the correct position, move pointers.
28
- a1Pointer++;
29
- a2Pointer++;
30
- }
31
- else {
32
- // The item in a1 does not exist in a2, so it should be removed.
33
- a1.splice(a1Pointer, 1);
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
- while (a2Pointer < a2.length) {
43
- _loop_1();
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
- if (!isSame(a1, a2)) {
50
- bbn.fn.log(a1, a2);
51
- throw new Error('mutateArray failed');
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.185",
3
+ "version": "1.0.187",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",