Skip to content

Commit 75137ad

Browse files
author
Simon he
committed
chore:update
1 parent a3c6acb commit 75137ad

File tree

2 files changed

+500
-500
lines changed

2 files changed

+500
-500
lines changed

src/quickFind.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
export function quickFind(array: any[], id: string | number) {
2-
const indexMap = new Map()
3-
array.forEach((item, i) => indexMap.set(item[id], i))
4-
return new QuickFind(array, indexMap, id)
5-
}
6-
7-
class QuickFind {
8-
constructor(public array: any[], public indexMap: Map<any, number>, public id: string | number) {
9-
this.array = array
10-
this.indexMap = indexMap
11-
this.id = id
12-
}
13-
14-
find(id: any) {
15-
const index = this.indexMap.get(id)
1+
export function quickFind(array: any[], id: string | number) {
2+
const indexMap = new Map()
3+
array.forEach((item, i) => indexMap.set(item[id], i))
4+
return new QuickFind(array, indexMap, id)
5+
}
6+
7+
class QuickFind {
8+
constructor(public array: any[], public indexMap: Map<any, number>, public id: string | number) {
9+
this.array = array
10+
this.indexMap = indexMap
11+
this.id = id
12+
}
13+
14+
find(id: any) {
15+
const index = this.indexMap.get(id)
1616
if (index === undefined)
17-
return undefined
18-
return this.array[index]
19-
}
20-
21-
_update(id: any, key: any, value: any) {
22-
if (key === undefined) {
23-
const index = this.indexMap.get(id)
17+
return undefined
18+
return this.array[index]
19+
}
20+
21+
_update(id: any, key: any, value: any) {
22+
if (key === undefined) {
23+
const index = this.indexMap.get(id)
2424
if (index === undefined)
25-
throw new Error('当前id不存在')
25+
throw new Error('当前id不存在')
2626
if (value[this.id] !== id)
27-
throw new Error('不可修改唯一id')
28-
this.array[index] = value
29-
}
30-
else {
31-
const target = this.find(id)
27+
throw new Error('不可修改唯一id')
28+
this.array[index] = value
29+
}
30+
else {
31+
const target = this.find(id)
3232
if (target === undefined)
33-
return
34-
target[key] = value
35-
}
36-
return this.array
37-
}
38-
39-
delete(id: any) {
40-
const index = this.indexMap.get(id)
33+
return
34+
target[key] = value
35+
}
36+
return this.array
37+
}
38+
39+
delete(id: any) {
40+
const index = this.indexMap.get(id)
4141
if (index === undefined)
42-
return
43-
this.array.splice(index, 1)
44-
this.indexMap.delete(id)
45-
return this.array
46-
}
47-
48-
set(id: any, key: any, value?: any) {
49-
const index = this.indexMap.get(id)
50-
if (value === undefined) {
42+
return
43+
this.array.splice(index, 1)
44+
this.indexMap.delete(id)
45+
return this.array
46+
}
47+
48+
set(id: any, key: any, value?: any) {
49+
const index = this.indexMap.get(id)
50+
if (value === undefined) {
5151
if (key === undefined)
52-
return
53-
value = key
54-
key = undefined
55-
}
56-
if (index !== undefined) { return this._update(id, key, value) }
57-
else {
52+
return
53+
value = key
54+
key = undefined
55+
}
56+
if (index !== undefined) { return this._update(id, key, value) }
57+
else {
5858
if (value[this.id] === undefined)
59-
throw new Error('新增的数据必须包含唯一id')
59+
throw new Error('新增的数据必须包含唯一id')
6060
if (value[this.id] !== id)
61-
throw new Error('新增的数据id必须与当前id一致')
62-
this.indexMap.set(id, this.array.length)
63-
this.array.push(value)
64-
return this.array
65-
}
66-
}
67-
}
68-
61+
throw new Error('新增的数据id必须与当前id一致')
62+
this.indexMap.set(id, this.array.length)
63+
this.array.push(value)
64+
return this.array
65+
}
66+
}
67+
}
68+

0 commit comments

Comments
 (0)