|
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) |
16 | 16 | 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) |
24 | 24 | if (index === undefined) |
25 | | - throw new Error('当前id不存在') |
| 25 | + throw new Error('当前id不存在') |
26 | 26 | 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) |
32 | 32 | 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) |
41 | 41 | 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) { |
51 | 51 | 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 { |
58 | 58 | if (value[this.id] === undefined) |
59 | | - throw new Error('新增的数据必须包含唯一id') |
| 59 | + throw new Error('新增的数据必须包含唯一id') |
60 | 60 | 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