json-diff-ts 5.0.0-alpha.7 → 5.0.0-alpha.8

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/index.cjs CHANGED
@@ -487,12 +487,14 @@ var compareArray = (oldObj, newObj, path, keyPath, options) => {
487
487
  const isFunctionKey = typeof uniqKey === "function" && uniqKey.length === 2;
488
488
  if (diffs.length) {
489
489
  const resolvedKey = isFunctionKey ? uniqKey(newObj[0] ?? oldObj[0], true) : uniqKey;
490
+ const sampleEl = newObj[0] ?? oldObj[0];
491
+ const isNestedPath = typeof resolvedKey === "string" && resolvedKey.includes(".") && NESTED_PATH_RE.test(resolvedKey) && (isFunctionKey || !(sampleEl != null && typeof sampleEl === "object" && resolvedKey in sampleEl));
490
492
  return [
491
493
  {
492
494
  type: "UPDATE" /* UPDATE */,
493
495
  key: getKey(path),
494
496
  embeddedKey: resolvedKey,
495
- ...isFunctionKey && typeof resolvedKey === "string" && NESTED_PATH_RE.test(resolvedKey) && resolvedKey.includes(".") ? { embeddedKeyIsPath: true } : {},
497
+ ...isNestedPath ? { embeddedKeyIsPath: true } : {},
496
498
  changes: diffs
497
499
  }
498
500
  ];
@@ -533,7 +535,8 @@ var convertArrayToObj = (arr, uniqKey) => {
533
535
  obj[i] = value;
534
536
  }
535
537
  } else {
536
- const keyFunction = typeof uniqKey === "string" ? (item) => item[uniqKey] : uniqKey;
538
+ const maybeNestedPath = typeof uniqKey === "string" && uniqKey.includes(".") && NESTED_PATH_RE.test(uniqKey);
539
+ const keyFunction = typeof uniqKey === "string" ? maybeNestedPath ? (item) => item != null && typeof item === "object" && uniqKey in item ? item[uniqKey] : resolveProperty(item, uniqKey, true) : (item) => item[uniqKey] : uniqKey;
537
540
  obj = keyBy(arr, keyFunction);
538
541
  }
539
542
  return obj;
@@ -580,7 +583,7 @@ var indexOfItemInArray = (arr, key, value, isPath) => {
580
583
  const item = arr[i];
581
584
  if (item == null) continue;
582
585
  const resolved = resolveProperty(item, key, isPath);
583
- if (resolved != null && String(resolved) === String(value)) {
586
+ if (resolved !== void 0 && String(resolved) === String(value)) {
584
587
  return i;
585
588
  }
586
589
  }
@@ -636,7 +639,7 @@ var applyArrayChange = (arr, change) => {
636
639
  } else {
637
640
  element = arr.find((el) => {
638
641
  const resolved = resolveProperty(el, change.embeddedKey, change.embeddedKeyIsPath);
639
- return resolved != null && String(resolved) === String(subchange.key);
642
+ return resolved !== void 0 && String(resolved) === String(subchange.key);
640
643
  });
641
644
  }
642
645
  if (element) {
@@ -706,7 +709,7 @@ var revertArrayChange = (arr, change) => {
706
709
  } else {
707
710
  element = arr.find((el) => {
708
711
  const resolved = resolveProperty(el, change.embeddedKey, change.embeddedKeyIsPath);
709
- return resolved != null && String(resolved) === String(subchange.key);
712
+ return resolved !== void 0 && String(resolved) === String(subchange.key);
710
713
  });
711
714
  }
712
715
  if (element) {
@@ -1361,7 +1364,8 @@ function walkChanges(changes, basePath, oldCtx, newCtx, ops, options) {
1361
1364
  childChange.key,
1362
1365
  childOld,
1363
1366
  childNew,
1364
- childChange
1367
+ childChange,
1368
+ change.embeddedKeyIsPath
1365
1369
  );
1366
1370
  if (childChange.changes) {
1367
1371
  const oldEl = findElement(childOld, change.embeddedKey, childChange.key);
@@ -1404,7 +1408,7 @@ function emitLeafOp(change, path, ops, options) {
1404
1408
  }
1405
1409
  }
1406
1410
  }
1407
- function buildCanonicalFilterPath(basePath, embeddedKey, changeKey, oldArr, newArr, change) {
1411
+ function buildCanonicalFilterPath(basePath, embeddedKey, changeKey, oldArr, newArr, change, embeddedKeyIsPath) {
1408
1412
  if (embeddedKey === "$index") {
1409
1413
  return `${basePath}[${changeKey}]`;
1410
1414
  }
@@ -1424,9 +1428,11 @@ function buildCanonicalFilterPath(basePath, embeddedKey, changeKey, oldArr, newA
1424
1428
  const memberAccess2 = typeof keyName === "string" && SIMPLE_PROPERTY_RE2.test(keyName) ? `.${keyName}` : `.${changeKey}`;
1425
1429
  return `${basePath}[?(@${memberAccess2}=='${changeKey}')]`;
1426
1430
  }
1427
- const element = findElementByKey(oldArr, newArr, embeddedKey, changeKey, change.type);
1428
- const typedVal = element ? element[embeddedKey] : changeKey;
1429
- const memberAccess = SIMPLE_PROPERTY_RE2.test(embeddedKey) ? `.${embeddedKey}` : `['${embeddedKey.replace(/'/g, "''")}']`;
1431
+ const isNestedPath = embeddedKeyIsPath && NESTED_PATH_RE3.test(embeddedKey);
1432
+ const element = findElementByKey(oldArr, newArr, embeddedKey, changeKey, change.type, isNestedPath);
1433
+ const resolved = element !== void 0 ? resolveNestedKey(element, embeddedKey, !!isNestedPath) : void 0;
1434
+ const typedVal = resolved !== void 0 ? resolved : changeKey;
1435
+ const memberAccess = SIMPLE_PROPERTY_RE2.test(embeddedKey) || isNestedPath ? `.${embeddedKey}` : `['${embeddedKey.replace(/'/g, "''")}']`;
1430
1436
  return `${basePath}[?(@${memberAccess}==${formatFilterLiteral(typedVal)})]`;
1431
1437
  }
1432
1438
  function findActualValue(oldArr, newArr, stringKey, opType) {
@@ -1465,13 +1471,20 @@ function findElement(arr, embeddedKey, changeKey) {
1465
1471
  }
1466
1472
  return arr.find((item) => item && String(item[embeddedKey]) === changeKey);
1467
1473
  }
1468
- function findElementByKey(oldArr, newArr, embeddedKey, changeKey, opType) {
1474
+ function resolveNestedKey(item, key, isPath) {
1475
+ if (isPath) {
1476
+ return key.split(".").reduce((c, s) => c?.[s], item);
1477
+ }
1478
+ return item?.[key];
1479
+ }
1480
+ function findElementByKey(oldArr, newArr, embeddedKey, changeKey, opType, isPath) {
1481
+ const match = (item) => item && String(resolveNestedKey(item, embeddedKey, !!isPath)) === changeKey;
1469
1482
  if (opType === "REMOVE" /* REMOVE */ || opType === "UPDATE" /* UPDATE */) {
1470
- const el = oldArr?.find((item) => item && String(item[embeddedKey]) === changeKey);
1483
+ const el = oldArr?.find(match);
1471
1484
  if (el) return el;
1472
1485
  }
1473
1486
  if (opType === "ADD" /* ADD */ || opType === "UPDATE" /* UPDATE */) {
1474
- const el = newArr?.find((item) => item && String(item[embeddedKey]) === changeKey);
1487
+ const el = newArr?.find(match);
1475
1488
  if (el) return el;
1476
1489
  }
1477
1490
  return void 0;