Skip to content

Commit ef3b695

Browse files
author
Simon he
committed
✨ feature: quickFind api add set and remove
1 parent 3914faf commit ef3b695

File tree

3 files changed

+123
-8
lines changed

3 files changed

+123
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- deepCompare // 比较2个对象的差异返回不同的属性和具体不同的值
33
- deepMerge // Object.assign的深度拷贝版本,返回合并后传入的第一个对象
44
- asyncPool // limit:控制异步并发执行的数量,tasks:异步任务数组
5-
- quickFind // quickFind(array: any[], key: any),返回一个新的实例,在实例中find方法可以根据key查找对应的项,查找效率O(1)
5+
- quickFind // quickFind(array: any[], key: any),返回一个新的实例,在实例中find方法可以根据key查找对应的项,查找效率O(1),set更新或新增项,delete删除项
66
- quickFilter // quickFilter(array: any[], key: string | number | Array<string | number>, value: string | number | RegExp), 快速模糊查找key名字的项,支持正则匹配
77

88
## 使用方法

src/quickFind.ts

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,75 @@
1-
export function quickFind(array: any[], key: any) {
1+
export function quickFind(array: any[], id: any) {
22
const indexMap = new Map()
33
array.forEach((item, i) => {
4-
indexMap.set(item[key], i)
4+
indexMap.set(item[id], i)
55
})
6-
return new QuickFind(array, indexMap)
6+
return new QuickFind(array, indexMap, id)
77
}
88

99
class QuickFind {
10-
constructor(public array: any[], public indexMap: Map<any, number>) {
10+
constructor(public array: any[], public indexMap: Map<any, number>, public id: any) {
1111
this.array = array
1212
this.indexMap = indexMap
13+
this.id = id
1314
}
1415

15-
find(key: any) {
16-
const index = this.indexMap.get(key)
16+
find(id: any) {
17+
const index = this.indexMap.get(id)
1718
if (index === undefined)
1819
return undefined
1920
return this.array[index]
2021
}
22+
23+
_update(id: any, key: any, value: any) {
24+
if (key === undefined) {
25+
const index = this.indexMap.get(id)
26+
if (index === undefined)
27+
throw new Error('当前id不存在')
28+
29+
if (value[this.id] !== id)
30+
throw new Error('不可修改唯一id')
31+
32+
this.array[index] = value
33+
}
34+
else {
35+
const target = this.find(id)
36+
if (target === undefined)
37+
return
38+
target[key] = value
39+
}
40+
return this.array
41+
}
42+
43+
delete(id: any) {
44+
const index = this.indexMap.get(id)
45+
if (index === undefined)
46+
return
47+
this.array.splice(index, 1)
48+
this.indexMap.delete(id)
49+
return this.array
50+
}
51+
52+
set(id: any, key: any, value?: any) {
53+
const index = this.indexMap.get(id)
54+
if (value === undefined) {
55+
if (key === undefined)
56+
return
57+
value = key
58+
key = undefined
59+
}
60+
if (index !== undefined) {
61+
return this._update(id, key, value)
62+
}
63+
else {
64+
if (value[this.id] === undefined)
65+
throw new Error('新增的数据必须包含唯一id')
66+
if (value[this.id] !== id)
67+
throw new Error('新增的数据id必须与当前id一致')
68+
69+
this.indexMap.set(id, this.array.length)
70+
this.array.push(value)
71+
return this.array
72+
}
73+
}
2174
}
2275

test/basic.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,75 @@ describe('Test 4', () => {
164164
id: 2,
165165
},
166166
]
167-
expect(quickFind(arr, 'id').find(2)).toMatchInlineSnapshot(`
167+
const finder = quickFind(arr, 'id')
168+
expect(finder.find(2)).toMatchInlineSnapshot(`
168169
{
169170
"age": 29,
170171
"id": 2,
171172
"name": "simon2",
172173
}
173174
`)
175+
expect(finder.set(2, 'name', 'new value~~')).toMatchInlineSnapshot(`
176+
[
177+
{
178+
"age": 18,
179+
"id": 0,
180+
"name": "simon",
181+
},
182+
{
183+
"age": 19,
184+
"id": 1,
185+
"name": "simon1",
186+
},
187+
{
188+
"age": 29,
189+
"id": 2,
190+
"name": "new value~~",
191+
},
192+
]
193+
`)
194+
expect(finder.set(3, { id: 3, name: 'new value' })).toMatchInlineSnapshot(`
195+
[
196+
{
197+
"age": 18,
198+
"id": 0,
199+
"name": "simon",
200+
},
201+
{
202+
"age": 19,
203+
"id": 1,
204+
"name": "simon1",
205+
},
206+
{
207+
"age": 29,
208+
"id": 2,
209+
"name": "new value~~",
210+
},
211+
{
212+
"id": 3,
213+
"name": "new value",
214+
},
215+
]
216+
`)
217+
expect(finder.delete(3)).toMatchInlineSnapshot(`
218+
[
219+
{
220+
"age": 18,
221+
"id": 0,
222+
"name": "simon",
223+
},
224+
{
225+
"age": 19,
226+
"id": 1,
227+
"name": "simon1",
228+
},
229+
{
230+
"age": 29,
231+
"id": 2,
232+
"name": "new value~~",
233+
},
234+
]
235+
`)
174236
})
175237
})
176238

0 commit comments

Comments
 (0)