@leafer/list 1.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +22 -0
- package/src/LeafLevelList.ts +73 -0
- package/src/LeafList.ts +93 -0
- package/src/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, Chao (Leafer) Wan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @leafer/list
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leafer/list",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "@leafer/list",
|
|
5
|
+
"author": "Chao (Leafer) Wan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"files": ["src"],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/leaferjs/leafer.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/leaferjs/leafer/tree/main/packages/list",
|
|
14
|
+
"bugs": "https://github.com/leaferjs/leafer/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"leafer",
|
|
17
|
+
"leaferjs"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@leafer/interface": "1.0.0-alpha.1"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -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 = undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
package/src/LeafList.ts
ADDED
|
@@ -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 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 = undefined
|
|
92
|
+
}
|
|
93
|
+
}
|
package/src/index.ts
ADDED