Skip to content

Commit 3dc0344

Browse files
author
Simon he
committed
feature: deepCompare add ignoreKeys
1 parent 11c3cb3 commit 3dc0344

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## [docs](https://www.hejian.club/posts/toolsfunction)
22

33
## 工具函数
4-
- deepCompare // 比较2个对象的差异返回不同的属性和具体不同的值
5-
- deepMerge // Object.assign的深度拷贝版本,返回合并后传入的第一个对象
4+
- deepCompare // 比较2个对象的差异返回不同的属性和具体不同的值 params: {obj1: any, obj2: any, ignoreKeys: string[] | RegExp}
5+
- deepMerge // Object.assign的深度拷贝版本,返回合并后传入的第一个对象 params: {target: Record<any, any>, ...sources: Record<any, any>[]} => target
66
- asyncPool // limit:控制异步并发执行的数量,tasks:异步任务数组
77
- quickFind // quickFind(array: any[], key: any),返回一个新的实例,在实例中find方法可以根据key查找对应的项,查找效率O(1),set更新或新增项,delete删除项效率都是O(1)
88
- quickFilter // quickFilter(array: any[], key: string | number | Array<string | number>, value: string | number | RegExp), 快速模糊查找key名字的项,支持正则匹配

src/deepCompare.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { isArray } from './isArray'
12
import { isPlainObject } from './isPlainObject'
3+
import { isReg } from './isReg'
24

35
// 深比较
4-
export function deepCompare(comp1: any, comp2: any, error: string[] = [], errorMsg: string[] = [], name?: string, index?: string) {
6+
export function deepCompare(comp1: any, comp2: any, ignoreKeys?: string[] | RegExp, error: string[] = [], errorMsg: string[] = [], name?: string, index?: string) {
57
if (isPlainObject(comp1) && isPlainObject(comp2)) {
68
const longer = Object.keys(comp1).length >= Object.keys(comp2).length
79
? comp1
810
: comp2
911
for (const key in longer) {
12+
if ((isArray(ignoreKeys) && (ignoreKeys as string[]).includes(key)) || (isReg(ignoreKeys) && (ignoreKeys as RegExp).test(key)))
13+
continue
14+
1015
const value1 = comp1[key]
1116
const value2 = comp2[key]
1217
const _key = name ? `${name}.${key}` : key
13-
deepCompare(value1, value2, error, errorMsg, _key)
18+
deepCompare(value1, value2, ignoreKeys, error, errorMsg, _key)
1419
}
1520
}
1621
else if (Array.isArray(comp1) && Array.isArray(comp2)) {
@@ -20,7 +25,7 @@ export function deepCompare(comp1: any, comp2: any, error: string[] = [], errorM
2025
for (const key in longer) {
2126
const value1 = comp1[key]
2227
const value2 = comp2[key]
23-
deepCompare(value1, value2, error, errorMsg, name, key)
28+
deepCompare(value1, value2, ignoreKeys, error, errorMsg, name, key)
2429
}
2530
}
2631
else if (comp1 !== comp2) {

0 commit comments

Comments
 (0)