@leafer/list 1.0.0-rc.6 → 1.0.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/list",
3
- "version": "1.0.0-rc.6",
3
+ "version": "1.0.0-rc.7",
4
4
  "description": "@leafer/list",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,6 +22,6 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "devDependencies": {
25
- "@leafer/interface": "1.0.0-rc.6"
25
+ "@leafer/interface": "1.0.0-rc.7"
26
26
  }
27
27
  }
@@ -11,7 +11,7 @@ export class LeafLevelList implements ILeafLevelList {
11
11
 
12
12
  constructor(item?: ILeaf | ILeaf[]) {
13
13
  this.reset()
14
- if (item) item instanceof Array ? this.pushList(item) : this.push(item)
14
+ if (item) item instanceof Array ? this.addList(item) : this.add(item)
15
15
  }
16
16
 
17
17
  public has(leaf: ILeaf): boolean {
@@ -31,11 +31,11 @@ export class LeafLevelList implements ILeafLevelList {
31
31
  }
32
32
  }
33
33
 
34
- public pushList(list: ILeaf[]): void {
35
- list.forEach(leaf => { this.push(leaf) })
34
+ public addList(list: ILeaf[]): void {
35
+ list.forEach(leaf => { this.add(leaf) })
36
36
  }
37
37
 
38
- public push(leaf: ILeaf): void {
38
+ public add(leaf: ILeaf): void {
39
39
  const { keys, levelMap } = this
40
40
  if (!keys[leaf.innerId]) {
41
41
  keys[leaf.innerId] = 1
package/src/LeafList.ts CHANGED
@@ -9,7 +9,7 @@ export class LeafList implements ILeafList {
9
9
 
10
10
  constructor(item?: ILeaf | ILeaf[]) {
11
11
  this.reset()
12
- if (item) item instanceof Array ? this.pushList(item) : this.push(item)
12
+ if (item) item instanceof Array ? this.addList(item) : this.add(item)
13
13
  }
14
14
 
15
15
  public has(leaf: ILeaf): boolean {
@@ -25,22 +25,8 @@ export class LeafList implements ILeafList {
25
25
  return index === undefined ? -1 : index
26
26
  }
27
27
 
28
- public pushList(list: ILeaf[]): void {
29
- list.forEach(leaf => { this.push(leaf) })
30
- }
31
28
 
32
- public unshift(leaf: ILeaf): void {
33
- const { keys } = this
34
- if (keys[leaf.innerId] === undefined) {
35
- this.list.unshift(leaf)
36
- Object.keys(keys).forEach(innerId => {
37
- if (keys[innerId] !== undefined) keys[innerId]++
38
- })
39
- keys[leaf.innerId] = 0
40
- }
41
- }
42
-
43
- public push(leaf: ILeaf): void {
29
+ public add(leaf: ILeaf): void {
44
30
  const { list, keys } = this
45
31
  if (keys[leaf.innerId] === undefined) {
46
32
  list.push(leaf)
@@ -48,15 +34,26 @@ export class LeafList implements ILeafList {
48
34
  }
49
35
  }
50
36
 
51
- public sort(reverse?: boolean): void {
52
- const { list } = this
53
- if (reverse) {
54
- list.sort((a, b) => b.__level - a.__level) // 倒序
55
- } else {
56
- list.sort((a, b) => a.__level - b.__level) // 顺序
37
+ public addAt(leaf: ILeaf, index = 0): void {
38
+ const { keys } = this
39
+ if (keys[leaf.innerId] === undefined) {
40
+ const { list } = this
41
+ for (let i = index, len = list.length; i < len; i++) keys[list[i].innerId]++
42
+ if (index === 0) {
43
+ list.unshift(leaf)
44
+ } else {
45
+ if (index > list.length) index = list.length
46
+ list.splice(index, 0, leaf)
47
+ }
48
+ keys[leaf.innerId] = index
57
49
  }
58
50
  }
59
51
 
52
+ public addList(list: ILeaf[]): void {
53
+ for (let i = 0; i < list.length; i++) this.add(list[i])
54
+ }
55
+
56
+
60
57
  public remove(leaf: ILeaf): void {
61
58
  const { list } = this
62
59
  let findIndex: number
@@ -72,22 +69,40 @@ export class LeafList implements ILeafList {
72
69
  if (findIndex !== undefined) list.splice(findIndex, 1)
73
70
  }
74
71
 
72
+
73
+ public sort(reverse?: boolean): void {
74
+ const { list } = this
75
+ if (reverse) {
76
+ list.sort((a, b) => b.__level - a.__level) // 倒序
77
+ } else {
78
+ list.sort((a, b) => a.__level - b.__level) // 顺序
79
+ }
80
+ }
81
+
75
82
  public forEach(itemCallback: ILeafListItemCallback): void {
76
83
  this.list.forEach(itemCallback)
77
84
  }
78
85
 
79
86
  public clone(): ILeafList {
80
87
  const list = new LeafList()
81
- this.list.forEach(item => { list.push(item) })
88
+ list.list = [...this.list]
89
+ list.keys = { ...this.keys }
82
90
  return list
83
91
  }
84
92
 
93
+
94
+ public update(): void {
95
+ this.keys = {}
96
+ const { list, keys } = this
97
+ for (let i = 0, len = list.length; i < len; i++) keys[list[i].innerId] = i
98
+ }
99
+
85
100
  public reset(): void {
86
101
  this.list = []
87
102
  this.keys = {}
88
103
  }
89
104
 
90
105
  public destroy(): void {
91
- this.list = null
106
+ this.reset()
92
107
  }
93
108
  }
package/types/index.d.ts CHANGED
@@ -8,13 +8,14 @@ declare class LeafList implements ILeafList {
8
8
  has(leaf: ILeaf): boolean;
9
9
  indexAt(index: number): ILeaf;
10
10
  indexOf(leaf: ILeaf): number;
11
- pushList(list: ILeaf[]): void;
12
- unshift(leaf: ILeaf): void;
13
- push(leaf: ILeaf): void;
14
- sort(reverse?: boolean): void;
11
+ add(leaf: ILeaf): void;
12
+ addAt(leaf: ILeaf, index?: number): void;
13
+ addList(list: ILeaf[]): void;
15
14
  remove(leaf: ILeaf): void;
15
+ sort(reverse?: boolean): void;
16
16
  forEach(itemCallback: ILeafListItemCallback): void;
17
17
  clone(): ILeafList;
18
+ update(): void;
18
19
  reset(): void;
19
20
  destroy(): void;
20
21
  }
@@ -29,8 +30,8 @@ declare class LeafLevelList implements ILeafLevelList {
29
30
  has(leaf: ILeaf): boolean;
30
31
  without(leaf: ILeaf): boolean;
31
32
  sort(reverse?: boolean): void;
32
- pushList(list: ILeaf[]): void;
33
- push(leaf: ILeaf): void;
33
+ addList(list: ILeaf[]): void;
34
+ add(leaf: ILeaf): void;
34
35
  forEach(itemCallback: ILeafListItemCallback): void;
35
36
  reset(): void;
36
37
  destroy(): void;