@whitesev/utils 2.9.3 → 2.9.5
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 +1423 -1412
- 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 +1423 -1412
- 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 +1423 -1412
- 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 +1423 -1412
- 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 +1423 -1412
- 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 +1423 -1412
- 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/eslint.config.d.mts +2 -0
- package/dist/types/src/ColorConversion.d.ts +15 -14
- package/dist/types/src/Dictionary.d.ts +14 -0
- package/dist/types/src/Utils.d.ts +3 -0
- package/package.json +10 -10
- package/src/ColorConversion.ts +26 -19
- package/src/Dictionary.ts +41 -5
- package/src/Utils.ts +3 -0
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
export declare class ColorConversion {
|
|
2
2
|
/**
|
|
3
3
|
* 判断是否是16进制颜色
|
|
4
|
-
* @param str
|
|
4
|
+
* @param str 十六进制颜色,如`#000000`
|
|
5
5
|
*/
|
|
6
6
|
isHex(str: string): boolean;
|
|
7
7
|
/**
|
|
8
8
|
* 16进制颜色转rgba
|
|
9
9
|
*
|
|
10
10
|
* 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
|
|
11
|
-
* @param hex
|
|
12
|
-
* @param opacity
|
|
11
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
12
|
+
* @param opacity 透明度,0~1
|
|
13
13
|
*/
|
|
14
14
|
hexToRgba(hex: string, opacity: number): string;
|
|
15
15
|
/**
|
|
16
16
|
* hex转rgb
|
|
17
|
-
* @param
|
|
17
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
18
18
|
*/
|
|
19
|
-
hexToRgb(
|
|
19
|
+
hexToRgb(hex: string): RegExpMatchArray;
|
|
20
20
|
/**
|
|
21
21
|
* rgb转hex
|
|
22
|
-
* @param redValue
|
|
23
|
-
* @param greenValue
|
|
24
|
-
* @param blueValue
|
|
22
|
+
* @param redValue 红色值
|
|
23
|
+
* @param greenValue 绿色值
|
|
24
|
+
* @param blueValue 蓝色值
|
|
25
|
+
* @returns hex
|
|
25
26
|
*/
|
|
26
27
|
rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string;
|
|
27
28
|
/**
|
|
28
29
|
* 获取颜色变暗或亮
|
|
29
|
-
* @param color
|
|
30
|
-
* @param level 0~1.0
|
|
30
|
+
* @param color hex颜色,如`#000000`
|
|
31
|
+
* @param level 0~1.0 系数越大,颜色越变暗
|
|
31
32
|
*/
|
|
32
|
-
getDarkColor(color: string, level: string): string;
|
|
33
|
+
getDarkColor(color: string, level: number | string): string;
|
|
33
34
|
/**
|
|
34
35
|
* 获取颜色变亮
|
|
35
|
-
* @param color
|
|
36
|
-
* @param level 0~1.0
|
|
36
|
+
* @param color hex颜色,如`#000000`
|
|
37
|
+
* @param level 0~1.0 系数越大,颜色越变亮
|
|
37
38
|
*/
|
|
38
|
-
getLightColor(color: string, level: number): string;
|
|
39
|
+
getLightColor(color: string, level: number | string): string;
|
|
39
40
|
}
|
|
@@ -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.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "一个常用的工具库",
|
|
7
7
|
"main": "dist/index.cjs.js",
|
|
@@ -39,24 +39,24 @@
|
|
|
39
39
|
"worker-timers": "^8.0.25"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@eslint/js": "^9.
|
|
43
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
42
|
+
"@eslint/js": "^9.38.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
|
-
"@rollup/plugin-typescript": "^12.
|
|
48
|
-
"browserslist": "^4.
|
|
49
|
-
"caniuse-lite": "^1.0.
|
|
50
|
-
"eslint": "^9.
|
|
47
|
+
"@rollup/plugin-typescript": "^12.2.0",
|
|
48
|
+
"browserslist": "^4.27.0",
|
|
49
|
+
"caniuse-lite": "^1.0.30001751",
|
|
50
|
+
"eslint": "^9.38.0",
|
|
51
51
|
"eslint-config-prettier": "^10.1.8",
|
|
52
52
|
"eslint-plugin-compat": "^6.0.2",
|
|
53
53
|
"eslint-plugin-prettier": "^5.5.4",
|
|
54
54
|
"globals": "^16.4.0",
|
|
55
|
-
"rollup": "^4.52.
|
|
55
|
+
"rollup": "^4.52.5",
|
|
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.2"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"lint": "eslint .",
|
package/src/ColorConversion.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export class ColorConversion {
|
|
2
2
|
/**
|
|
3
3
|
* 判断是否是16进制颜色
|
|
4
|
-
* @param str
|
|
4
|
+
* @param str 十六进制颜色,如`#000000`
|
|
5
5
|
*/
|
|
6
6
|
isHex(str: string): boolean {
|
|
7
7
|
if (typeof str !== "string") {
|
|
@@ -16,8 +16,8 @@ export class ColorConversion {
|
|
|
16
16
|
* 16进制颜色转rgba
|
|
17
17
|
*
|
|
18
18
|
* 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
|
|
19
|
-
* @param hex
|
|
20
|
-
* @param opacity
|
|
19
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
20
|
+
* @param opacity 透明度,0~1
|
|
21
21
|
*/
|
|
22
22
|
hexToRgba(hex: string, opacity: number): string {
|
|
23
23
|
if (!this.isHex(hex)) {
|
|
@@ -31,16 +31,16 @@ export class ColorConversion {
|
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* hex转rgb
|
|
34
|
-
* @param
|
|
34
|
+
* @param hex 十六进制颜色,如`#000000`
|
|
35
35
|
*/
|
|
36
|
-
hexToRgb(
|
|
37
|
-
if (!this.isHex(
|
|
38
|
-
throw new TypeError(`输入错误的hex:${
|
|
36
|
+
hexToRgb(hex: string) {
|
|
37
|
+
if (!this.isHex(hex)) {
|
|
38
|
+
throw new TypeError(`输入错误的hex:${hex}`);
|
|
39
39
|
}
|
|
40
40
|
/* replace替换查找的到的字符串 */
|
|
41
|
-
|
|
41
|
+
hex = hex.replace("#", "");
|
|
42
42
|
/* match得到查询数组 */
|
|
43
|
-
const hxs =
|
|
43
|
+
const hxs = hex.match(/../g)!;
|
|
44
44
|
for (let index = 0; index < 3; index++) {
|
|
45
45
|
const value = parseInt(hxs[index], 16);
|
|
46
46
|
Reflect.set(hxs, index, value);
|
|
@@ -50,9 +50,10 @@ export class ColorConversion {
|
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* rgb转hex
|
|
53
|
-
* @param redValue
|
|
54
|
-
* @param greenValue
|
|
55
|
-
* @param blueValue
|
|
53
|
+
* @param redValue 红色值
|
|
54
|
+
* @param greenValue 绿色值
|
|
55
|
+
* @param blueValue 蓝色值
|
|
56
|
+
* @returns hex
|
|
56
57
|
*/
|
|
57
58
|
rgbToHex(redValue: string | number, greenValue: string | number, blueValue: string | number): string {
|
|
58
59
|
/* 验证输入的rgb值是否合法 */
|
|
@@ -69,17 +70,20 @@ export class ColorConversion {
|
|
|
69
70
|
}
|
|
70
71
|
/**
|
|
71
72
|
* 获取颜色变暗或亮
|
|
72
|
-
* @param color
|
|
73
|
-
* @param level 0~1.0
|
|
73
|
+
* @param color hex颜色,如`#000000`
|
|
74
|
+
* @param level 0~1.0 系数越大,颜色越变暗
|
|
74
75
|
*/
|
|
75
|
-
getDarkColor(color: string, level: string): string {
|
|
76
|
+
getDarkColor(color: string, level: number | string): string {
|
|
76
77
|
if (!this.isHex(color)) {
|
|
77
78
|
throw new TypeError(`输入错误的hex:${color}`);
|
|
78
79
|
}
|
|
80
|
+
if (typeof level !== "number") {
|
|
81
|
+
level = Number(level);
|
|
82
|
+
}
|
|
79
83
|
const rgbc = this.hexToRgb(color);
|
|
80
84
|
for (let index = 0; index < 3; index++) {
|
|
81
85
|
const rgbcItemValue = rgbc[index];
|
|
82
|
-
const value = Math.floor(Number(rgbcItemValue) * (1 -
|
|
86
|
+
const value = Math.floor(Number(rgbcItemValue) * (1 - level));
|
|
83
87
|
Reflect.set(rgbc, index, value);
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -87,13 +91,16 @@ export class ColorConversion {
|
|
|
87
91
|
}
|
|
88
92
|
/**
|
|
89
93
|
* 获取颜色变亮
|
|
90
|
-
* @param color
|
|
91
|
-
* @param level 0~1.0
|
|
94
|
+
* @param color hex颜色,如`#000000`
|
|
95
|
+
* @param level 0~1.0 系数越大,颜色越变亮
|
|
92
96
|
*/
|
|
93
|
-
getLightColor(color: string, level: number): string {
|
|
97
|
+
getLightColor(color: string, level: number | string): string {
|
|
94
98
|
if (!this.isHex(color)) {
|
|
95
99
|
throw new TypeError(`输入错误的hex:${color}`);
|
|
96
100
|
}
|
|
101
|
+
if (typeof level !== "number") {
|
|
102
|
+
level = Number(level);
|
|
103
|
+
}
|
|
97
104
|
const rgbc = this.hexToRgb(color);
|
|
98
105
|
for (let index = 0; index < 3; index++) {
|
|
99
106
|
const rgbcItemValue = Number(rgbc[index]);
|
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
|
}
|