@leafer/list 1.0.0-beta.15 → 1.0.0-beta.17

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,12 +1,13 @@
1
1
  {
2
2
  "name": "@leafer/list",
3
- "version": "1.0.0-beta.15",
3
+ "version": "1.0.0-beta.17",
4
4
  "description": "@leafer/list",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
8
  "types": "types/index.d.ts",
9
9
  "files": [
10
+ "src",
10
11
  "types",
11
12
  "dist"
12
13
  ],
@@ -21,6 +22,6 @@
21
22
  "leaferjs"
22
23
  ],
23
24
  "devDependencies": {
24
- "@leafer/interface": "1.0.0-beta.15"
25
+ "@leafer/interface": "1.0.0-beta.17"
25
26
  }
26
27
  }
@@ -0,0 +1,73 @@
1
+ import { ILeaf, ILeafArrayMap, ILeafLevelList, ILeafListItemCallback, INumberMap } from '@leafer/interface'
2
+
3
+ export class LeafLevelList implements ILeafLevelList {
4
+
5
+ public levelMap: ILeafArrayMap
6
+ public keys: INumberMap
7
+ public levels: number[]
8
+
9
+ public get length(): number { return this._length }
10
+ private _length = 0
11
+
12
+ constructor(item?: ILeaf | ILeaf[]) {
13
+ this.reset()
14
+ if (item) item instanceof Array ? this.pushList(item) : this.push(item)
15
+ }
16
+
17
+ public has(leaf: ILeaf): boolean {
18
+ return this.keys[leaf.innerId] !== undefined
19
+ }
20
+
21
+ public without(leaf: ILeaf): boolean {
22
+ return this.keys[leaf.innerId] === undefined
23
+ }
24
+
25
+ public sort(reverse?: boolean): void {
26
+ const { levels } = this
27
+ if (reverse) {
28
+ levels.sort((a, b) => b - a) // 倒序
29
+ } else {
30
+ levels.sort((a, b) => a - b) // 顺序
31
+ }
32
+ }
33
+
34
+ public pushList(list: ILeaf[]): void {
35
+ list.forEach(leaf => { this.push(leaf) })
36
+ }
37
+
38
+ public push(leaf: ILeaf): void {
39
+ const { keys, levelMap } = this
40
+ if (!keys[leaf.innerId]) {
41
+ keys[leaf.innerId] = 1
42
+ if (!levelMap[leaf.__level]) {
43
+ levelMap[leaf.__level] = [leaf]
44
+ this.levels.push(leaf.__level)
45
+ } else {
46
+ levelMap[leaf.__level].push(leaf)
47
+ }
48
+ this._length++
49
+ }
50
+ }
51
+
52
+ public forEach(itemCallback: ILeafListItemCallback): void {
53
+ let list: ILeaf[]
54
+ this.levels.forEach(level => {
55
+ list = this.levelMap[level]
56
+ for (let i = 0, len = list.length; i < len; i++) {
57
+ itemCallback(list[i])
58
+ }
59
+ })
60
+ }
61
+
62
+ public reset(): void {
63
+ this.levelMap = {}
64
+ this.keys = {}
65
+ this.levels = []
66
+ this._length = 0
67
+ }
68
+
69
+ public destroy(): void {
70
+ this.levelMap = null
71
+ }
72
+
73
+ }
@@ -0,0 +1,93 @@
1
+ import { ILeaf, ILeafList, ILeafListItemCallback, INumberMap } from '@leafer/interface'
2
+
3
+ export class LeafList implements ILeafList {
4
+
5
+ public list: ILeaf[]
6
+ public keys: INumberMap
7
+
8
+ public get length(): number { return this.list.length }
9
+
10
+ constructor(item?: ILeaf | ILeaf[]) {
11
+ this.reset()
12
+ if (item) item instanceof Array ? this.pushList(item) : this.push(item)
13
+ }
14
+
15
+ public has(leaf: ILeaf): boolean {
16
+ return leaf && this.keys[leaf.innerId] !== undefined
17
+ }
18
+
19
+ public indexAt(index: number): ILeaf {
20
+ return this.list[index]
21
+ }
22
+
23
+ public indexOf(leaf: ILeaf): number {
24
+ const index = this.keys[leaf.innerId]
25
+ return index === undefined ? -1 : index
26
+ }
27
+
28
+ public pushList(list: ILeaf[]): void {
29
+ list.forEach(leaf => { this.push(leaf) })
30
+ }
31
+
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 {
44
+ const { list, keys } = this
45
+ if (keys[leaf.innerId] === undefined) {
46
+ list.push(leaf)
47
+ keys[leaf.innerId] = list.length - 1
48
+ }
49
+ }
50
+
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) // 顺序
57
+ }
58
+ }
59
+
60
+ public remove(leaf: ILeaf): void {
61
+ const { list } = this
62
+ let findIndex: number
63
+ for (let i = 0, len = list.length; i < len; i++) {
64
+ if (findIndex !== undefined) {
65
+ this.keys[list[i].innerId] = i - 1 // update rest keys
66
+ } else if (list[i].innerId === leaf.innerId) {
67
+ findIndex = i
68
+ delete this.keys[leaf.innerId]
69
+ }
70
+ }
71
+
72
+ if (findIndex !== undefined) list.splice(findIndex, 1)
73
+ }
74
+
75
+ public forEach(itemCallback: ILeafListItemCallback): void {
76
+ this.list.forEach(itemCallback)
77
+ }
78
+
79
+ public clone(): ILeafList {
80
+ const list = new LeafList()
81
+ this.list.forEach(item => { list.push(item) })
82
+ return list
83
+ }
84
+
85
+ public reset(): void {
86
+ this.list = []
87
+ this.keys = {}
88
+ }
89
+
90
+ public destroy(): void {
91
+ this.list = null
92
+ }
93
+ }