@whitesev/utils 1.0.0 → 1.0.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 +5 -3
- package/dist/index.amd.js +6022 -0
- package/dist/index.amd.js.map +1 -0
- package/dist/index.cjs.js +156 -153
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +156 -153
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +6023 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.system.js +6025 -0
- package/dist/index.system.js.map +1 -0
- package/dist/index.umd.js +156 -153
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Dictionary.d.ts +30 -32
- package/dist/src/Utils.d.ts +3 -2
- package/package.json +2 -1
- package/rollup.config.js +20 -1
- package/src/{Dictionary.ts → Dictionary.js} +37 -38
- package/src/Utils.ts +4 -2
- package/src/ajaxHooker.js +0 -2
- package/src/types/Dictionary.d.ts +52 -0
package/dist/src/Dictionary.d.ts
CHANGED
|
@@ -1,48 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
items: {
|
|
3
|
-
[key: string | number | symbol]: V;
|
|
4
|
-
};
|
|
1
|
+
export class UtilsDictionary {
|
|
2
|
+
items: {};
|
|
5
3
|
/**
|
|
6
4
|
* 检查是否有某一个键
|
|
7
|
-
* @param key 键
|
|
5
|
+
* @param {string} key 键
|
|
6
|
+
* @returns {boolean}
|
|
8
7
|
*/
|
|
9
|
-
has(key:
|
|
8
|
+
has(key: string): boolean;
|
|
10
9
|
/**
|
|
11
10
|
* 检查已有的键中是否以xx开头
|
|
12
|
-
* @param key 需要匹配的键
|
|
11
|
+
* @param {string} key 需要匹配的键
|
|
12
|
+
* @returns {boolean}
|
|
13
13
|
*/
|
|
14
14
|
startsWith(key: string): boolean;
|
|
15
15
|
/**
|
|
16
16
|
* 获取以xx开头的键的值
|
|
17
|
-
* @param key 需要匹配的键
|
|
17
|
+
* @param {string} key 需要匹配的键
|
|
18
|
+
* @returns {any}
|
|
18
19
|
*/
|
|
19
|
-
getStartsWith(key: string):
|
|
20
|
+
getStartsWith(key: string): any;
|
|
20
21
|
/**
|
|
21
22
|
* 为字典添加某一个值
|
|
22
|
-
* @param key 键
|
|
23
|
-
* @param val 值,默认为""
|
|
23
|
+
* @param {string} key 键
|
|
24
|
+
* @param {any} val 值,默认为""
|
|
24
25
|
*/
|
|
25
|
-
set(key:
|
|
26
|
+
set(key: string, val?: any): void;
|
|
26
27
|
/**
|
|
27
28
|
* 删除某一个键
|
|
28
|
-
* @param key 键
|
|
29
|
+
* @param {string} key 键
|
|
30
|
+
* @returns {boolean}
|
|
29
31
|
*/
|
|
30
|
-
delete(key:
|
|
32
|
+
delete(key: string): boolean;
|
|
31
33
|
/**
|
|
32
34
|
* 获取某个键的值
|
|
33
|
-
* @param key 键
|
|
35
|
+
* @param {string} key 键
|
|
36
|
+
* @returns {any}
|
|
34
37
|
*/
|
|
35
|
-
get(key:
|
|
38
|
+
get(key: string): any;
|
|
36
39
|
/**
|
|
37
40
|
* 返回字典中的所有值
|
|
41
|
+
* @returns {any[]}
|
|
38
42
|
*/
|
|
39
|
-
values():
|
|
43
|
+
values(): any[];
|
|
40
44
|
/**
|
|
41
45
|
* 清空字典
|
|
42
46
|
*/
|
|
43
47
|
clear(): void;
|
|
44
48
|
/**
|
|
45
49
|
* 获取字典的长度
|
|
50
|
+
* @returns {number}
|
|
46
51
|
*/
|
|
47
52
|
size(): number;
|
|
48
53
|
/**
|
|
@@ -51,37 +56,30 @@ declare class UtilsDictionary<K extends any, V extends any> {
|
|
|
51
56
|
keys(): string[];
|
|
52
57
|
/**
|
|
53
58
|
* 返回字典本身
|
|
54
|
-
* @returns
|
|
59
|
+
* @returns {object}
|
|
55
60
|
*/
|
|
56
|
-
getItems():
|
|
57
|
-
[key: string]: V;
|
|
58
|
-
[key: number]: V;
|
|
59
|
-
[key: symbol]: V;
|
|
60
|
-
};
|
|
61
|
+
getItems(): object;
|
|
61
62
|
/**
|
|
62
63
|
* 合并另一个字典
|
|
63
|
-
* @param data 需要合并的字典
|
|
64
|
+
* @param {object} data 需要合并的字典
|
|
64
65
|
*/
|
|
65
|
-
concat(data:
|
|
66
|
-
|
|
67
|
-
* 循环字典
|
|
68
|
-
*/
|
|
69
|
-
forEach(callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void): void;
|
|
66
|
+
concat(data: object): void;
|
|
67
|
+
forEach(callbackfn: any): void;
|
|
70
68
|
/**
|
|
71
69
|
* 获取字典的长度,同this.size
|
|
70
|
+
* @returns {number}
|
|
72
71
|
*/
|
|
73
72
|
get length(): number;
|
|
74
73
|
/**
|
|
75
74
|
* 迭代器
|
|
76
75
|
*/
|
|
77
|
-
get entries(): () => Generator<
|
|
76
|
+
get entries(): () => Generator<any[], void, unknown>;
|
|
78
77
|
/**
|
|
79
78
|
* 是否可遍历
|
|
80
79
|
*/
|
|
81
|
-
get [Symbol.iterator](): () => Generator<
|
|
80
|
+
get [Symbol.iterator](): () => Generator<any[], void, unknown>;
|
|
82
81
|
/**
|
|
83
82
|
* .toString()和.toLocaleString()输出的字符串
|
|
84
83
|
*/
|
|
85
84
|
get [Symbol.toStringTag](): string;
|
|
86
85
|
}
|
|
87
|
-
export { UtilsDictionary };
|
package/dist/src/Utils.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ColorConversion } from "./ColorConversion";
|
|
2
|
-
import { UtilsDictionary } from "./Dictionary";
|
|
3
2
|
import { GBKEncoder } from "./GBKEncoder";
|
|
4
3
|
import { UtilsGMCookie } from "./UtilsGMCookie";
|
|
5
4
|
declare class Utils {
|
|
@@ -135,7 +134,9 @@ declare class Utils {
|
|
|
135
134
|
* > true
|
|
136
135
|
* dictionary.concat(dictionary2);
|
|
137
136
|
**/
|
|
138
|
-
Dictionary:
|
|
137
|
+
Dictionary: {
|
|
138
|
+
new <K, V>(): UtilsDictionaryConstructor<K, V>;
|
|
139
|
+
};
|
|
139
140
|
/**
|
|
140
141
|
* 主动触发事件
|
|
141
142
|
* @param element 元素
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whitesev/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "一个常用的工具库",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/node/index.esm.js",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"author": "WhiteSev",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
26
27
|
"@rollup/plugin-commonjs": "^25.0.8",
|
|
27
28
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
28
29
|
"@rollup/plugin-typescript": "^11.1.6",
|
package/rollup.config.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// 允许使用 node 或 umd 包
|
|
2
2
|
const commonjs = require("@rollup/plugin-commonjs");
|
|
3
|
+
// 解析导入的依赖模块路径,以便 Rollup 能够正确找到依赖模块。
|
|
3
4
|
const { nodeResolve } = require("@rollup/plugin-node-resolve");
|
|
4
5
|
// 编译 TS 代码
|
|
5
6
|
const typescript = require("@rollup/plugin-typescript");
|
|
6
7
|
|
|
8
|
+
const moduleName = "Utils";
|
|
7
9
|
module.exports = {
|
|
8
10
|
plugins: [nodeResolve(), commonjs(), typescript()],
|
|
9
11
|
input: "./index.ts", // 源文件入口
|
|
@@ -20,9 +22,26 @@ module.exports = {
|
|
|
20
22
|
},
|
|
21
23
|
{
|
|
22
24
|
file: "dist/index.umd.js",
|
|
23
|
-
name:
|
|
25
|
+
name: moduleName, // 模块名
|
|
24
26
|
format: "umd", // umd 兼容形式的包, 可以直接应用于网页 script
|
|
25
27
|
sourcemap: true,
|
|
26
28
|
},
|
|
29
|
+
{
|
|
30
|
+
file: "dist/index.amd.js",
|
|
31
|
+
format: "amd", // amd 兼容形式的包, 适用于浏览器环境中使用 AMD 加载器加载模块
|
|
32
|
+
sourcemap: true,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
file: "dist/index.iife.js",
|
|
36
|
+
name: moduleName, // 模块名
|
|
37
|
+
format: "iife", // iife 兼容形式的包, 将模块包裹在一个立即执行的函数中。适用于直接在浏览器中使用
|
|
38
|
+
sourcemap: true,
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
file: "dist/index.system.js",
|
|
42
|
+
name: moduleName, // 模块名
|
|
43
|
+
format: "system", // system 兼容形式的包, 可以在浏览器和 Node.js 环境下加载
|
|
44
|
+
sourcemap: true,
|
|
45
|
+
},
|
|
27
46
|
],
|
|
28
47
|
};
|
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class UtilsDictionary<K extends any, V extends any> {
|
|
4
|
-
items: {
|
|
5
|
-
[key: string | number | symbol]: V;
|
|
6
|
-
} = {};
|
|
1
|
+
class UtilsDictionary {
|
|
2
|
+
items = {};
|
|
7
3
|
/**
|
|
8
4
|
* 检查是否有某一个键
|
|
9
|
-
* @param key 键
|
|
5
|
+
* @param {string} key 键
|
|
6
|
+
* @returns {boolean}
|
|
10
7
|
*/
|
|
11
|
-
has(key
|
|
12
|
-
return this.items.hasOwnProperty(key
|
|
8
|
+
has(key) {
|
|
9
|
+
return this.items.hasOwnProperty(key);
|
|
13
10
|
}
|
|
14
11
|
/**
|
|
15
12
|
* 检查已有的键中是否以xx开头
|
|
16
|
-
* @param key 需要匹配的键
|
|
13
|
+
* @param {string} key 需要匹配的键
|
|
14
|
+
* @returns {boolean}
|
|
17
15
|
*/
|
|
18
|
-
startsWith(key
|
|
16
|
+
startsWith(key) {
|
|
19
17
|
let allKeys = this.keys();
|
|
20
18
|
for (const keyName of allKeys) {
|
|
21
19
|
if (keyName.startsWith(key)) {
|
|
@@ -26,52 +24,56 @@ class UtilsDictionary<K extends any, V extends any> {
|
|
|
26
24
|
}
|
|
27
25
|
/**
|
|
28
26
|
* 获取以xx开头的键的值
|
|
29
|
-
* @param key 需要匹配的键
|
|
27
|
+
* @param {string} key 需要匹配的键
|
|
28
|
+
* @returns {any}
|
|
30
29
|
*/
|
|
31
|
-
getStartsWith(key
|
|
30
|
+
getStartsWith(key) {
|
|
32
31
|
let allKeys = this.keys();
|
|
33
32
|
for (const keyName of allKeys) {
|
|
34
33
|
if (keyName.startsWith(key)) {
|
|
35
|
-
return this.
|
|
34
|
+
return this.items[keyName];
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
38
|
/**
|
|
40
39
|
* 为字典添加某一个值
|
|
41
|
-
* @param key 键
|
|
42
|
-
* @param val 值,默认为""
|
|
40
|
+
* @param {string} key 键
|
|
41
|
+
* @param {any} val 值,默认为""
|
|
43
42
|
*/
|
|
44
|
-
set(key
|
|
43
|
+
set(key, val = "") {
|
|
45
44
|
if (key === void 0) {
|
|
46
45
|
throw new Error("Utils.Dictionary().set 参数 key 不能为空");
|
|
47
46
|
}
|
|
48
|
-
this.items[key
|
|
47
|
+
this.items[key] = val;
|
|
49
48
|
}
|
|
50
49
|
/**
|
|
51
50
|
* 删除某一个键
|
|
52
|
-
* @param key 键
|
|
51
|
+
* @param {string} key 键
|
|
52
|
+
* @returns {boolean}
|
|
53
53
|
*/
|
|
54
|
-
delete(key
|
|
54
|
+
delete(key) {
|
|
55
55
|
if (this.has(key)) {
|
|
56
|
-
Reflect.deleteProperty(this.items, key
|
|
56
|
+
Reflect.deleteProperty(this.items, key);
|
|
57
57
|
return true;
|
|
58
58
|
}
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* 获取某个键的值
|
|
63
|
-
* @param key 键
|
|
63
|
+
* @param {string} key 键
|
|
64
|
+
* @returns {any}
|
|
64
65
|
*/
|
|
65
|
-
get(key
|
|
66
|
-
return this.
|
|
66
|
+
get(key) {
|
|
67
|
+
return this.has(key) ? this.items[key] : void 0;
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
69
70
|
* 返回字典中的所有值
|
|
71
|
+
* @returns {any[]}
|
|
70
72
|
*/
|
|
71
73
|
values() {
|
|
72
74
|
let resultList = [];
|
|
73
75
|
for (let prop in this.items) {
|
|
74
|
-
if (this.has(prop
|
|
76
|
+
if (this.has(prop)) {
|
|
75
77
|
resultList.push(this.items[prop]);
|
|
76
78
|
}
|
|
77
79
|
}
|
|
@@ -81,11 +83,12 @@ class UtilsDictionary<K extends any, V extends any> {
|
|
|
81
83
|
* 清空字典
|
|
82
84
|
*/
|
|
83
85
|
clear() {
|
|
84
|
-
this.items =
|
|
86
|
+
this.items = null;
|
|
85
87
|
this.items = {};
|
|
86
88
|
}
|
|
87
89
|
/**
|
|
88
90
|
* 获取字典的长度
|
|
91
|
+
* @returns {number}
|
|
89
92
|
*/
|
|
90
93
|
size() {
|
|
91
94
|
return Object.keys(this.items).length;
|
|
@@ -98,30 +101,26 @@ class UtilsDictionary<K extends any, V extends any> {
|
|
|
98
101
|
}
|
|
99
102
|
/**
|
|
100
103
|
* 返回字典本身
|
|
101
|
-
* @returns
|
|
104
|
+
* @returns {object}
|
|
102
105
|
*/
|
|
103
106
|
getItems() {
|
|
104
107
|
return this.items;
|
|
105
108
|
}
|
|
106
109
|
/**
|
|
107
110
|
* 合并另一个字典
|
|
108
|
-
* @param data 需要合并的字典
|
|
111
|
+
* @param {object} data 需要合并的字典
|
|
109
112
|
*/
|
|
110
|
-
concat(data
|
|
113
|
+
concat(data) {
|
|
111
114
|
this.items = Utils.assign(this.items, data.getItems());
|
|
112
115
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
forEach(
|
|
117
|
-
callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void
|
|
118
|
-
) {
|
|
119
|
-
for (const key in this.getItems()) {
|
|
120
|
-
callbackfn(this.get(key as K), key as K, this.getItems() as any);
|
|
116
|
+
forEach(callbackfn) {
|
|
117
|
+
for (const key in this.items) {
|
|
118
|
+
callbackfn(this.get(key), key, this.items);
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
/**
|
|
124
122
|
* 获取字典的长度,同this.size
|
|
123
|
+
* @returns {number}
|
|
125
124
|
*/
|
|
126
125
|
get length() {
|
|
127
126
|
return this.size();
|
|
@@ -134,7 +133,7 @@ class UtilsDictionary<K extends any, V extends any> {
|
|
|
134
133
|
return function* () {
|
|
135
134
|
let itemKeys = Object.keys(that.getItems());
|
|
136
135
|
for (const keyName of itemKeys) {
|
|
137
|
-
yield [keyName, that.get(keyName
|
|
136
|
+
yield [keyName, that.get(keyName)];
|
|
138
137
|
}
|
|
139
138
|
};
|
|
140
139
|
}
|
package/src/Utils.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ColorConversion } from "./ColorConversion";
|
|
2
|
-
import { UtilsDictionary } from "./Dictionary";
|
|
3
2
|
import { GBKEncoder } from "./GBKEncoder";
|
|
4
3
|
import { UtilsCore } from "./UtilsCore";
|
|
5
4
|
import { UtilsGMCookie } from "./UtilsGMCookie";
|
|
@@ -12,6 +11,7 @@ import { LockFunction } from "./LockFunction";
|
|
|
12
11
|
import { Log } from "./Log";
|
|
13
12
|
import { Progress } from "./Progress";
|
|
14
13
|
import { TryCatch } from "./tryCatch";
|
|
14
|
+
import { UtilsDictionary } from "./Dictionary";
|
|
15
15
|
|
|
16
16
|
class Utils {
|
|
17
17
|
/** 版本号 */
|
|
@@ -442,7 +442,9 @@ class Utils {
|
|
|
442
442
|
* > true
|
|
443
443
|
* dictionary.concat(dictionary2);
|
|
444
444
|
**/
|
|
445
|
-
Dictionary
|
|
445
|
+
Dictionary: {
|
|
446
|
+
new <K, V>(): UtilsDictionaryConstructor<K, V>;
|
|
447
|
+
} = UtilsDictionary as any;
|
|
446
448
|
/**
|
|
447
449
|
* 主动触发事件
|
|
448
450
|
* @param element 元素
|
package/src/ajaxHooker.js
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
declare interface UtilsDictionaryConstructor<K extends any, V extends any> {
|
|
2
|
+
/** 检查是否有某一个键 */
|
|
3
|
+
has(key: K): boolean;
|
|
4
|
+
/** 检查已有的键中是否以xx开头 */
|
|
5
|
+
startsWith(key: K): boolean;
|
|
6
|
+
/** 获取以xx开头的键的值 */
|
|
7
|
+
getStartsWith(key: K): V;
|
|
8
|
+
/** 为字典添加某一个值 */
|
|
9
|
+
set(key: K, val: V): void;
|
|
10
|
+
/** 删除某一个键 */
|
|
11
|
+
delete(key: K): boolean;
|
|
12
|
+
/** 获取某个键的值 */
|
|
13
|
+
get(key: K): V;
|
|
14
|
+
/** 返回字典中的所有值 */
|
|
15
|
+
values(): V[];
|
|
16
|
+
/** 清空字典 */
|
|
17
|
+
clear(): void;
|
|
18
|
+
/** 获取字典的长度 */
|
|
19
|
+
size(): number;
|
|
20
|
+
/** 获取字典所有的键 */
|
|
21
|
+
keys(): K[];
|
|
22
|
+
/** 返回字典本身 */
|
|
23
|
+
getItems(): Record<K, V>;
|
|
24
|
+
/** 合并另一个字典 */
|
|
25
|
+
concat(data: UtilsDictionaryConstructor<K, V>): void;
|
|
26
|
+
/**
|
|
27
|
+
* 迭代器
|
|
28
|
+
*/
|
|
29
|
+
entries(): IterableIterator<[K, V]>;
|
|
30
|
+
/**
|
|
31
|
+
* 循环字典
|
|
32
|
+
*/
|
|
33
|
+
forEach(
|
|
34
|
+
callbackfn: (
|
|
35
|
+
value: V,
|
|
36
|
+
key: K,
|
|
37
|
+
dictionary: UtilsDictionaryConstructor<K, V>
|
|
38
|
+
) => void
|
|
39
|
+
): void;
|
|
40
|
+
/**
|
|
41
|
+
* 可迭代
|
|
42
|
+
*/
|
|
43
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
44
|
+
/**
|
|
45
|
+
* .toString()和.toLocaleString()输出的字符串
|
|
46
|
+
*/
|
|
47
|
+
[Symbol.toStringTag](): string;
|
|
48
|
+
/**
|
|
49
|
+
* 同this.size()
|
|
50
|
+
*/
|
|
51
|
+
get length(): number;
|
|
52
|
+
}
|