@whitesev/utils 1.0.0 → 1.0.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.
@@ -1,48 +1,53 @@
1
- declare class UtilsDictionary<K extends any, V extends any> {
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: K): boolean;
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): V | undefined;
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: K, val: V): void;
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: K): boolean;
32
+ delete(key: string): boolean;
31
33
  /**
32
34
  * 获取某个键的值
33
- * @param key 键
35
+ * @param {string} key 键
36
+ * @returns {any}
34
37
  */
35
- get(key: K): V;
38
+ get(key: string): any;
36
39
  /**
37
40
  * 返回字典中的所有值
41
+ * @returns {any[]}
38
42
  */
39
- values(): V[];
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: UtilsDictionary<K, V>): void;
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<(string | V)[], void, unknown>;
76
+ get entries(): () => Generator<any[], void, unknown>;
78
77
  /**
79
78
  * 是否可遍历
80
79
  */
81
- get [Symbol.iterator](): () => Generator<(string | V)[], void, unknown>;
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 };
@@ -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: typeof UtilsDictionary;
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.0",
3
+ "version": "1.0.1",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
@@ -1,21 +1,19 @@
1
- import { Utils } from "./Utils";
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: K) {
12
- return this.items.hasOwnProperty(key as any);
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: string) {
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: string) {
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.getItems()[keyName];
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: K, val: V) {
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 as any] = val;
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: K) {
54
+ delete(key) {
55
55
  if (this.has(key)) {
56
- Reflect.deleteProperty(this.items, key as any);
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: K) {
66
- return this.getItems()[key as any];
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 as K)) {
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 = void 0 as any;
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: UtilsDictionary<K, V>) {
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 as K)];
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 = UtilsDictionary;
445
+ Dictionary: {
446
+ new <K, V>(): UtilsDictionaryConstructor<K, V>;
447
+ } = UtilsDictionary as any;
446
448
  /**
447
449
  * 主动触发事件
448
450
  * @param element 元素
@@ -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
+ }