@creejs/commons-lang 1.0.3 → 2.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/package.json +2 -1
- package/types/lang-utils.d.ts +29 -29
- package/types/promise-utils.d.ts +10 -7
- package/types/type-assert.d.ts +61 -13
- package/types/type-utils.d.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@creejs/commons-lang",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Commons Utils About Language",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"private": false,
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"types/",
|
|
11
11
|
"README.md"
|
|
12
12
|
],
|
|
13
|
+
"types": "types/index.d.ts",
|
|
13
14
|
"publishConfig": {
|
|
14
15
|
"access": "public"
|
|
15
16
|
},
|
package/types/lang-utils.d.ts
CHANGED
|
@@ -3,18 +3,18 @@
|
|
|
3
3
|
* @description Language utility functions
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
* Gets the constructor name of a value.
|
|
7
|
+
* @param {*} value - The value to check.
|
|
8
|
+
* @returns {string|undefined} The constructor name, or undefined if value has no constructor.
|
|
9
|
+
*/
|
|
10
10
|
export function constructorName(value: any): string | undefined;
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
* Assigns default values from source objects to target object for undefined properties.
|
|
13
|
+
* @param {Object<string, any>} target - The target object to assign defaults to
|
|
14
|
+
* @param {...Object<string, any>} sources - Source objects containing default values
|
|
15
|
+
* @returns {Object<string, any>} The modified target object with defaults applied
|
|
16
|
+
* @throws {TypeError} If target is null or undefined
|
|
17
|
+
*/
|
|
18
18
|
export function defaults(target: {
|
|
19
19
|
[x: string]: any;
|
|
20
20
|
}, ...sources: {
|
|
@@ -23,12 +23,12 @@ export function defaults(target: {
|
|
|
23
23
|
[x: string]: any;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
* Extends a target object with properties from one or more source objects.
|
|
27
|
+
* @param {Object<string, any>} target - The target object to extend (must not be null/undefined)
|
|
28
|
+
* @param {...Object<string, any>} sources - One or more source objects to copy properties from
|
|
29
|
+
* @returns {Object<string, any>} The modified target object
|
|
30
|
+
* @throws {TypeError} If target is null or undefined
|
|
31
|
+
*/
|
|
32
32
|
export function extend(target: {
|
|
33
33
|
[x: string]: any;
|
|
34
34
|
}, ...sources: {
|
|
@@ -37,22 +37,22 @@ export function extend(target: {
|
|
|
37
37
|
[x: string]: any;
|
|
38
38
|
};
|
|
39
39
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
* Compares two values for equality
|
|
41
|
+
* 1. First checks strict equality (===),
|
|
42
|
+
* 2. then checks if either value has an `equals` method and uses it.
|
|
43
|
+
* @param {*} value1 - First value to compare
|
|
44
|
+
* @param {*} value2 - Second value to compare
|
|
45
|
+
* @returns {boolean} True if values are equal, false otherwise
|
|
46
|
+
*/
|
|
47
47
|
export function equals(value1: any, value2: any): boolean;
|
|
48
48
|
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
* Check if the current environment is a browser
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
52
|
export function isBrowser(): boolean;
|
|
53
53
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
* Check if the current environment is a nodejs
|
|
55
|
+
* @returns {boolean}
|
|
56
|
+
*/
|
|
57
57
|
export function isNode(): boolean;
|
|
58
58
|
export { extend as extends };
|
package/types/promise-utils.d.ts
CHANGED
|
@@ -2,23 +2,26 @@
|
|
|
2
2
|
* Creates a "Deferred" Object with Timeout support
|
|
3
3
|
* 1. timeout=-1, it means no timeout check
|
|
4
4
|
* @param {number} [timeout=-1] - Timeout duration in milliseconds
|
|
5
|
+
* @param {string} [timeoutMessage]
|
|
5
6
|
* @returns {{promise: Promise<*>, reject: function, resolve: function}}
|
|
6
7
|
*/
|
|
7
|
-
export function defer(timeout?: number, timeoutMessage
|
|
8
|
+
export function defer(timeout?: number, timeoutMessage?: string): {
|
|
8
9
|
promise: Promise<any>;
|
|
9
10
|
reject: Function;
|
|
10
11
|
resolve: Function;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
|
-
* Delays
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* Delays a promise by a specified time.
|
|
15
|
+
* 1. delay(), wait 1ms
|
|
16
|
+
* 2. delay(1000), wait 1000ms
|
|
17
|
+
* 3. delay(promise), after promise settled, wait 1000ms
|
|
18
|
+
* 4. delay(promise, 2000), after promise settled, wait 2000ms
|
|
16
19
|
*
|
|
17
|
-
* @param {Promise} promise - The input promise to delay
|
|
18
|
-
* @param {number} [ms
|
|
20
|
+
* @param {Promise<*>|number|undefined} [promise] - The input promise to delay
|
|
21
|
+
* @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
|
|
19
22
|
* @returns {Promise} A new promise that settles after the delay period
|
|
20
23
|
*/
|
|
21
|
-
export function delay(promise
|
|
24
|
+
export function delay(promise?: Promise<any> | number | undefined, ms?: number | undefined): Promise<any>;
|
|
22
25
|
/**
|
|
23
26
|
* Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
|
|
24
27
|
* @param {Promise<*>} promise - The promise to wrap with a timeout
|
package/types/type-assert.d.ts
CHANGED
|
@@ -1,91 +1,139 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* if value is not a Number, throw error
|
|
3
3
|
* @param {*} value
|
|
4
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
4
5
|
* @returns {void}
|
|
5
6
|
* @throws {Error}
|
|
6
7
|
*/
|
|
7
|
-
export function assertNumber(value: any): void;
|
|
8
|
+
export function assertNumber(value: any, paramName?: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Asserts that a value is a positive number.
|
|
11
|
+
* @param {number} value - The value to check.
|
|
12
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
13
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
14
|
+
*/
|
|
15
|
+
export function assertPositive(value: number, paramName?: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Asserts that a value is a Negative number.
|
|
18
|
+
* @param {number} value - The value to check.
|
|
19
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
20
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
21
|
+
*/
|
|
22
|
+
export function assertNegative(value: number, paramName?: string): void;
|
|
8
23
|
/**
|
|
9
24
|
* if value is not a string, throw error
|
|
10
25
|
* @param {*} value
|
|
26
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
11
27
|
* @returns {void}
|
|
12
28
|
* @throws {Error}
|
|
13
29
|
*/
|
|
14
|
-
export function assertBoolean(value: any): void;
|
|
30
|
+
export function assertBoolean(value: any, paramName?: string): void;
|
|
15
31
|
/**
|
|
16
32
|
* if value is not a Object, throw error
|
|
17
33
|
* @param {*} value
|
|
34
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
18
35
|
* @returns {void}
|
|
19
36
|
* @throws {Error}
|
|
20
37
|
*/
|
|
21
|
-
export function assertObject(value: any): void;
|
|
38
|
+
export function assertObject(value: any, paramName?: string): void;
|
|
22
39
|
/**
|
|
23
40
|
* if value is not a PlainObject, throw error
|
|
24
41
|
* @param {*} value
|
|
42
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
25
43
|
* @returns {void}
|
|
26
44
|
* @throws {Error}
|
|
27
45
|
*/
|
|
28
|
-
export function assertPlainObject(value: any): void;
|
|
46
|
+
export function assertPlainObject(value: any, paramName?: string): void;
|
|
29
47
|
/**
|
|
30
48
|
* if value is not a Symbol, throw error
|
|
31
49
|
* @param {*} value
|
|
50
|
+
* @param {string} [paramName] - The name of the parameter to check@param {string} [paramName] - The name of the parameter to check
|
|
32
51
|
* @returns {void}
|
|
33
52
|
* @throws {Error}
|
|
34
53
|
*/
|
|
35
|
-
export function assertSymbol(value: any): void;
|
|
54
|
+
export function assertSymbol(value: any, paramName?: string): void;
|
|
36
55
|
/**
|
|
37
56
|
* if value is not a Function, throw error
|
|
38
57
|
* @param {*} value
|
|
58
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
39
59
|
* @returns {void}
|
|
40
60
|
* @throws {Error}
|
|
41
61
|
*/
|
|
42
|
-
export function assertFunction(value: any): void;
|
|
62
|
+
export function assertFunction(value: any, paramName?: string): void;
|
|
43
63
|
/**
|
|
44
64
|
* if value is not a Class instance, throw error
|
|
45
65
|
* @param {*} value
|
|
66
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
46
67
|
* @returns {void}
|
|
47
68
|
* @throws {Error}
|
|
48
69
|
*/
|
|
49
|
-
export function assertInstance(value: any): void;
|
|
70
|
+
export function assertInstance(value: any, paramName?: string): void;
|
|
50
71
|
/**
|
|
51
72
|
* if value is not a string, throw error
|
|
52
73
|
* @param {*} value
|
|
74
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
53
75
|
* @returns {void}
|
|
54
76
|
* @throws {Error}
|
|
55
77
|
*/
|
|
56
|
-
export function assertPromise(value: any): void;
|
|
78
|
+
export function assertPromise(value: any, paramName?: string): void;
|
|
57
79
|
/**
|
|
58
80
|
* if value is not a Null or Undefined, throw error
|
|
59
81
|
* @param {*} value
|
|
82
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
60
83
|
* @returns {void}
|
|
61
84
|
* @throws {Error}
|
|
62
85
|
*/
|
|
63
|
-
export function assertNil(value: any): void;
|
|
86
|
+
export function assertNil(value: any, paramName?: string): void;
|
|
87
|
+
/**
|
|
88
|
+
* Asserts that the given value is not nil.
|
|
89
|
+
* @param {*} value - The value to check
|
|
90
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
91
|
+
* @throws {Error} Throws an error if the value is nil
|
|
92
|
+
*/
|
|
93
|
+
export function assertNotNil(value: any, paramName?: string): void;
|
|
64
94
|
/**
|
|
65
95
|
* if value is not a Null, throw error
|
|
66
96
|
* @param {*} value
|
|
97
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
67
98
|
* @returns {void}
|
|
68
99
|
* @throws {Error}
|
|
69
100
|
*/
|
|
70
|
-
export function assertNull(value: any): void;
|
|
101
|
+
export function assertNull(value: any, paramName?: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* Asserts that the given value is not null.
|
|
104
|
+
* @param {*} value - The value to check
|
|
105
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
106
|
+
* @throws {Error} Throws an error if the value is null
|
|
107
|
+
*/
|
|
108
|
+
export function assertNotNull(value: any, paramName?: string): void;
|
|
71
109
|
/**
|
|
72
110
|
* if value is not a Undefined, throw error
|
|
73
111
|
* @param {*} value
|
|
112
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
74
113
|
* @returns {void}
|
|
75
114
|
* @throws {Error}
|
|
76
115
|
*/
|
|
77
|
-
export function assertUndefined(value: any): void;
|
|
116
|
+
export function assertUndefined(value: any, paramName?: string): void;
|
|
78
117
|
/**
|
|
79
118
|
* if value is not a string, throw error
|
|
80
119
|
* @param {*} value
|
|
120
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
81
121
|
* @returns {void}
|
|
82
122
|
* @throws {Error}
|
|
83
123
|
*/
|
|
84
|
-
export function assertString(value: any): void;
|
|
124
|
+
export function assertString(value: any, paramName?: string): void;
|
|
85
125
|
/**
|
|
86
126
|
* if value is not Array, throw error
|
|
87
127
|
* @param {*} value
|
|
128
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
88
129
|
* @returns {void}
|
|
89
130
|
* @throws {Error}
|
|
90
131
|
*/
|
|
91
|
-
export function assertArray(value: any): void;
|
|
132
|
+
export function assertArray(value: any, paramName?: string): void;
|
|
133
|
+
/**
|
|
134
|
+
* Asserts that the given value is either a string or a symbol.
|
|
135
|
+
* @param {*} value - The value to check.
|
|
136
|
+
* @param {string} [paramName] - Optional parameter name for error message.
|
|
137
|
+
* @throws {Error} Throws an error if the value is not a string or symbol.
|
|
138
|
+
*/
|
|
139
|
+
export function assertStringOrSymbol(value: any, paramName?: string): void;
|
package/types/type-utils.d.ts
CHANGED
|
@@ -68,6 +68,17 @@ export function isWeakMap(value: any): boolean;
|
|
|
68
68
|
* @returns {boolean} True if the value is a number, false otherwise.
|
|
69
69
|
*/
|
|
70
70
|
export function isNumber(value: any): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* check that a value is a positive number.
|
|
73
|
+
* @param {number} value - The value to check.
|
|
74
|
+
* @returns {boolean}
|
|
75
|
+
*/
|
|
76
|
+
export function isPositive(value: number): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* check that a value is a Negative number.
|
|
79
|
+
* @param {number} value - The value to check.
|
|
80
|
+
*/
|
|
81
|
+
export function isNegative(value: number): boolean;
|
|
71
82
|
/**
|
|
72
83
|
* Checks if a value is null or undefined.
|
|
73
84
|
* 1. value == null
|