@vope/utils 1.7.0 → 1.7.2
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/README.md +175 -5
- package/dist/each.d.ts +14 -0
- package/dist/each.js +28 -0
- package/dist/flat-tree.d.ts +99 -0
- package/dist/flat-tree.js +191 -0
- package/dist/index.cjs.js +223 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/memory/simple.d.ts +1 -1
- package/dist/memory/simple.js +3 -2
- package/dist/union.d.ts +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,179 @@
|
|
|
1
|
-
|
|
1
|
+
# @vope/utils
|
|
2
|
+
|
|
3
|
+
工具函数库。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @vope/utils
|
|
9
|
+
# 或
|
|
10
|
+
pnpm add @vope/utils
|
|
11
|
+
# 或
|
|
12
|
+
yarn add @vope/utils
|
|
2
13
|
```
|
|
3
|
-
|
|
14
|
+
|
|
15
|
+
## API 文档
|
|
16
|
+
|
|
17
|
+
### 异步控制
|
|
18
|
+
|
|
19
|
+
| 函数名 | 说明 |
|
|
20
|
+
|--------|------|
|
|
21
|
+
| asyncLock | 异步锁 |
|
|
22
|
+
| once | 缓存函数返回值 |
|
|
23
|
+
| wait | 等待 |
|
|
24
|
+
| Subscriber | 订阅 |
|
|
25
|
+
|
|
26
|
+
### 树结构处理
|
|
27
|
+
|
|
28
|
+
| 函数名 | 说明 |
|
|
29
|
+
|--------|------|
|
|
30
|
+
| FlatTree | 扁平树 |
|
|
31
|
+
| forBfsEach | 广度优先搜索 |
|
|
32
|
+
| forDfsEach | 深度优先搜索 |
|
|
33
|
+
|
|
34
|
+
### 数据转换
|
|
35
|
+
|
|
36
|
+
| 函数名 | 说明 |
|
|
37
|
+
|--------|------|
|
|
38
|
+
| parseNum | 解析一个字符串并返回一个数字 |
|
|
39
|
+
| hash8json | 对象数组转 hash |
|
|
40
|
+
| hash8object | 对象转 hash |
|
|
41
|
+
| hash8strings | 字符串数组转 hash |
|
|
42
|
+
|
|
43
|
+
### 数学计算
|
|
44
|
+
|
|
45
|
+
| 函数名 | 说明 |
|
|
46
|
+
|--------|------|
|
|
47
|
+
| calc | CSS 长度值加减混合运算 |
|
|
48
|
+
| calc.getExpMN | 获取表达式方法名 |
|
|
49
|
+
| calc.multiply | CSS 长度值乘法运算 |
|
|
50
|
+
| calc.divide | CSS 长度值除法运算 |
|
|
51
|
+
| calc.formatter | CSS 长度值格式化 |
|
|
52
|
+
| calc.clamp | CSS 长度值取中间值运算 |
|
|
53
|
+
| clamp | 获取中间数 |
|
|
54
|
+
|
|
55
|
+
### 类型判断
|
|
56
|
+
|
|
57
|
+
| 函数名 | 说明 |
|
|
58
|
+
|--------|------|
|
|
59
|
+
| isNull | 检测是否无值 (undefined/null) |
|
|
60
|
+
| isEmpty | 检测是否为空 (undefined/null/'') |
|
|
61
|
+
| isClass | 判断是否为 Class |
|
|
62
|
+
| isKey | 判断是否为有效的对象键 |
|
|
63
|
+
|
|
64
|
+
### 样式处理
|
|
65
|
+
|
|
66
|
+
| 函数名 | 说明 |
|
|
67
|
+
|--------|------|
|
|
68
|
+
| classnames | 拼接多个 class |
|
|
69
|
+
| css | CSS 样式处理 |
|
|
70
|
+
|
|
71
|
+
### 数据脱敏
|
|
72
|
+
|
|
73
|
+
| 函数名 | 说明 |
|
|
74
|
+
|--------|------|
|
|
75
|
+
| desensEncrypt | 脱敏加密 (对明文进行多个片段加密) |
|
|
76
|
+
| desensDecrypt | 脱敏解密 (对密文进行多个片段解密) |
|
|
77
|
+
| desensReplace | 脱敏平替 |
|
|
78
|
+
|
|
79
|
+
### Cron 表达式
|
|
80
|
+
|
|
81
|
+
| 函数名 | 说明 |
|
|
82
|
+
|--------|------|
|
|
83
|
+
| weekDic | 星期字典 |
|
|
84
|
+
| default_max_second | 默认最大秒 |
|
|
85
|
+
| default_min_second | 默认最小秒 |
|
|
86
|
+
| default_max_minute | 默认最大分 |
|
|
87
|
+
| default_min_minute | 默认最小分 |
|
|
88
|
+
| default_max_hour | 默认最大时 |
|
|
89
|
+
| default_min_hour | 默认最小时 |
|
|
90
|
+
| default_max_day | 默认最大日 |
|
|
91
|
+
| default_min_day | 默认最小日 |
|
|
92
|
+
| default_max_month | 默认最大月 |
|
|
93
|
+
| default_min_month | 默认最小月 |
|
|
94
|
+
| default_max_week | 默认最大周 |
|
|
95
|
+
| default_min_week | 默认最小周 |
|
|
96
|
+
| createCronRegexText | 创建 Cron 正则表达式文本 |
|
|
97
|
+
| InvalidUnit | 无效单位 |
|
|
98
|
+
| isInvalidUnit | 是否为无效单位 |
|
|
99
|
+
| DayLastUnit | 月末单位 |
|
|
100
|
+
| isDayLastUnit | 是否为月末单位 |
|
|
101
|
+
| WeekLastUnit | 周最后单位 |
|
|
102
|
+
| isWeekLastUnit | 是否为周最后单位 |
|
|
103
|
+
| WildcardUnit | 通配符单位 |
|
|
104
|
+
| isWildcardUnit | 是否为通配符单位 |
|
|
105
|
+
| RangeUnit | 范围单位 |
|
|
106
|
+
| isRangeUnit | 是否为范围单位 |
|
|
107
|
+
| AggUnit | 聚合单位 |
|
|
108
|
+
| isAggUnit | 是否为聚合单位 |
|
|
109
|
+
| Cron | Cron 表达式 |
|
|
110
|
+
|
|
111
|
+
### 比较操作
|
|
112
|
+
|
|
113
|
+
| 函数名 | 说明 |
|
|
114
|
+
|--------|------|
|
|
115
|
+
| compareVersion | 版本号大小比较 |
|
|
116
|
+
| deepWeakEqual | 深度弱相等比较 |
|
|
117
|
+
|
|
118
|
+
### 数据结构与集合
|
|
119
|
+
|
|
120
|
+
| 函数名 | 说明 |
|
|
121
|
+
|--------|------|
|
|
122
|
+
| DicRollup | 字典操作 |
|
|
123
|
+
| MemoryBasic | 内存基础 |
|
|
124
|
+
| MemoryList | 内存列表 |
|
|
125
|
+
| MemoryPower | 内存幂 |
|
|
126
|
+
| MemorySimple | 内存简单 |
|
|
127
|
+
| FlexTable | 弹性表格 |
|
|
128
|
+
| Union | 联合 |
|
|
129
|
+
|
|
130
|
+
### 图数据库
|
|
131
|
+
|
|
132
|
+
| 函数名 | 说明 |
|
|
133
|
+
|--------|------|
|
|
134
|
+
| Neo4j | 图管理 |
|
|
135
|
+
| Neo4jAhchors | 图布局之二阶贝塞尔曲线边锚点管理 |
|
|
136
|
+
|
|
137
|
+
### URL 与路径
|
|
138
|
+
|
|
139
|
+
| 函数名 | 说明 |
|
|
140
|
+
|--------|------|
|
|
141
|
+
| joinPaths | 拼接路径 |
|
|
142
|
+
| connectURLParams | 连接 URL 参数 |
|
|
143
|
+
| cubeURLParams | 将 URL 查询参数转换成 (复杂) 对象 |
|
|
144
|
+
| flatURLParams | 将 (复杂) 对象转换成 URL 查询参数 |
|
|
145
|
+
| Serializes | 序列化 |
|
|
146
|
+
|
|
147
|
+
### 几何变换
|
|
148
|
+
|
|
149
|
+
| 函数名 | 说明 |
|
|
150
|
+
|--------|------|
|
|
151
|
+
| rotateAngle | 角度旋转 |
|
|
152
|
+
| rotateRad | 弧度旋转 |
|
|
153
|
+
|
|
154
|
+
### 其他工具
|
|
155
|
+
|
|
156
|
+
| 函数名 | 说明 |
|
|
157
|
+
|--------|------|
|
|
158
|
+
| disableEnumerable | 禁用可枚举性 |
|
|
159
|
+
| indexSort | 索引排序 |
|
|
160
|
+
| getEncoding | 获取编码 |
|
|
161
|
+
| inputRegex | 输入正则 |
|
|
162
|
+
| loadImage | 加载图片 |
|
|
163
|
+
| deconPowersSum | 幂和分解 |
|
|
164
|
+
|
|
165
|
+
## 测试
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
npm run test
|
|
4
169
|
```
|
|
5
170
|
|
|
6
|
-
|
|
171
|
+
## 构建
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
npm run build
|
|
7
175
|
```
|
|
8
|
-
|
|
9
|
-
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
ISC
|
package/dist/each.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 广度优先搜索
|
|
3
|
+
* @param list 节点源
|
|
4
|
+
* @param up 节点下钻,返回当前节点的子节点
|
|
5
|
+
* @param callback 节点回调,返回false则停止搜索
|
|
6
|
+
*/
|
|
7
|
+
export declare const forBfsEach: <T>(list: T[] | Set<T>, up: (data: T) => T[] | Set<T> | void, callback: (data: T) => boolean) => void;
|
|
8
|
+
/**
|
|
9
|
+
* 深度优先搜索
|
|
10
|
+
* @param list 节点源
|
|
11
|
+
* @param up 节点下钻,返回当前节点的子节点
|
|
12
|
+
* @param callback 节点回调,返回false则停止搜索
|
|
13
|
+
*/
|
|
14
|
+
export declare const forDfsEach: <T>(list: T[] | Set<T>, up: (data: T) => T[] | Set<T> | void, callback: (data: T) => boolean) => void;
|
package/dist/each.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const forBfsEach = (list, up, callback) => {
|
|
2
|
+
const stack = Array.isArray(list) ? list.slice() : Array.from(list);
|
|
3
|
+
for (const value of stack) {
|
|
4
|
+
if (!callback(value)) break;
|
|
5
|
+
const result = up(value);
|
|
6
|
+
result === void 0 || stack.push(...result);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const reverseMergeIn = (from, to = []) => {
|
|
10
|
+
let i = to.length + (Array.isArray(from) ? from.length : from.size);
|
|
11
|
+
for (const value of from) {
|
|
12
|
+
to[--i] = value;
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
const forDfsEach = (list, up, callback) => {
|
|
17
|
+
const stack = reverseMergeIn(list);
|
|
18
|
+
while (stack.length > 0) {
|
|
19
|
+
const value = stack.pop();
|
|
20
|
+
if (!callback(value)) break;
|
|
21
|
+
const result = up(value);
|
|
22
|
+
result === void 0 || reverseMergeIn(result, stack);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export {
|
|
26
|
+
forBfsEach,
|
|
27
|
+
forDfsEach
|
|
28
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
type Value = keyof any;
|
|
2
|
+
type PValue = Value | null | undefined;
|
|
3
|
+
type Sup<K, T> = [K] extends [never] ? ((data: T) => PValue) : K;
|
|
4
|
+
interface Options<K, SK, T> {
|
|
5
|
+
/**
|
|
6
|
+
* 节点数据唯一标识字段
|
|
7
|
+
*/
|
|
8
|
+
key: K;
|
|
9
|
+
/**
|
|
10
|
+
* 节点数据源
|
|
11
|
+
*/
|
|
12
|
+
origin: T[];
|
|
13
|
+
/**
|
|
14
|
+
* 节点数据与上级节点关系标识
|
|
15
|
+
*/
|
|
16
|
+
sup: Sup<SK, T>;
|
|
17
|
+
}
|
|
18
|
+
export declare namespace FlatTree {
|
|
19
|
+
/**
|
|
20
|
+
* (0) 先入先出的普通模式 (1) 广度优先 (2) 深度优先
|
|
21
|
+
*/
|
|
22
|
+
type Mode = 0 | 1 | 2;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 扁平树
|
|
26
|
+
*/
|
|
27
|
+
export declare class FlatTree<K extends string, T extends {
|
|
28
|
+
[P in K]: Value;
|
|
29
|
+
} & {
|
|
30
|
+
[P in SK]?: PValue;
|
|
31
|
+
}, SK extends string = never> {
|
|
32
|
+
protected key: K;
|
|
33
|
+
protected sup: Sup<SK, T>;
|
|
34
|
+
/**
|
|
35
|
+
* 节点存储
|
|
36
|
+
*/
|
|
37
|
+
protected memory: Map<string | number | symbol, T>;
|
|
38
|
+
/**
|
|
39
|
+
* 顶级节点
|
|
40
|
+
*/
|
|
41
|
+
protected list: Set<string | number | symbol>;
|
|
42
|
+
/**
|
|
43
|
+
* 节点下钻
|
|
44
|
+
*/
|
|
45
|
+
protected downs: Map<string | number | symbol, Set<string | number | symbol>>;
|
|
46
|
+
/**
|
|
47
|
+
* 节点上钻
|
|
48
|
+
*/
|
|
49
|
+
protected ups: Map<string | number | symbol, string | number | symbol>;
|
|
50
|
+
constructor(options: Options<K, SK, T>);
|
|
51
|
+
/**
|
|
52
|
+
* 获取指定节点的上级节点标识
|
|
53
|
+
*/
|
|
54
|
+
protected getPid(data: T): PValue | T[SK];
|
|
55
|
+
/**
|
|
56
|
+
* 添加节点
|
|
57
|
+
*/
|
|
58
|
+
add(data: T): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* 删除节点
|
|
61
|
+
* @description 删除节点时如果存在子孙节点也将被同步删除
|
|
62
|
+
*/
|
|
63
|
+
delete(value: Value): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* 删除指定节点所有的子节点
|
|
66
|
+
*/
|
|
67
|
+
deleteSubs(value: Value): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* 更新上级节点标识
|
|
70
|
+
*/
|
|
71
|
+
updateSup(data: T): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* 获取节点
|
|
74
|
+
*/
|
|
75
|
+
get(value: Value): T | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* 获取上级节点
|
|
78
|
+
*/
|
|
79
|
+
getSup(value: Value): T | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* 是否含有节点
|
|
82
|
+
*/
|
|
83
|
+
has(value: Value): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* 遍历节点
|
|
86
|
+
* @param mode 遍历模式
|
|
87
|
+
*/
|
|
88
|
+
forEach(callback: (data: T) => void, mode?: FlatTree.Mode): void;
|
|
89
|
+
/**
|
|
90
|
+
* 查询节点
|
|
91
|
+
* @param mode 遍历模式
|
|
92
|
+
*/
|
|
93
|
+
some(callback: (data: T) => boolean, mode?: FlatTree.Mode): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* 清除所有节点数据
|
|
96
|
+
*/
|
|
97
|
+
clear(): void;
|
|
98
|
+
}
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { forBfsEach, forDfsEach } from "./each";
|
|
2
|
+
import { isNull } from "./is/null";
|
|
3
|
+
const eachs = {
|
|
4
|
+
1: forBfsEach,
|
|
5
|
+
2: forDfsEach
|
|
6
|
+
};
|
|
7
|
+
class FlatTree {
|
|
8
|
+
key;
|
|
9
|
+
sup;
|
|
10
|
+
/**
|
|
11
|
+
* 节点存储
|
|
12
|
+
*/
|
|
13
|
+
memory = /* @__PURE__ */ new Map();
|
|
14
|
+
/**
|
|
15
|
+
* 顶级节点
|
|
16
|
+
*/
|
|
17
|
+
list = /* @__PURE__ */ new Set();
|
|
18
|
+
/**
|
|
19
|
+
* 节点下钻
|
|
20
|
+
*/
|
|
21
|
+
downs = /* @__PURE__ */ new Map();
|
|
22
|
+
/**
|
|
23
|
+
* 节点上钻
|
|
24
|
+
*/
|
|
25
|
+
ups = /* @__PURE__ */ new Map();
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.key = options.key;
|
|
28
|
+
this.sup = options.sup;
|
|
29
|
+
options.origin.forEach((data) => this.add(data));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 获取指定节点的上级节点标识
|
|
33
|
+
*/
|
|
34
|
+
getPid(data) {
|
|
35
|
+
const { sup } = this;
|
|
36
|
+
return typeof sup === "function" ? sup(data) : data[sup];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 添加节点
|
|
40
|
+
*/
|
|
41
|
+
add(data) {
|
|
42
|
+
const { memory, downs } = this;
|
|
43
|
+
const id = data[this.key];
|
|
44
|
+
if (memory.has(id)) return false;
|
|
45
|
+
const pid = this.getPid(data);
|
|
46
|
+
if (isNull(pid)) {
|
|
47
|
+
this.list.add(id);
|
|
48
|
+
} else {
|
|
49
|
+
const tmp = downs.get(pid);
|
|
50
|
+
tmp ? tmp.add(id) : downs.set(pid, /* @__PURE__ */ new Set([id]));
|
|
51
|
+
this.ups.set(id, pid);
|
|
52
|
+
}
|
|
53
|
+
this.memory.set(id, data);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 删除节点
|
|
58
|
+
* @description 删除节点时如果存在子孙节点也将被同步删除
|
|
59
|
+
*/
|
|
60
|
+
delete(value) {
|
|
61
|
+
const { memory, downs } = this;
|
|
62
|
+
if (!memory.has(value)) return false;
|
|
63
|
+
let isTop = false;
|
|
64
|
+
const stack = [value];
|
|
65
|
+
for (const id of stack) {
|
|
66
|
+
const pid = this.ups.get(id);
|
|
67
|
+
const tmp = downs.get(id);
|
|
68
|
+
if (tmp) {
|
|
69
|
+
downs.delete(id);
|
|
70
|
+
tmp.forEach((value2) => stack.push(value2));
|
|
71
|
+
}
|
|
72
|
+
this.ups.delete(id);
|
|
73
|
+
isNull(pid) ? isTop = true : downs.get(pid)?.delete(id);
|
|
74
|
+
memory.delete(id);
|
|
75
|
+
}
|
|
76
|
+
isTop && this.list.delete(value);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 删除指定节点所有的子节点
|
|
81
|
+
*/
|
|
82
|
+
deleteSubs(value) {
|
|
83
|
+
if (!this.memory.has(value)) return false;
|
|
84
|
+
const { downs } = this;
|
|
85
|
+
const subs = downs.get(value);
|
|
86
|
+
if (!subs) return true;
|
|
87
|
+
for (const id of subs) {
|
|
88
|
+
this.delete(id);
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 更新上级节点标识
|
|
94
|
+
*/
|
|
95
|
+
updateSup(data) {
|
|
96
|
+
const { memory } = this;
|
|
97
|
+
const id = data[this.key];
|
|
98
|
+
if (!memory.has(id)) return false;
|
|
99
|
+
const oldPid = this.ups.get(id);
|
|
100
|
+
const newPid = this.getPid(data);
|
|
101
|
+
if (oldPid === newPid || isNull(oldPid) && isNull(newPid)) return true;
|
|
102
|
+
const { list, ups, downs } = this;
|
|
103
|
+
if (isNull(oldPid)) {
|
|
104
|
+
if (!memory.has(newPid)) return false;
|
|
105
|
+
list.delete(id);
|
|
106
|
+
ups.set(id, newPid);
|
|
107
|
+
downs.get(newPid).add(id);
|
|
108
|
+
} else {
|
|
109
|
+
if (isNull(newPid)) {
|
|
110
|
+
list.add(id);
|
|
111
|
+
ups.delete(id);
|
|
112
|
+
} else {
|
|
113
|
+
if (!memory.has(newPid)) return false;
|
|
114
|
+
ups.set(id, newPid);
|
|
115
|
+
const tmp = downs.get(newPid);
|
|
116
|
+
tmp ? tmp.add(id) : downs.set(newPid, /* @__PURE__ */ new Set([id]));
|
|
117
|
+
}
|
|
118
|
+
downs.get(oldPid).delete(id);
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 获取节点
|
|
124
|
+
*/
|
|
125
|
+
get(value) {
|
|
126
|
+
return this.memory.get(value);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 获取上级节点
|
|
130
|
+
*/
|
|
131
|
+
getSup(value) {
|
|
132
|
+
const pid = this.ups.get(value);
|
|
133
|
+
return isNull(pid) ? void 0 : this.memory.get(pid);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 是否含有节点
|
|
137
|
+
*/
|
|
138
|
+
has(value) {
|
|
139
|
+
return this.memory.has(value);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 遍历节点
|
|
143
|
+
* @param mode 遍历模式
|
|
144
|
+
*/
|
|
145
|
+
forEach(callback, mode) {
|
|
146
|
+
if (mode === void 0 || mode === 0) {
|
|
147
|
+
this.memory.forEach(callback);
|
|
148
|
+
} else {
|
|
149
|
+
eachs[mode](
|
|
150
|
+
this.list,
|
|
151
|
+
(value) => this.downs.get(value),
|
|
152
|
+
(value) => (callback(this.memory.get(value)), true)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* 查询节点
|
|
158
|
+
* @param mode 遍历模式
|
|
159
|
+
*/
|
|
160
|
+
some(callback, mode) {
|
|
161
|
+
const { memory } = this;
|
|
162
|
+
let result = false;
|
|
163
|
+
if (mode === void 0 || mode === 0) {
|
|
164
|
+
for (const [, data] of memory) {
|
|
165
|
+
if (callback(data)) {
|
|
166
|
+
result = true;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
eachs[mode](
|
|
172
|
+
this.list,
|
|
173
|
+
(value) => this.downs.get(value),
|
|
174
|
+
(value) => (result = true, callback(memory.get(value)))
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 清除所有节点数据
|
|
181
|
+
*/
|
|
182
|
+
clear() {
|
|
183
|
+
this.memory.clear();
|
|
184
|
+
this.list.clear();
|
|
185
|
+
this.downs.clear();
|
|
186
|
+
this.ups.clear();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export {
|
|
190
|
+
FlatTree
|
|
191
|
+
};
|
package/dist/index.cjs.js
CHANGED
|
@@ -23,6 +23,7 @@ __export(index_exports, {
|
|
|
23
23
|
Cron: () => Cron,
|
|
24
24
|
DayLastUnit: () => DayLastUnit,
|
|
25
25
|
DicRollup: () => dic_default,
|
|
26
|
+
FlatTree: () => FlatTree,
|
|
26
27
|
FlexTable: () => FlexTable,
|
|
27
28
|
InvalidUnit: () => InvalidUnit,
|
|
28
29
|
MemoryBasic: () => MemoryBasic,
|
|
@@ -64,6 +65,8 @@ __export(index_exports, {
|
|
|
64
65
|
desensReplace: () => desensReplace,
|
|
65
66
|
disableEnumerable: () => disable_enumerable_default,
|
|
66
67
|
flatURLParams: () => flatURLParams,
|
|
68
|
+
forBfsEach: () => forBfsEach,
|
|
69
|
+
forDfsEach: () => forDfsEach,
|
|
67
70
|
getEncoding: () => getEncoding,
|
|
68
71
|
hash8json: () => hash8json,
|
|
69
72
|
hash8object: () => hash8object,
|
|
@@ -1167,6 +1170,32 @@ var disable_enumerable_default = (data, key) => {
|
|
|
1167
1170
|
});
|
|
1168
1171
|
};
|
|
1169
1172
|
|
|
1173
|
+
// src/each.ts
|
|
1174
|
+
var forBfsEach = (list, up, callback) => {
|
|
1175
|
+
const stack = Array.isArray(list) ? list.slice() : Array.from(list);
|
|
1176
|
+
for (const value of stack) {
|
|
1177
|
+
if (!callback(value)) break;
|
|
1178
|
+
const result = up(value);
|
|
1179
|
+
result === void 0 || stack.push(...result);
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
var reverseMergeIn = (from, to = []) => {
|
|
1183
|
+
let i = to.length + (Array.isArray(from) ? from.length : from.size);
|
|
1184
|
+
for (const value of from) {
|
|
1185
|
+
to[--i] = value;
|
|
1186
|
+
}
|
|
1187
|
+
return to;
|
|
1188
|
+
};
|
|
1189
|
+
var forDfsEach = (list, up, callback) => {
|
|
1190
|
+
const stack = reverseMergeIn(list);
|
|
1191
|
+
while (stack.length > 0) {
|
|
1192
|
+
const value = stack.pop();
|
|
1193
|
+
if (!callback(value)) break;
|
|
1194
|
+
const result = up(value);
|
|
1195
|
+
result === void 0 || reverseMergeIn(result, stack);
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
|
|
1170
1199
|
// src/encoding.ts
|
|
1171
1200
|
var utf8_first_prefix = /^1{2,}/g;
|
|
1172
1201
|
var utf8_other_prefix = /^10/;
|
|
@@ -1222,6 +1251,197 @@ var getEncoding = (uint, limit = 1024) => {
|
|
|
1222
1251
|
return { encoding, offset };
|
|
1223
1252
|
};
|
|
1224
1253
|
|
|
1254
|
+
// src/is/null.ts
|
|
1255
|
+
var isNull = (value) => value === void 0 || value === null;
|
|
1256
|
+
|
|
1257
|
+
// src/flat-tree.ts
|
|
1258
|
+
var eachs = {
|
|
1259
|
+
1: forBfsEach,
|
|
1260
|
+
2: forDfsEach
|
|
1261
|
+
};
|
|
1262
|
+
var FlatTree = class {
|
|
1263
|
+
key;
|
|
1264
|
+
sup;
|
|
1265
|
+
/**
|
|
1266
|
+
* 节点存储
|
|
1267
|
+
*/
|
|
1268
|
+
memory = /* @__PURE__ */ new Map();
|
|
1269
|
+
/**
|
|
1270
|
+
* 顶级节点
|
|
1271
|
+
*/
|
|
1272
|
+
list = /* @__PURE__ */ new Set();
|
|
1273
|
+
/**
|
|
1274
|
+
* 节点下钻
|
|
1275
|
+
*/
|
|
1276
|
+
downs = /* @__PURE__ */ new Map();
|
|
1277
|
+
/**
|
|
1278
|
+
* 节点上钻
|
|
1279
|
+
*/
|
|
1280
|
+
ups = /* @__PURE__ */ new Map();
|
|
1281
|
+
constructor(options) {
|
|
1282
|
+
this.key = options.key;
|
|
1283
|
+
this.sup = options.sup;
|
|
1284
|
+
options.origin.forEach((data) => this.add(data));
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* 获取指定节点的上级节点标识
|
|
1288
|
+
*/
|
|
1289
|
+
getPid(data) {
|
|
1290
|
+
const { sup } = this;
|
|
1291
|
+
return typeof sup === "function" ? sup(data) : data[sup];
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* 添加节点
|
|
1295
|
+
*/
|
|
1296
|
+
add(data) {
|
|
1297
|
+
const { memory, downs } = this;
|
|
1298
|
+
const id = data[this.key];
|
|
1299
|
+
if (memory.has(id)) return false;
|
|
1300
|
+
const pid = this.getPid(data);
|
|
1301
|
+
if (isNull(pid)) {
|
|
1302
|
+
this.list.add(id);
|
|
1303
|
+
} else {
|
|
1304
|
+
const tmp = downs.get(pid);
|
|
1305
|
+
tmp ? tmp.add(id) : downs.set(pid, /* @__PURE__ */ new Set([id]));
|
|
1306
|
+
this.ups.set(id, pid);
|
|
1307
|
+
}
|
|
1308
|
+
this.memory.set(id, data);
|
|
1309
|
+
return true;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* 删除节点
|
|
1313
|
+
* @description 删除节点时如果存在子孙节点也将被同步删除
|
|
1314
|
+
*/
|
|
1315
|
+
delete(value) {
|
|
1316
|
+
const { memory, downs } = this;
|
|
1317
|
+
if (!memory.has(value)) return false;
|
|
1318
|
+
let isTop = false;
|
|
1319
|
+
const stack = [value];
|
|
1320
|
+
for (const id of stack) {
|
|
1321
|
+
const pid = this.ups.get(id);
|
|
1322
|
+
const tmp = downs.get(id);
|
|
1323
|
+
if (tmp) {
|
|
1324
|
+
downs.delete(id);
|
|
1325
|
+
tmp.forEach((value2) => stack.push(value2));
|
|
1326
|
+
}
|
|
1327
|
+
this.ups.delete(id);
|
|
1328
|
+
isNull(pid) ? isTop = true : downs.get(pid)?.delete(id);
|
|
1329
|
+
memory.delete(id);
|
|
1330
|
+
}
|
|
1331
|
+
isTop && this.list.delete(value);
|
|
1332
|
+
return true;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* 删除指定节点所有的子节点
|
|
1336
|
+
*/
|
|
1337
|
+
deleteSubs(value) {
|
|
1338
|
+
if (!this.memory.has(value)) return false;
|
|
1339
|
+
const { downs } = this;
|
|
1340
|
+
const subs = downs.get(value);
|
|
1341
|
+
if (!subs) return true;
|
|
1342
|
+
for (const id of subs) {
|
|
1343
|
+
this.delete(id);
|
|
1344
|
+
}
|
|
1345
|
+
return true;
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* 更新上级节点标识
|
|
1349
|
+
*/
|
|
1350
|
+
updateSup(data) {
|
|
1351
|
+
const { memory } = this;
|
|
1352
|
+
const id = data[this.key];
|
|
1353
|
+
if (!memory.has(id)) return false;
|
|
1354
|
+
const oldPid = this.ups.get(id);
|
|
1355
|
+
const newPid = this.getPid(data);
|
|
1356
|
+
if (oldPid === newPid || isNull(oldPid) && isNull(newPid)) return true;
|
|
1357
|
+
const { list, ups, downs } = this;
|
|
1358
|
+
if (isNull(oldPid)) {
|
|
1359
|
+
if (!memory.has(newPid)) return false;
|
|
1360
|
+
list.delete(id);
|
|
1361
|
+
ups.set(id, newPid);
|
|
1362
|
+
downs.get(newPid).add(id);
|
|
1363
|
+
} else {
|
|
1364
|
+
if (isNull(newPid)) {
|
|
1365
|
+
list.add(id);
|
|
1366
|
+
ups.delete(id);
|
|
1367
|
+
} else {
|
|
1368
|
+
if (!memory.has(newPid)) return false;
|
|
1369
|
+
ups.set(id, newPid);
|
|
1370
|
+
const tmp = downs.get(newPid);
|
|
1371
|
+
tmp ? tmp.add(id) : downs.set(newPid, /* @__PURE__ */ new Set([id]));
|
|
1372
|
+
}
|
|
1373
|
+
downs.get(oldPid).delete(id);
|
|
1374
|
+
}
|
|
1375
|
+
return true;
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* 获取节点
|
|
1379
|
+
*/
|
|
1380
|
+
get(value) {
|
|
1381
|
+
return this.memory.get(value);
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* 获取上级节点
|
|
1385
|
+
*/
|
|
1386
|
+
getSup(value) {
|
|
1387
|
+
const pid = this.ups.get(value);
|
|
1388
|
+
return isNull(pid) ? void 0 : this.memory.get(pid);
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* 是否含有节点
|
|
1392
|
+
*/
|
|
1393
|
+
has(value) {
|
|
1394
|
+
return this.memory.has(value);
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* 遍历节点
|
|
1398
|
+
* @param mode 遍历模式
|
|
1399
|
+
*/
|
|
1400
|
+
forEach(callback, mode) {
|
|
1401
|
+
if (mode === void 0 || mode === 0) {
|
|
1402
|
+
this.memory.forEach(callback);
|
|
1403
|
+
} else {
|
|
1404
|
+
eachs[mode](
|
|
1405
|
+
this.list,
|
|
1406
|
+
(value) => this.downs.get(value),
|
|
1407
|
+
(value) => (callback(this.memory.get(value)), true)
|
|
1408
|
+
);
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* 查询节点
|
|
1413
|
+
* @param mode 遍历模式
|
|
1414
|
+
*/
|
|
1415
|
+
some(callback, mode) {
|
|
1416
|
+
const { memory } = this;
|
|
1417
|
+
let result = false;
|
|
1418
|
+
if (mode === void 0 || mode === 0) {
|
|
1419
|
+
for (const [, data] of memory) {
|
|
1420
|
+
if (callback(data)) {
|
|
1421
|
+
result = true;
|
|
1422
|
+
break;
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
} else {
|
|
1426
|
+
eachs[mode](
|
|
1427
|
+
this.list,
|
|
1428
|
+
(value) => this.downs.get(value),
|
|
1429
|
+
(value) => (result = true, callback(memory.get(value)))
|
|
1430
|
+
);
|
|
1431
|
+
}
|
|
1432
|
+
return result;
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* 清除所有节点数据
|
|
1436
|
+
*/
|
|
1437
|
+
clear() {
|
|
1438
|
+
this.memory.clear();
|
|
1439
|
+
this.list.clear();
|
|
1440
|
+
this.downs.clear();
|
|
1441
|
+
this.ups.clear();
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
|
|
1225
1445
|
// src/index-sort.ts
|
|
1226
1446
|
var index_sort_default = (list, key) => {
|
|
1227
1447
|
const { length } = list;
|
|
@@ -1283,9 +1503,6 @@ var isClass = (value, strict = true) => {
|
|
|
1283
1503
|
return false;
|
|
1284
1504
|
};
|
|
1285
1505
|
|
|
1286
|
-
// src/is/null.ts
|
|
1287
|
-
var isNull = (value) => value === void 0 || value === null;
|
|
1288
|
-
|
|
1289
1506
|
// src/is/empty.ts
|
|
1290
1507
|
var isEmpty = (value) => isNull(value) || value === "";
|
|
1291
1508
|
|
|
@@ -1537,11 +1754,12 @@ var MemorySimple = class {
|
|
|
1537
1754
|
get size() {
|
|
1538
1755
|
return this.#memory.size;
|
|
1539
1756
|
}
|
|
1540
|
-
constructor(key) {
|
|
1757
|
+
constructor(key, emit) {
|
|
1541
1758
|
this.#key = key;
|
|
1542
1759
|
this.#memory = new MemoryBasic(key, {
|
|
1543
1760
|
mount: (index) => index,
|
|
1544
|
-
reflex: (reactor) => [reactor]
|
|
1761
|
+
reflex: (reactor) => [reactor],
|
|
1762
|
+
emit
|
|
1545
1763
|
});
|
|
1546
1764
|
}
|
|
1547
1765
|
#getType(data) {
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,9 @@ export { weekDic, default_max_second, default_min_second, default_max_minute, de
|
|
|
11
11
|
export { type DesensDecrypt, type DesensEncrypt, desensEncrypt, desensDecrypt, desensReplace } from './desens';
|
|
12
12
|
export { type Dic, default as DicRollup } from './dic';
|
|
13
13
|
export { default as disableEnumerable } from './disable-enumerable';
|
|
14
|
+
export { forBfsEach, forDfsEach } from './each';
|
|
14
15
|
export { getEncoding } from './encoding';
|
|
16
|
+
export { FlatTree } from './flat-tree';
|
|
15
17
|
export { default as indexSort } from './index-sort';
|
|
16
18
|
export { inputRegex } from './input-regex';
|
|
17
19
|
export { isClass } from './is/class';
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,9 @@ import { weekDic, default_max_second, default_min_second, default_max_minute, de
|
|
|
11
11
|
import { desensEncrypt, desensDecrypt, desensReplace } from "./desens";
|
|
12
12
|
import { default as default4 } from "./dic";
|
|
13
13
|
import { default as default5 } from "./disable-enumerable";
|
|
14
|
+
import { forBfsEach, forDfsEach } from "./each";
|
|
14
15
|
import { getEncoding } from "./encoding";
|
|
16
|
+
import { FlatTree } from "./flat-tree";
|
|
15
17
|
import { default as default6 } from "./index-sort";
|
|
16
18
|
import { inputRegex } from "./input-regex";
|
|
17
19
|
import { isClass } from "./is/class";
|
|
@@ -43,6 +45,7 @@ export {
|
|
|
43
45
|
Cron,
|
|
44
46
|
DayLastUnit,
|
|
45
47
|
default4 as DicRollup,
|
|
48
|
+
FlatTree,
|
|
46
49
|
FlexTable,
|
|
47
50
|
InvalidUnit,
|
|
48
51
|
MemoryBasic,
|
|
@@ -84,6 +87,8 @@ export {
|
|
|
84
87
|
desensReplace,
|
|
85
88
|
default5 as disableEnumerable,
|
|
86
89
|
flatURLParams,
|
|
90
|
+
forBfsEach,
|
|
91
|
+
forDfsEach,
|
|
87
92
|
getEncoding,
|
|
88
93
|
hash8json,
|
|
89
94
|
hash8object,
|
package/dist/memory/simple.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ type Basic<T, V, Type extends Memory.Type> = Assign<Transform<Omit<MemoryBasic<T
|
|
|
13
13
|
export declare class MemorySimple<T, Type extends Memory.Type> implements Basic<T, number, Type> {
|
|
14
14
|
#private;
|
|
15
15
|
get size(): number;
|
|
16
|
-
constructor(key: Memory.Pitch<T, Type>);
|
|
16
|
+
constructor(key: Memory.Pitch<T, Type>, emit?: Memory.Emit<T>);
|
|
17
17
|
/**
|
|
18
18
|
* 返回位于指定索引处的项
|
|
19
19
|
*/
|
package/dist/memory/simple.js
CHANGED
|
@@ -5,11 +5,12 @@ class MemorySimple {
|
|
|
5
5
|
get size() {
|
|
6
6
|
return this.#memory.size;
|
|
7
7
|
}
|
|
8
|
-
constructor(key) {
|
|
8
|
+
constructor(key, emit) {
|
|
9
9
|
this.#key = key;
|
|
10
10
|
this.#memory = new MemoryBasic(key, {
|
|
11
11
|
mount: (index) => index,
|
|
12
|
-
reflex: (reactor) => [reactor]
|
|
12
|
+
reflex: (reactor) => [reactor],
|
|
13
|
+
emit
|
|
13
14
|
});
|
|
14
15
|
}
|
|
15
16
|
#getType(data) {
|
package/dist/union.d.ts
CHANGED
|
@@ -8,7 +8,9 @@ type Raws<Primary extends string, Status extends string, Origin extends {
|
|
|
8
8
|
}> = {
|
|
9
9
|
[P in Status]: Revise<Origin, Primary, P>;
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
export declare namespace Union {
|
|
12
|
+
type Status<T> = T extends Union<any, any, any, any, any, infer S> ? S : never;
|
|
13
|
+
}
|
|
12
14
|
/**
|
|
13
15
|
* 数据联合(用于需要对加入的数据进行智能提示)
|
|
14
16
|
*/
|
|
@@ -29,6 +31,6 @@ export declare class Union<Primary extends string, Member extends string, Origin
|
|
|
29
31
|
/**
|
|
30
32
|
* 从已有的联合中拷贝数据
|
|
31
33
|
*/
|
|
32
|
-
clone<T extends Union<Primary, string, Origin, Data, Option>, const P extends Exclude<
|
|
34
|
+
clone<T extends Union<Primary, string, Origin, Data, Option>, const P extends Exclude<Union.Status<T> & Member, Status>>(target: T, key: P | P[]): Union<Primary, Member, Origin, Data, Option, Status | P> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | P>>;
|
|
33
35
|
}
|
|
34
36
|
export {};
|