@terzogenito/json-utils 1.0.11 → 1.0.12
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/index.js +123 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -520,6 +520,125 @@ function sortBy(array, key, ascending = true) {
|
|
|
520
520
|
}
|
|
521
521
|
}
|
|
522
522
|
|
|
523
|
+
function findKeys(jsonObject, keyName) {
|
|
524
|
+
try {
|
|
525
|
+
if (typeof jsonObject === 'string') {
|
|
526
|
+
jsonObject = JSON.parse(jsonObject);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
530
|
+
return [];
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const results = [];
|
|
534
|
+
const pattern = keyName.includes('*')
|
|
535
|
+
? new RegExp('^' + keyName.replace(/\*/g, '.*') + '$')
|
|
536
|
+
: null;
|
|
537
|
+
|
|
538
|
+
function search(obj, path = '') {
|
|
539
|
+
if (typeof obj !== 'object' || obj === null) return;
|
|
540
|
+
|
|
541
|
+
if (Array.isArray(obj)) {
|
|
542
|
+
obj.forEach((item, index) => {
|
|
543
|
+
search(item, path ? `${path}[${index}]` : `[${index}]`);
|
|
544
|
+
});
|
|
545
|
+
} else {
|
|
546
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
547
|
+
const newPath = path ? `${path}.${key}` : key;
|
|
548
|
+
|
|
549
|
+
const isMatch = pattern ? pattern.test(key) : key === keyName;
|
|
550
|
+
if (isMatch) {
|
|
551
|
+
results.push({ path: newPath, value });
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if (typeof value === 'object' && value !== null) {
|
|
555
|
+
search(value, newPath);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
search(jsonObject);
|
|
562
|
+
return results;
|
|
563
|
+
} catch (error) {
|
|
564
|
+
console.error('Error in findKeys:', error);
|
|
565
|
+
return [];
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function deepEqual(obj1, obj2) {
|
|
570
|
+
if (obj1 === obj2) return true;
|
|
571
|
+
|
|
572
|
+
if (typeof obj1 !== 'object' || obj1 === null ||
|
|
573
|
+
typeof obj2 !== 'object' || obj2 === null) {
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const keys1 = Object.keys(obj1);
|
|
578
|
+
const keys2 = Object.keys(obj2);
|
|
579
|
+
|
|
580
|
+
if (keys1.length !== keys2.length) return false;
|
|
581
|
+
|
|
582
|
+
for (const key of keys1) {
|
|
583
|
+
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function diff(obj1, obj2, path = '') {
|
|
592
|
+
const differences = [];
|
|
593
|
+
|
|
594
|
+
if (typeof obj1 !== 'object' || obj1 === null ||
|
|
595
|
+
typeof obj2 !== 'object' || obj2 === null) {
|
|
596
|
+
if (obj1 !== obj2) {
|
|
597
|
+
differences.push({
|
|
598
|
+
path,
|
|
599
|
+
type: 'value',
|
|
600
|
+
old: obj1,
|
|
601
|
+
new: obj2
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
return differences;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const allKeys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]);
|
|
608
|
+
|
|
609
|
+
for (const key of allKeys) {
|
|
610
|
+
const newPath = path ? `${path}.${key}` : key;
|
|
611
|
+
|
|
612
|
+
if (!(key in obj1)) {
|
|
613
|
+
differences.push({
|
|
614
|
+
path: newPath,
|
|
615
|
+
type: 'added',
|
|
616
|
+
value: obj2[key]
|
|
617
|
+
});
|
|
618
|
+
} else if (!(key in obj2)) {
|
|
619
|
+
differences.push({
|
|
620
|
+
path: newPath,
|
|
621
|
+
type: 'removed',
|
|
622
|
+
value: obj1[key]
|
|
623
|
+
});
|
|
624
|
+
} else if (!deepEqual(obj1[key], obj2[key])) {
|
|
625
|
+
if (typeof obj1[key] === 'object' && obj1[key] !== null &&
|
|
626
|
+
typeof obj2[key] === 'object' && obj2[key] !== null) {
|
|
627
|
+
differences.push(...diff(obj1[key], obj2[key], newPath));
|
|
628
|
+
} else {
|
|
629
|
+
differences.push({
|
|
630
|
+
path: newPath,
|
|
631
|
+
type: 'changed',
|
|
632
|
+
old: obj1[key],
|
|
633
|
+
new: obj2[key]
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
return differences;
|
|
640
|
+
}
|
|
641
|
+
|
|
523
642
|
module.exports = {
|
|
524
643
|
getString,
|
|
525
644
|
getFile,
|
|
@@ -543,5 +662,8 @@ module.exports = {
|
|
|
543
662
|
getPartialWithDefaults,
|
|
544
663
|
excludeAttributes,
|
|
545
664
|
getSize,
|
|
546
|
-
sortBy
|
|
665
|
+
sortBy,
|
|
666
|
+
findKeys,
|
|
667
|
+
deepEqual,
|
|
668
|
+
diff
|
|
547
669
|
};
|