@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.
package/dist/bbn.js CHANGED
@@ -9301,41 +9301,57 @@ __webpack_require__.r(__webpack_exports__);
9301
9301
  /* harmony export */ });
9302
9302
  /* harmony import */ var _type_isArray_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../type/isArray.js */ "./dist/fn/type/isArray.js");
9303
9303
  /* harmony import */ var _string_hash_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../string/hash.js */ "./dist/fn/string/hash.js");
9304
- var __spreadArray = (undefined && undefined.__spreadArray) || function (to, from, pack) {
9305
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
9306
- if (ar || !(i in from)) {
9307
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
9308
- ar[i] = from[i];
9309
- }
9310
- }
9311
- return to.concat(ar || Array.prototype.slice.call(from));
9312
- };
9313
9304
 
9314
9305
 
9315
9306
  var mutateArray = function (a1, a2) {
9316
- if (!(0,_type_isArray_js__WEBPACK_IMPORTED_MODULE_0__.isArray)(a1) || !(0,_type_isArray_js__WEBPACK_IMPORTED_MODULE_0__.isArray)(a2)) {
9307
+ if (!(0,_type_isArray_js__WEBPACK_IMPORTED_MODULE_0__.isArray)(a1, a2)) {
9317
9308
  throw new TypeError('mutateArray can only be called with arrays');
9318
9309
  }
9319
- // Create a map from the second array using the identity function to get the key
9320
9310
  var mapA2 = new Map(a2.map(function (item) { return [(0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(item), item]; }));
9321
- var mapA1 = new Map(a1.map(function (item) { return [(0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(item), item]; }));
9322
- // Result array to build the correct order
9323
- var result = [];
9324
- // Iterate over a2 and build the result array
9325
- for (var _i = 0, a2_1 = a2; _i < a2_1.length; _i++) {
9326
- var item = a2_1[_i];
9327
- var key = (0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(item);
9328
- if (mapA1.has(key)) {
9329
- // If the item is in a1, use the item from a2 to preserve the order
9330
- result.push(mapA2.get(key));
9311
+ var a1Pointer = 0;
9312
+ var a2Pointer = 0;
9313
+ var _loop_1 = function () {
9314
+ var a1Item = a1[a1Pointer];
9315
+ var a2Item = a2[a2Pointer];
9316
+ var a1Key = a1Item ? (0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(a1Item) : undefined;
9317
+ var a2Key = (0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(a2Item);
9318
+ if (a1Key === a2Key) {
9319
+ // The items match, move both pointers.
9320
+ a1Pointer++;
9321
+ a2Pointer++;
9322
+ }
9323
+ else if (mapA2.has(a1Key)) {
9324
+ // The item in a1 exists in a2 but is out of order, so it should be moved.
9325
+ // First, find the correct position to move it to.
9326
+ var correctIndex = a1.findIndex(function (item) { return (0,_string_hash_js__WEBPACK_IMPORTED_MODULE_1__.hash)(item) === a2Key; });
9327
+ var itemToMove = a1.splice(correctIndex, 1)[0];
9328
+ a1.splice(a1Pointer, 0, itemToMove);
9329
+ // Now that the item has been moved to the correct position, move pointers.
9330
+ a1Pointer++;
9331
+ a2Pointer++;
9331
9332
  }
9332
9333
  else {
9333
- // If the item is not in a1, it's a new item to be added
9334
- result.push(item);
9334
+ // The item in a1 does not exist in a2, so it should be removed.
9335
+ a1.splice(a1Pointer, 1);
9335
9336
  }
9337
+ // If there's no corresponding item in a1 for the current a2 item, insert it.
9338
+ if (a1[a1Pointer] === undefined && a2Pointer < a2.length) {
9339
+ a1.splice(a1Pointer, 0, a2Item);
9340
+ a1Pointer++;
9341
+ a2Pointer++;
9342
+ }
9343
+ };
9344
+ while (a2Pointer < a2.length) {
9345
+ _loop_1();
9346
+ }
9347
+ // If there are any remaining items in a1 that are not in a2, remove them.
9348
+ while (a1.length > a2.length) {
9349
+ a1.pop();
9350
+ }
9351
+ if (JSON.stringify(a1) !== JSON.stringify(a2)) {
9352
+ bbn.fn.log(a1, a2);
9353
+ throw new Error('mutateArray failed');
9336
9354
  }
9337
- // Clear a1 and push the ordered results into it
9338
- a1.splice.apply(a1, __spreadArray([0, a1.length], result, false));
9339
9355
  return a1;
9340
9356
  };
9341
9357