|
@@ -387,4 +387,90 @@ export function camelCase(str) {
|
|
|
export function isNumberStr(str) {
|
|
export function isNumberStr(str) {
|
|
|
return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
|
|
return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 深度比较两个值是否相等
|
|
|
|
|
+ * @param {*} a 要比较的值
|
|
|
|
|
+ * @param {*} b 要比较的值
|
|
|
|
|
+ * @returns {boolean} 两个值是否相等
|
|
|
|
|
+ */
|
|
|
|
|
+export function deepEqual(a, b) {
|
|
|
|
|
+ // 如果是基本类型或null,直接比较
|
|
|
|
|
+ if (a === b) return true;
|
|
|
|
|
+
|
|
|
|
|
+ // 如果其中一个不是对象或为null,不相等
|
|
|
|
|
+ if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object') {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理数组
|
|
|
|
|
+ if (Array.isArray(a) && Array.isArray(b)) {
|
|
|
|
|
+ if (a.length !== b.length) return false;
|
|
|
|
|
+ for (let i = 0; i < a.length; i++) {
|
|
|
|
|
+ if (!deepEqual(a[i], b[i])) return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理对象
|
|
|
|
|
+ const keysA = Object.keys(a);
|
|
|
|
|
+ const keysB = Object.keys(b);
|
|
|
|
|
+
|
|
|
|
|
+ if (keysA.length !== keysB.length) return false;
|
|
|
|
|
+
|
|
|
|
|
+ for (const key of keysA) {
|
|
|
|
|
+ if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 找出两个数组之间的差异项(数组项可能是深层对象)
|
|
|
|
|
+ * @param {Array} arr1 第一个数组
|
|
|
|
|
+ * @param {Array} arr2 第二个数组
|
|
|
|
|
+ * @returns {Object} 包含差异项的对象,{ onlyInFirst: [], onlyInSecond: [] }
|
|
|
|
|
+ */
|
|
|
|
|
+export function findArrayDifferences(arr1, arr2) {
|
|
|
|
|
+ // 存储只在第一个数组中存在的项
|
|
|
|
|
+ const onlyInFirst = [];
|
|
|
|
|
+ // 存储只在第二个数组中存在的项
|
|
|
|
|
+ const onlyInSecond = [];
|
|
|
|
|
+
|
|
|
|
|
+ // 检查第一个数组中的项是否在第二个数组中存在
|
|
|
|
|
+ for (const item1 of arr1) {
|
|
|
|
|
+ let found = false;
|
|
|
|
|
+ for (const item2 of arr2) {
|
|
|
|
|
+ if (deepEqual(item1, item2)) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!found) {
|
|
|
|
|
+ onlyInFirst.push(item1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查第二个数组中的项是否在第一个数组中存在
|
|
|
|
|
+ for (const item2 of arr2) {
|
|
|
|
|
+ let found = false;
|
|
|
|
|
+ for (const item1 of arr1) {
|
|
|
|
|
+ if (deepEqual(item1, item2)) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!found) {
|
|
|
|
|
+ onlyInSecond.push(item2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ onlyInFirst: onlyInFirst,
|
|
|
|
|
+ onlyInSecond: onlyInSecond
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|