@whitesev/utils 2.9.3 → 2.9.4
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/dist/index.amd.js +31 -5
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +31 -5
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +31 -5
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +31 -5
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +31 -5
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +31 -5
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Dictionary.d.ts +14 -0
- package/package.json +5 -5
- package/src/Dictionary.ts +41 -5
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
export declare class UtilsDictionary<K, V> {
|
|
2
2
|
private items;
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* new utils.Dictionary();
|
|
6
|
+
* @example
|
|
7
|
+
* new utils.Dictionary(1, 2);
|
|
8
|
+
* @example
|
|
9
|
+
* new utils.Dictionary([1, 2], [3, 4], [5, 6]);
|
|
10
|
+
* @example
|
|
11
|
+
* new utils.Dictionary({1:2, 3:4, "5":"6"});
|
|
12
|
+
*/
|
|
3
13
|
constructor();
|
|
14
|
+
constructor(dataList: [key: K, value: V][]);
|
|
15
|
+
constructor(data: {
|
|
16
|
+
[key: string | symbol]: V;
|
|
17
|
+
});
|
|
4
18
|
constructor(key: K, value: V);
|
|
5
19
|
/**
|
|
6
20
|
* 获取字典的长度,同this.size
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@whitesev/utils",
|
|
4
|
-
"version": "2.9.
|
|
4
|
+
"version": "2.9.4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "一个常用的工具库",
|
|
7
7
|
"main": "dist/index.cjs.js",
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@eslint/js": "^9.37.0",
|
|
43
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
43
|
+
"@rollup/plugin-commonjs": "^28.0.8",
|
|
44
44
|
"@rollup/plugin-json": "^6.1.0",
|
|
45
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
45
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
46
46
|
"@rollup/plugin-terser": "^0.4.4",
|
|
47
47
|
"@rollup/plugin-typescript": "^12.1.4",
|
|
48
48
|
"browserslist": "^4.26.3",
|
|
49
|
-
"caniuse-lite": "^1.0.
|
|
49
|
+
"caniuse-lite": "^1.0.30001751",
|
|
50
50
|
"eslint": "^9.37.0",
|
|
51
51
|
"eslint-config-prettier": "^10.1.8",
|
|
52
52
|
"eslint-plugin-compat": "^6.0.2",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"rollup-plugin-clear": "^2.0.7",
|
|
57
57
|
"tslib": "^2.8.1",
|
|
58
58
|
"typescript": "^5.9.3",
|
|
59
|
-
"typescript-eslint": "^8.
|
|
59
|
+
"typescript-eslint": "^8.46.1"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"lint": "eslint .",
|
package/src/Dictionary.ts
CHANGED
|
@@ -1,11 +1,47 @@
|
|
|
1
1
|
export class UtilsDictionary<K, V> {
|
|
2
2
|
private items: Map<K, V>;
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* new utils.Dictionary();
|
|
6
|
+
* @example
|
|
7
|
+
* new utils.Dictionary(1, 2);
|
|
8
|
+
* @example
|
|
9
|
+
* new utils.Dictionary([1, 2], [3, 4], [5, 6]);
|
|
10
|
+
* @example
|
|
11
|
+
* new utils.Dictionary({1:2, 3:4, "5":"6"});
|
|
12
|
+
*/
|
|
3
13
|
constructor();
|
|
14
|
+
constructor(dataList: [key: K, value: V][]);
|
|
15
|
+
constructor(data: { [key: string | symbol]: V });
|
|
4
16
|
constructor(key: K, value: V);
|
|
5
|
-
constructor(
|
|
17
|
+
constructor(...args: any[]) {
|
|
6
18
|
this.items = new Map();
|
|
7
|
-
if (
|
|
8
|
-
|
|
19
|
+
if (args.length === 1) {
|
|
20
|
+
// 数组|对象
|
|
21
|
+
const data = args[0];
|
|
22
|
+
if (Array.isArray(data)) {
|
|
23
|
+
// 数组
|
|
24
|
+
// [[1,2], [3,4], ...]
|
|
25
|
+
for (let index = 0; index < data.length; index++) {
|
|
26
|
+
const item = data[index];
|
|
27
|
+
if (Array.isArray(item)) {
|
|
28
|
+
const [key, value] = item;
|
|
29
|
+
this.set(key, value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
} else if (typeof data === "object" && data != null) {
|
|
33
|
+
// 对象
|
|
34
|
+
// {1:2, 3:4}
|
|
35
|
+
for (const key in data) {
|
|
36
|
+
if (Reflect.has(data, key)) {
|
|
37
|
+
this.set(key as K, data[key] as V);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} else if (args.length === 2) {
|
|
42
|
+
// 键、值
|
|
43
|
+
const [key, value] = args;
|
|
44
|
+
this.set(key, value);
|
|
9
45
|
}
|
|
10
46
|
}
|
|
11
47
|
/**
|
|
@@ -20,7 +56,7 @@ export class UtilsDictionary<K, V> {
|
|
|
20
56
|
get entries() {
|
|
21
57
|
const that = this;
|
|
22
58
|
return function* (): IterableIterator<[K, V]> {
|
|
23
|
-
const itemKeys =
|
|
59
|
+
const itemKeys = that.keys();
|
|
24
60
|
for (const keyName of itemKeys) {
|
|
25
61
|
yield [keyName as K, that.get(keyName as K) as V];
|
|
26
62
|
}
|
|
@@ -57,7 +93,7 @@ export class UtilsDictionary<K, V> {
|
|
|
57
93
|
*/
|
|
58
94
|
set(key: K, val: V): void {
|
|
59
95
|
if (key === void 0) {
|
|
60
|
-
throw new Error("Utils.Dictionary().set 参数 key
|
|
96
|
+
throw new Error("Utils.Dictionary().set 参数 key 不能为undefined");
|
|
61
97
|
}
|
|
62
98
|
this.items.set(key, val);
|
|
63
99
|
}
|