utils-lib-js-lite 1.0.0
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/LICENSE +339 -0
- package/README.en.md +760 -0
- package/README.md +748 -0
- package/dist/bundle/animate.d.ts +25 -0
- package/dist/bundle/array.d.ts +10 -0
- package/dist/bundle/base.d.ts +17 -0
- package/dist/bundle/element.d.ts +6 -0
- package/dist/bundle/event.d.ts +14 -0
- package/dist/bundle/function.d.ts +13 -0
- package/dist/bundle/index.d.ts +61 -0
- package/dist/bundle/index.js +1 -0
- package/dist/bundle/log.d.ts +8 -0
- package/dist/bundle/object.d.ts +36 -0
- package/dist/bundle/static.d.ts +16 -0
- package/dist/bundle/storage.d.ts +9 -0
- package/dist/bundle/types.d.ts +69 -0
- package/dist/cjs/animate.d.ts +25 -0
- package/dist/cjs/array.d.ts +10 -0
- package/dist/cjs/base.d.ts +17 -0
- package/dist/cjs/element.d.ts +6 -0
- package/dist/cjs/event.d.ts +14 -0
- package/dist/cjs/function.d.ts +13 -0
- package/dist/cjs/index.d.ts +61 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/log.d.ts +8 -0
- package/dist/cjs/object.d.ts +36 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/static.d.ts +16 -0
- package/dist/cjs/storage.d.ts +9 -0
- package/dist/cjs/types.d.ts +69 -0
- package/dist/esm/animate.d.ts +25 -0
- package/dist/esm/array.d.ts +10 -0
- package/dist/esm/base.d.ts +17 -0
- package/dist/esm/element.d.ts +6 -0
- package/dist/esm/event.d.ts +14 -0
- package/dist/esm/function.d.ts +13 -0
- package/dist/esm/index.d.ts +61 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/log.d.ts +8 -0
- package/dist/esm/object.d.ts +36 -0
- package/dist/esm/static.d.ts +16 -0
- package/dist/esm/storage.d.ts +9 -0
- package/dist/esm/types.d.ts +69 -0
- package/dist/umd/animate.d.ts +25 -0
- package/dist/umd/array.d.ts +10 -0
- package/dist/umd/base.d.ts +17 -0
- package/dist/umd/element.d.ts +6 -0
- package/dist/umd/event.d.ts +14 -0
- package/dist/umd/function.d.ts +13 -0
- package/dist/umd/index.d.ts +61 -0
- package/dist/umd/index.js +1 -0
- package/dist/umd/log.d.ts +8 -0
- package/dist/umd/object.d.ts +36 -0
- package/dist/umd/static.d.ts +16 -0
- package/dist/umd/storage.d.ts +9 -0
- package/dist/umd/types.d.ts +69 -0
- package/package.json +44 -0
package/README.en.md
ADDED
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
# utils-lib-js-lite
|
|
2
|
+
|
|
3
|
+
## Introduction
|
|
4
|
+
|
|
5
|
+
JavaScript utility functions, packaging some commonly used js functions
|
|
6
|
+
|
|
7
|
+
## Debugging instructions
|
|
8
|
+
|
|
9
|
+
1. pnpm build
|
|
10
|
+
2. pnpm debug (debug source)
|
|
11
|
+
3. Import the tool library in esm, cjs, and window environments
|
|
12
|
+
|
|
13
|
+
## Contribute
|
|
14
|
+
|
|
15
|
+
1. Fork the local warehouse
|
|
16
|
+
2. Star Warehouse
|
|
17
|
+
3. Submit the code
|
|
18
|
+
4. Create a Pull Request
|
|
19
|
+
|
|
20
|
+
## Instructions for use
|
|
21
|
+
|
|
22
|
+
### Install
|
|
23
|
+
|
|
24
|
+
`npm install utils-lib-js-lite`
|
|
25
|
+
or
|
|
26
|
+
`yarn add utils-lib-js-lite`
|
|
27
|
+
or
|
|
28
|
+
`pnpm install utils-lib-js-lite`
|
|
29
|
+
|
|
30
|
+
### Introduce
|
|
31
|
+
|
|
32
|
+
#### ESM
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import { defer, messageCenter, TaskQueue, Request } from "utils-lib-js-lite";
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### CJS
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
const { defer, messageCenter, TaskQueue } = require("utils-lib-js-lite");
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### in your browser
|
|
45
|
+
|
|
46
|
+
```html
|
|
47
|
+
<script src="./node_modules/utils-lib-js-lite/dist/umd/index.js"></script>
|
|
48
|
+
<script>
|
|
49
|
+
console.log(UtilsLib);
|
|
50
|
+
</script>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### TS interface comment
|
|
54
|
+
|
|
55
|
+
With reference to https://gitee.com/DieHunter/utils-lib-js/blob/master/src/types.ts
|
|
56
|
+
|
|
57
|
+
### Code demo
|
|
58
|
+
|
|
59
|
+
#### base module
|
|
60
|
+
|
|
61
|
+
##### 1. `randomNum(min: number, max: number, bool? : boolean): number`
|
|
62
|
+
|
|
63
|
+
Generates random integers within the specified range.
|
|
64
|
+
|
|
65
|
+
- `min` : indicates the minimum value (including).
|
|
66
|
+
- `max` : indicates the maximum value (inclusive).
|
|
67
|
+
- `bool` : Indicates whether the maximum value is contained. The default value is false.
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const randomNumber = randomNum(1, 10);
|
|
71
|
+
console.log(randomNumber); // Generates a random integer between 1 and 10
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
##### 2. `urlSplit(url: string): object`
|
|
75
|
+
|
|
76
|
+
Parses URL query parameters into objects.
|
|
77
|
+
|
|
78
|
+
- `url` : indicates the URL containing the query parameter.
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
const url = "https://example.com/path?param1=value1¶m2=value2";
|
|
82
|
+
const params = urlSplit(url);
|
|
83
|
+
console.log(params);
|
|
84
|
+
// Output: {param1: 'value1', param2: 'value2'}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
##### 3. `urlJoin(url: string, query: object): string`
|
|
88
|
+
|
|
89
|
+
Converts the object to a URL query parameter and concatenates it with the original URL.
|
|
90
|
+
|
|
91
|
+
- `url` : indicates the original URL.
|
|
92
|
+
- `query` : object containing query parameters.
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
const url = "https://example.com/path";
|
|
96
|
+
const query = { param1: "value1", param2: "value2" };
|
|
97
|
+
const newUrl = urlJoin(url, query);
|
|
98
|
+
console.log(newUrl);
|
|
99
|
+
// Output: https://example.com/path?param1=value1¶m2=value2
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
##### 4. `getType(data: any): string`
|
|
103
|
+
|
|
104
|
+
Get the type of data.
|
|
105
|
+
|
|
106
|
+
- `data` : indicates the data to be checked.
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
const dataType = getType("Hello, World!");
|
|
110
|
+
console.log(dataType); // Output: string
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
##### 5. `getTypeByList(data: any, whiteList: string[]): boolean`
|
|
114
|
+
|
|
115
|
+
Check whether the data type is in the whitelist.
|
|
116
|
+
|
|
117
|
+
- `data` : indicates the data to be checked.
|
|
118
|
+
- `whiteList` : indicates the list of allowed data types.
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
const data = 42;
|
|
122
|
+
const allowedTypes = ["number", "string"];
|
|
123
|
+
const isTypeAllowed = getTypeByList(data, allowedTypes);
|
|
124
|
+
console.log(isTypeAllowed); // Output: true
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
##### 6. `toKebabCase(camelCase: string, separator: string = "-"): string`
|
|
128
|
+
|
|
129
|
+
The hump name is converted to a hyphenated name
|
|
130
|
+
|
|
131
|
+
- `camelCase` : hump type variable
|
|
132
|
+
- `separator` : indicates the separator
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
const camelCase = "fontSize";
|
|
136
|
+
const kebabCase = toKebabCase(camelCase);
|
|
137
|
+
console.log(kebabCase); // Output: font-size
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### object module
|
|
141
|
+
|
|
142
|
+
##### 1. `getValue(object: object, key: string, defaultValue: any = ''): any`
|
|
143
|
+
|
|
144
|
+
Gets the value of the specified path in the object.
|
|
145
|
+
|
|
146
|
+
- `object` : indicates the target object.
|
|
147
|
+
- `key` : the path to the value to be obtained.
|
|
148
|
+
- `defaultValue` : Default value, returned if the path does not exist.
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
const obj = { a: { b: { c: 42 } } };
|
|
152
|
+
const value = getValue(obj, "a.b.c", "default");
|
|
153
|
+
console.log(value); // Output: 42
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
##### 2. `setValue(object: object, key: string, value: any = {}): object`
|
|
157
|
+
|
|
158
|
+
Sets the value of the specified path in the object.
|
|
159
|
+
|
|
160
|
+
- `object` : indicates the target object.
|
|
161
|
+
- `key` : path to the value to be set.
|
|
162
|
+
- `value` : indicates the value to be set.
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const obj = { a: { b: { c: 42 } } };
|
|
166
|
+
setValue(obj, "a.b.d", "new value");
|
|
167
|
+
console.log(obj); // Output: {a: {b: {c: 42, d: 'new value'}}}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
##### 3. `mixIn(target: object, source: object = {}, overwrite: boolean = false): object`
|
|
171
|
+
|
|
172
|
+
Mixes the properties of the source object into the target object.
|
|
173
|
+
|
|
174
|
+
- `target` : indicates the target object.
|
|
175
|
+
- `source` : indicates the source object.
|
|
176
|
+
- `overwrite` : Whether to overwrite an existing attribute. The default is' false '.
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
const target = { a: 1 };
|
|
180
|
+
const source = { b: 2 };
|
|
181
|
+
const result = mixIn(target, source);
|
|
182
|
+
console.log(result); // Output: {a: 1, b: 2}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
##### 4. `enumInversion(target: object): object`
|
|
186
|
+
|
|
187
|
+
Inverts the key-value pair of an enumeration object.
|
|
188
|
+
|
|
189
|
+
- `target` : enumeration object to be reversed.
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
const enumObj = { key1: "value1", key2: "value2" };
|
|
193
|
+
const invertedEnum = enumInversion(enumObj);
|
|
194
|
+
console.log(invertedEnum); // Output: {value1: 'key1', value2: 'key2'}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
##### 5. `isNotObject(source: any, type: string): boolean`
|
|
198
|
+
|
|
199
|
+
Check whether the value is of a non-object type.
|
|
200
|
+
|
|
201
|
+
- `source` : indicates the value to be checked.
|
|
202
|
+
- `type` : indicates the data type.
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
const result = isNotObject(42, "number");
|
|
206
|
+
console.log(result); // Output: false
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
##### 6. `cloneDeep(target: any): any`
|
|
210
|
+
|
|
211
|
+
Deep clone object.
|
|
212
|
+
|
|
213
|
+
- `target` : indicates the object to be cloned.
|
|
214
|
+
|
|
215
|
+
```javascript
|
|
216
|
+
const obj = { a: { b: { c: 42 } } };
|
|
217
|
+
const clonedObj = cloneDeep(obj);
|
|
218
|
+
console.log(clonedObj); // Output: {a: {b: {c: 42}}}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
##### 7. `createObjectVariable(type: string, source: object = {}): object`
|
|
222
|
+
|
|
223
|
+
Creates an object of a specific type based on its type.
|
|
224
|
+
|
|
225
|
+
- `type` : indicates the type of the object to be created.
|
|
226
|
+
- `source` : initializes the data of the object.
|
|
227
|
+
|
|
228
|
+
```javascript
|
|
229
|
+
const array = createObjectVariable("array", [1, 2, 3]);
|
|
230
|
+
console.log(array); // Output: [1, 2, 3]
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
##### 8. `createObject(source: object): object`
|
|
234
|
+
|
|
235
|
+
Create objects using a prototype chain.
|
|
236
|
+
|
|
237
|
+
- `source` : prototype object.
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
const protoObj = { a: 1 };
|
|
241
|
+
const newObj = createObject(protoObj);
|
|
242
|
+
console.log(newObj.a); // Output: 1
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
##### 9. `inherit(source: object, target: function): function`
|
|
246
|
+
|
|
247
|
+
Inherit the prototype chain.
|
|
248
|
+
|
|
249
|
+
- `source` : prototype chain of the parent object.
|
|
250
|
+
- `target` : constructor of the child object.
|
|
251
|
+
|
|
252
|
+
```javascript
|
|
253
|
+
function Parent() {}
|
|
254
|
+
Parent.prototype.method = function () {
|
|
255
|
+
console.log("Hello from parent");
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
function Child() {}
|
|
259
|
+
inherit(Parent, Child);
|
|
260
|
+
const childInstance = new Child();
|
|
261
|
+
childInstance.method(); // Output: Hello from parent
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
##### 10. `getInstance(classProto: function, overwrite: boolean = false, ... params: any[]): object`
|
|
265
|
+
|
|
266
|
+
Gets a singleton instance of a class.
|
|
267
|
+
|
|
268
|
+
- `classProto` : The prototype chain of the class.
|
|
269
|
+
- `overwrite` : Whether to overwrite an existing instance. The default is' false '.
|
|
270
|
+
- `params` : arguments to the class constructor.
|
|
271
|
+
|
|
272
|
+
```javascript
|
|
273
|
+
class Singleton {
|
|
274
|
+
constructor(name) {
|
|
275
|
+
this.name = name;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const instance1 = getInstance(Singleton, false, "Instance 1");
|
|
280
|
+
const instance2 = getInstance(Singleton, true, "Instance 2");
|
|
281
|
+
|
|
282
|
+
console.log(instance1 === instance2); // Output: false
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
##### 11. `classDecorator(params: object): ClassDecorator`
|
|
286
|
+
|
|
287
|
+
Add a decorator to the class.
|
|
288
|
+
|
|
289
|
+
- `params` : properties and methods to add.
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
@classDecorator({
|
|
293
|
+
additionalMethod: function () {
|
|
294
|
+
console.log("Additional method");
|
|
295
|
+
},
|
|
296
|
+
})
|
|
297
|
+
class DecoratedClass {}
|
|
298
|
+
|
|
299
|
+
const instance = new DecoratedClass();
|
|
300
|
+
instance.additionalMethod(); // Output: Additional method
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
##### 12. `stringToJson(target: string): object | null`
|
|
304
|
+
|
|
305
|
+
Converts a JSON string to an object.
|
|
306
|
+
|
|
307
|
+
- `target` : JSON string to be converted.
|
|
308
|
+
|
|
309
|
+
```javascript
|
|
310
|
+
const jsonString = '{"key": "value"}';
|
|
311
|
+
const jsonObject = stringToJson(jsonString);
|
|
312
|
+
console.log(jsonObject); // Output: {key: 'value'}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
##### 13. `jsonToString(target: any): string`
|
|
316
|
+
|
|
317
|
+
Converts the object to a JSON string.
|
|
318
|
+
|
|
319
|
+
- `target` : indicates the object to be converted.
|
|
320
|
+
|
|
321
|
+
```javascript
|
|
322
|
+
const obj = { key: "value" };
|
|
323
|
+
const jsonString = jsonToString(obj);
|
|
324
|
+
console.log(jsonString); // Output: '{"key":"value"}'
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
##### 14. `isWindow(win: any): boolean`
|
|
328
|
+
|
|
329
|
+
Check whether it is a browser window.
|
|
330
|
+
|
|
331
|
+
- `win` : indicates the object to be checked.
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
const isBrowserWindow = isWindow(window);
|
|
335
|
+
console.log(isBrowserWindow); // Output: true
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
##### 14. `emptyObject(init: IObject): object`
|
|
339
|
+
|
|
340
|
+
Creates an object whose prototype is empty.
|
|
341
|
+
|
|
342
|
+
- `init` : initializes the object.
|
|
343
|
+
|
|
344
|
+
```javascript
|
|
345
|
+
const o = emptyObject({ name: "hunter" });
|
|
346
|
+
const o2 = { name: "hunter" };
|
|
347
|
+
console.log(o.__proto__, o2.__proto__); // Output: undefined, [Object: null prototype] {}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
##### 15. `isEmptyObject(object: IObject<unknown>): boolean`
|
|
351
|
+
|
|
352
|
+
Determines if the object is empty
|
|
353
|
+
|
|
354
|
+
- `object` : the target object.
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
const o = isEmptyObject({name: "a u"});
|
|
358
|
+
const o2 = isEmptyObject({});
|
|
359
|
+
console.log(o, o2); // Output: false true
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
#### array module
|
|
363
|
+
|
|
364
|
+
##### 1. `arrayRandom(arr: any[]): any[]`
|
|
365
|
+
|
|
366
|
+
Randomly sort the array.
|
|
367
|
+
|
|
368
|
+
- `arr` : array to sort.
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
371
|
+
const originalArray = [1, 2, 3, 4, 5];
|
|
372
|
+
const randomizedArray = arrayRandom(originalArray);
|
|
373
|
+
console.log(randomizedArray);
|
|
374
|
+
// Output: a randomly sorted array
|
|
375
|
+
````
|
|
376
|
+
|
|
377
|
+
##### 2. `arrayUniq(arr: any[]): any[]`
|
|
378
|
+
|
|
379
|
+
Removes duplicate elements from the array.
|
|
380
|
+
|
|
381
|
+
- `arr` : array to process.
|
|
382
|
+
|
|
383
|
+
```javascript
|
|
384
|
+
const arrayWithDuplicates = [1, 2, 2, 3, 4, 4, 5];
|
|
385
|
+
const uniqueArray = arrayUniq(arrayWithDuplicates);
|
|
386
|
+
console.log(uniqueArray);
|
|
387
|
+
// Output: Array with duplicate elements removed
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
##### 3. `arrayDemote(arr: IDemoteArray<any>, result: any[] = []): any[]`
|
|
391
|
+
|
|
392
|
+
Reduces the dimension of multiple nested arrays.
|
|
393
|
+
|
|
394
|
+
- `arr` : array to be reduced.
|
|
395
|
+
- `result` : The array used to store the result. The default is an empty array.
|
|
396
|
+
|
|
397
|
+
```javascript
|
|
398
|
+
const nestedArray = [1, [2, [3, [4]], 5]];
|
|
399
|
+
const demotedArray = arrayDemote(nestedArray);
|
|
400
|
+
console.log(demotedArray);
|
|
401
|
+
// Output: reduced array [1, 2, 3, 4, 5]
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
#### function module
|
|
405
|
+
|
|
406
|
+
##### 1. `throttle(fn: Function, time: number): Function`
|
|
407
|
+
|
|
408
|
+
Limits how often a function is called within a specified period of time.
|
|
409
|
+
|
|
410
|
+
- `fn` : function to be executed.
|
|
411
|
+
- `time` : indicates the time interval (milliseconds).
|
|
412
|
+
|
|
413
|
+
```javascript
|
|
414
|
+
const throttledFunction = throttle(
|
|
415
|
+
() => console.log("Throttled function called!"),
|
|
416
|
+
1000
|
|
417
|
+
);
|
|
418
|
+
throttledFunction(); // This command is executed only after 1 second
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
##### 2. `debounce(fn: Function, time: number): Function`
|
|
422
|
+
|
|
423
|
+
Limits the continuous invocation of a function for a specified period of time.
|
|
424
|
+
|
|
425
|
+
- `fn` : function to be executed.
|
|
426
|
+
- `time` : indicates the time interval (milliseconds).
|
|
427
|
+
|
|
428
|
+
```javascript
|
|
429
|
+
const debouncedFunction = debounce(
|
|
430
|
+
() => console.log("Debounced function called!"),
|
|
431
|
+
1000
|
|
432
|
+
);
|
|
433
|
+
debouncedFunction(); // Called multiple times within 1 second, only the last time will be executed
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
##### 3. `defer(timer: number = 0): { promise: Promise<void>, resolve: Function, reject: Function }`
|
|
437
|
+
|
|
438
|
+
Create a flat Promise delay object that can be set to time out after a certain amount of time.
|
|
439
|
+
|
|
440
|
+
- `timer` : indicates the timeout period (milliseconds). The default value is 0.
|
|
441
|
+
|
|
442
|
+
```javascript
|
|
443
|
+
const deferFn = () => {
|
|
444
|
+
const { resolve, promise } = defer();
|
|
445
|
+
setTimeout(() => resolve("success"), 500);
|
|
446
|
+
return promise;
|
|
447
|
+
};
|
|
448
|
+
const delayFn = () => {
|
|
449
|
+
const { promise } = defer(1000);
|
|
450
|
+
return promise;
|
|
451
|
+
};
|
|
452
|
+
deferFn().then(console.log); // Delay output
|
|
453
|
+
delayFn().catch(console.error); // Timeout output
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
##### 4. `catchAwait(defer: Promise<any>): Promise<[Error | null, any]>`
|
|
457
|
+
|
|
458
|
+
Catches an Error for an asynchronous operation and returns' [Error, null] 'or' [null, result] '.
|
|
459
|
+
|
|
460
|
+
- `defer` : The Promise object for the asynchronous operation.
|
|
461
|
+
|
|
462
|
+
```javascript
|
|
463
|
+
const asyncOperation = new Promise((resolve, reject) => {
|
|
464
|
+
// Simulate asynchronous operations
|
|
465
|
+
setTimeout(() => {
|
|
466
|
+
reject(new Error("Something went wrong!"));
|
|
467
|
+
}, 1000);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
const [error, result] = await catchAwait(asyncOperation);
|
|
471
|
+
console.log(error); // Output: Error: Something went wrong!
|
|
472
|
+
console.log(result); // Output: null
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
##### 5. `requestFrame(callback: (timestamp: number) => void, delay? : number): () => void`
|
|
476
|
+
|
|
477
|
+
Custom frame callbacks for use in browsers and Node.js environments (like setinterval)
|
|
478
|
+
|
|
479
|
+
- callback: callback function to be executed in each frame, receiving a parameter timestamp indicating the current timestamp.
|
|
480
|
+
- delay (optional) : specifies the interval (milliseconds) between each callback execution. The default value is 0.
|
|
481
|
+
|
|
482
|
+
````javascript
|
|
483
|
+
let count = 0;
|
|
484
|
+
const clear = requestFrame(() => {
|
|
485
|
+
console.log(count);
|
|
486
|
+
count++;
|
|
487
|
+
if (count === 5) {
|
|
488
|
+
clear(); // Cancel the execution
|
|
489
|
+
}
|
|
490
|
+
}, 1000);
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
#### element module
|
|
494
|
+
|
|
495
|
+
##### 1. `createElement(options: { ele? : string | HTMLElement, style? : object, attr? : object, parent? : HTMLElement }): HTMLElement`
|
|
496
|
+
|
|
497
|
+
Creates and returns an HTML element.
|
|
498
|
+
|
|
499
|
+
- `ele` : element type or existing HTMLElement object, optional, default is 'div'.
|
|
500
|
+
- `style` : The style object of the element, optional.
|
|
501
|
+
- `attr` : attribute object of the element, optional.
|
|
502
|
+
- `parent` : indicates the parent of the element. This parameter is optional.
|
|
503
|
+
|
|
504
|
+
```javascript
|
|
505
|
+
const options = {
|
|
506
|
+
ele: "div",
|
|
507
|
+
style: { color: "blue", fontSize: "16px" },
|
|
508
|
+
attr: { id: "myElement", className: "custom-class" },
|
|
509
|
+
parent: document.body,
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
const createdElement = createElement(options);
|
|
513
|
+
// Create a div element with style and attributes in the body
|
|
514
|
+
````
|
|
515
|
+
|
|
516
|
+
#### event module
|
|
517
|
+
|
|
518
|
+
##### 1. `addHandler(ele: HTMLElement, type: string, handler: EventListener): void`
|
|
519
|
+
|
|
520
|
+
Add event listeners to the element.
|
|
521
|
+
|
|
522
|
+
- `ele` : target element.
|
|
523
|
+
- `type` : indicates the type of the event.
|
|
524
|
+
- `handler` : event handler.
|
|
525
|
+
|
|
526
|
+
```javascript
|
|
527
|
+
const button = document.getElementById("myButton");
|
|
528
|
+
const handleClick = () => console.log("Button clicked! ");
|
|
529
|
+
addHandler(button, "click", handleClick);
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
##### 2. `stopBubble(event: Event): void`
|
|
533
|
+
|
|
534
|
+
Prevent events from bubbling.
|
|
535
|
+
|
|
536
|
+
- `event` : indicates an event object.
|
|
537
|
+
|
|
538
|
+
```javascript
|
|
539
|
+
const handleClick = (event) => {
|
|
540
|
+
console.log("Button clicked! ");
|
|
541
|
+
stopBubble(event);
|
|
542
|
+
};
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
##### 3. `stopDefault(event: Event): void`
|
|
546
|
+
|
|
547
|
+
Default behavior to block events.
|
|
548
|
+
|
|
549
|
+
- `event` : indicates an event object.
|
|
550
|
+
|
|
551
|
+
```javascript
|
|
552
|
+
const handleFormSubmit = (event) => {
|
|
553
|
+
console.log("Form submitted! ");
|
|
554
|
+
stopDefault(event);
|
|
555
|
+
};
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
##### 4. `removeHandler(ele: HTMLElement, type: string, handler: EventListener): void`
|
|
559
|
+
|
|
560
|
+
Removes an element's event listener.
|
|
561
|
+
|
|
562
|
+
- `ele` : target element.
|
|
563
|
+
- `type` : indicates the type of the event.
|
|
564
|
+
- `handler` : event handler to be removed.
|
|
565
|
+
|
|
566
|
+
```javascript
|
|
567
|
+
const button = document.getElementById("myButton");
|
|
568
|
+
const handleClick = () => console.log("Button clicked! ");
|
|
569
|
+
addHandler(button, "click", handleClick);
|
|
570
|
+
// Other operations...
|
|
571
|
+
removeHandler(button, "click", handleClick);
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
##### 5. `dispatchEvent(ele: HTMLElement, data: any): void`
|
|
575
|
+
|
|
576
|
+
Trigger a custom event.
|
|
577
|
+
|
|
578
|
+
- `ele` : target element.
|
|
579
|
+
- `data` : indicates the data of a custom event.
|
|
580
|
+
|
|
581
|
+
```javascript
|
|
582
|
+
const customEvent = new CustomEvent("customEvent", {
|
|
583
|
+
detail: { key: "value" },
|
|
584
|
+
});
|
|
585
|
+
const targetElement = document.getElementById("myElement");
|
|
586
|
+
dispatchEvent(targetElement, customEvent);
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
#### storage module
|
|
590
|
+
|
|
591
|
+
##### 1. `setStorage(key: string, val: any): void`
|
|
592
|
+
|
|
593
|
+
Store values to local storage.
|
|
594
|
+
|
|
595
|
+
- `key` : specifies the name of the key.
|
|
596
|
+
- `val` : The value to be stored.
|
|
597
|
+
|
|
598
|
+
```javascript
|
|
599
|
+
const userData = { username: "john_doe", email: "john@example.com" };
|
|
600
|
+
setStorage("user_data", userData);
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
##### 2. `getStorage(key: string): any`
|
|
604
|
+
|
|
605
|
+
Gets the value from local storage.
|
|
606
|
+
|
|
607
|
+
- `key` : specifies the key name to be obtained.
|
|
608
|
+
|
|
609
|
+
```javascript
|
|
610
|
+
const storedUserData = getStorage("user_data");
|
|
611
|
+
console.log(storedUserData);
|
|
612
|
+
// Output: {username: 'john_doe', email: 'john@example.com'}
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
##### 3. `clearStorage(key? : string): void`
|
|
616
|
+
|
|
617
|
+
Clear the value from the local store.
|
|
618
|
+
|
|
619
|
+
- `key` : specifies the name of the key to be cleared. If no key name is provided, all storage is cleared.
|
|
620
|
+
|
|
621
|
+
```javascript
|
|
622
|
+
clearStorage("user_data"); // Clear the stored value for a specific key name
|
|
623
|
+
// or
|
|
624
|
+
clearStorage(); // Clear all locally stored values
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
#### log module
|
|
628
|
+
|
|
629
|
+
##### 1. `logOneLine(str: string, overwrite: boolean = false, wrap: boolean = true): void`
|
|
630
|
+
|
|
631
|
+
Output logs in one line.
|
|
632
|
+
|
|
633
|
+
- `str` : The string to be output.
|
|
634
|
+
- `overwrite` : Whether to overwrite the current line. The default is' false '.
|
|
635
|
+
- `wrap` : Whether to wrap lines after output, defaults to 'true'.
|
|
636
|
+
|
|
637
|
+
```javascript
|
|
638
|
+
logOneLine("This is a single line log message.");
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
##### 2. `logLoop(opts? : { loopList? : string[], isStop? : boolean, timer? : number, index? : number }): { loopList? : string[], isStop? : boolean, timer? : number, index? : number }`
|
|
642
|
+
|
|
643
|
+
Output a loop animation in the console.
|
|
644
|
+
|
|
645
|
+
- `opts` : an optional parameter object containing the following properties:
|
|
646
|
+
- `loopList` : animation character list, the default is `[' \ \ ', '|', '/', '-', '-']`.
|
|
647
|
+
- `isStop` : Whether to stop the loop. The default is' false '.
|
|
648
|
+
- `timer` : Animation switch interval (milliseconds), default is' 100 '.
|
|
649
|
+
- `index` : indicates the index of the current animated character. The default value is 0.
|
|
650
|
+
|
|
651
|
+
```javascript
|
|
652
|
+
logLoop(); // Start the default loop animation
|
|
653
|
+
// or
|
|
654
|
+
logLoop({ loopList: ["-", "+", "-", "+"], timer: 200 }); // Start a custom loop animation
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
#### animation module
|
|
658
|
+
|
|
659
|
+
##### 1. `class AnimateFrame`
|
|
660
|
+
|
|
661
|
+
A class that provides frame animation.
|
|
662
|
+
|
|
663
|
+
- `start(duration? : number): void ': Starts frame animation, optional parameter' duration 'is frame number control.
|
|
664
|
+
- `stop(): void` : stops frame animation.
|
|
665
|
+
|
|
666
|
+
```javascript
|
|
667
|
+
const animateFrame = new AnimateFrame((timestamp) => {
|
|
668
|
+
// Update animation status here
|
|
669
|
+
console.log("AnimationFrame callback:", timestamp);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
animateFrame.start(60); // Start frame animation at 60 frames per second
|
|
673
|
+
// Other operations...
|
|
674
|
+
animateFrame.stop(); // Stop frame animation
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
##### 2. `quadraticBezier(_x: number, _y: number, t: number): [number, number]`
|
|
678
|
+
|
|
679
|
+
Calculate the coordinates of points on the quadratic Bessel curve.
|
|
680
|
+
|
|
681
|
+
- `\_x` : x coordinates of control point 1.
|
|
682
|
+
- `\_y` : y coordinate of control point 1.
|
|
683
|
+
- `t` : indicates the time parameter. The value ranges from 0 to 1.
|
|
684
|
+
|
|
685
|
+
```javascript
|
|
686
|
+
const result = quadraticBezier(0, 0, 1, 1, 0.5);
|
|
687
|
+
console.log(result);
|
|
688
|
+
// Output: [0.75, 0.75]
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
##### 3. `cubicBezier(_x1: number, _y1: number, _x2: number, _y2: number, t: number): [number, number]`
|
|
692
|
+
|
|
693
|
+
Calculate the coordinates of points on a cubic Bezier curve.
|
|
694
|
+
|
|
695
|
+
- `\_x1` : x coordinate of control point 1.
|
|
696
|
+
- `\_y1` : y coordinate of control point 1.
|
|
697
|
+
- `\_x2` : control point 2 x coordinates.
|
|
698
|
+
- `\_y2` : y coordinate of control point 2.
|
|
699
|
+
- `t` : indicates the time parameter. The value ranges from 0 to 1.
|
|
700
|
+
|
|
701
|
+
```javascript
|
|
702
|
+
const result = cubicBezier(0, 0, 0.5, 1, 0.5);
|
|
703
|
+
console.log(result);
|
|
704
|
+
// Output: [0.375, 0.625]
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
##### 4. `factorial(n: number): number`
|
|
708
|
+
|
|
709
|
+
Calculate the factorial.
|
|
710
|
+
|
|
711
|
+
- n: indicates a non-negative integer.
|
|
712
|
+
|
|
713
|
+
```javascript
|
|
714
|
+
const result = factorial(5);
|
|
715
|
+
console.log(result);
|
|
716
|
+
// Output: 120
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
##### 5. `combination(n: number, k: number): number`
|
|
720
|
+
|
|
721
|
+
Calculate the number of combinations.
|
|
722
|
+
|
|
723
|
+
- n: indicates the total number.
|
|
724
|
+
- `k` : indicates the number of selected items.
|
|
725
|
+
|
|
726
|
+
```javascript
|
|
727
|
+
const result = combination(5, 2);
|
|
728
|
+
console.log(result);
|
|
729
|
+
// Output: 10
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
##### 6. `NBezier(points: number[][], t: number): [number, number]`
|
|
733
|
+
|
|
734
|
+
Calculate the point coordinates on the NTH Bezier curve.
|
|
735
|
+
|
|
736
|
+
- `points` : array of control points. Each point is a second-order array.
|
|
737
|
+
- `t` : indicates the time parameter. The value ranges from 0 to 1.
|
|
738
|
+
|
|
739
|
+
```javascript
|
|
740
|
+
const points = [
|
|
741
|
+
[0, 0],
|
|
742
|
+
[1, 1],
|
|
743
|
+
[2, 0],
|
|
744
|
+
];
|
|
745
|
+
const result = NBezier(points, 0.5);
|
|
746
|
+
console.log(result);
|
|
747
|
+
// Output: [1.5, 0.75]
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
#### event-message-center
|
|
751
|
+
|
|
752
|
+
https://gitee.com/DieHunter/message-center
|
|
753
|
+
|
|
754
|
+
#### task-queue-lib
|
|
755
|
+
|
|
756
|
+
https://gitee.com/DieHunter/task-queue
|
|
757
|
+
|
|
758
|
+
#### js-request-lib
|
|
759
|
+
|
|
760
|
+
https://gitee.com/DieHunter/js-request-lib
|