@mudbean/is 2.0.2 → 2.0.3
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/CHANGELOG.md +4 -0
- package/cjs/error.js +211 -1
- package/cjs/index.js +76 -1
- package/cjs/intl.js +152 -1
- package/cjs/isArray.js +340 -1
- package/cjs/isBoolean.js +71 -1
- package/cjs/isFunction.js +120 -1
- package/cjs/isNull.js +49 -1
- package/cjs/isNumber.js +148 -1
- package/cjs/isObject.js +200 -1
- package/cjs/isString.js +121 -1
- package/cjs/isSymbol.js +36 -1
- package/cjs/isType.js +16 -1
- package/cjs/typeOf.js +72 -1
- package/es/error.js +202 -1
- package/es/index.js +12 -1
- package/es/intl.js +145 -1
- package/es/isArray.js +323 -1
- package/es/isBoolean.js +67 -1
- package/es/isFunction.js +114 -1
- package/es/isNull.js +46 -1
- package/es/isNumber.js +141 -1
- package/es/isObject.js +193 -1
- package/es/isString.js +116 -1
- package/es/isSymbol.js +34 -1
- package/es/isType.js +14 -1
- package/es/typeOf.js +70 -1
- package/package.json +16 -23
- /package/es/{types/error.d.ts → error.d.ts} +0 -0
- /package/es/{types/index.d.ts → index.d.ts} +0 -0
- /package/es/{types/intl.d.ts → intl.d.ts} +0 -0
- /package/es/{types/isArray.d.ts → isArray.d.ts} +0 -0
- /package/es/{types/isBoolean.d.ts → isBoolean.d.ts} +0 -0
- /package/es/{types/isFunction.d.ts → isFunction.d.ts} +0 -0
- /package/es/{types/isNull.d.ts → isNull.d.ts} +0 -0
- /package/es/{types/isNumber.d.ts → isNumber.d.ts} +0 -0
- /package/es/{types/isObject.d.ts → isObject.d.ts} +0 -0
- /package/es/{types/isString.d.ts → isString.d.ts} +0 -0
- /package/es/{types/isSymbol.d.ts → isSymbol.d.ts} +0 -0
- /package/es/{types/isType.d.ts → isType.d.ts} +0 -0
- /package/es/{types/typeOf.d.ts → typeOf.d.ts} +0 -0
- /package/es/{types/types.d.ts → types.d.ts} +0 -0
package/es/isBoolean.js
CHANGED
|
@@ -1 +1,67 @@
|
|
|
1
|
-
import{typeOf
|
|
1
|
+
import { typeOf } from './typeOf.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测 Javascript 数据类型工具之: boolean
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* # 当前数据类型是否为 boolean
|
|
8
|
+
*
|
|
9
|
+
* @param input - 待检测的数据,任意类型
|
|
10
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `boolean` ,且在 Typescript 中进行类型收缩
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isBoolean } from '@mudbean/is';
|
|
15
|
+
*
|
|
16
|
+
* console.log(isBoolean(true)); // true
|
|
17
|
+
* console.log(isBoolean(false)); // true
|
|
18
|
+
*
|
|
19
|
+
* console.log(isBoolean(1)); // false
|
|
20
|
+
* console.log(isBoolean('true')); // false
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function isBoolean(input) {
|
|
24
|
+
return typeOf(input) === 'boolean';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* # 当前数据类型是否为真值 true
|
|
28
|
+
*
|
|
29
|
+
* @param input - 待检测的数据,任意类型
|
|
30
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `true` ,且在 Typescript 中进行类型收缩
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { isTrue } from '@mudbean/is';
|
|
35
|
+
*
|
|
36
|
+
* console.log(isTrue(true)); // true
|
|
37
|
+
*
|
|
38
|
+
* console.log(isTrue(false)); // false
|
|
39
|
+
* console.log(isTrue(1)); // false
|
|
40
|
+
* console.log(isTrue('true')); // false
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function isTrue(input) {
|
|
44
|
+
return input === true;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* # 当前数据类型是否为真值 false
|
|
48
|
+
*
|
|
49
|
+
* @param input - 待检测的数据,任意类型
|
|
50
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `false` ,且在 Typescript 中进行类型收缩
|
|
51
|
+
* @example
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { isFalse } from '@mudbean/is';
|
|
55
|
+
*
|
|
56
|
+
* console.log(isFalse(false)); // true
|
|
57
|
+
*
|
|
58
|
+
* console.log(isFalse(true)); // false
|
|
59
|
+
* console.log(isFalse(1)); // false
|
|
60
|
+
* console.log(isFalse('true')); // false
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function isFalse(input) {
|
|
64
|
+
return input === false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { isBoolean, isFalse, isTrue };
|
package/es/isFunction.js
CHANGED
|
@@ -1 +1,114 @@
|
|
|
1
|
-
import{typeOf
|
|
1
|
+
import { typeOf } from './typeOf.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测 Javascript 数据类型工具之: function
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* # 检测 `input` 是否是 `function` 类型
|
|
8
|
+
*
|
|
9
|
+
* 函数分类:
|
|
10
|
+
* - 普通函数
|
|
11
|
+
* - 使用 `async` 标注的异步函数
|
|
12
|
+
* - 使用 `*` 标注的生成器函数
|
|
13
|
+
*
|
|
14
|
+
* @param input - 待检测的数据,任意类型
|
|
15
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `function` ,且在 Typescript 中进行类型收缩
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { isFunction } from '@mudbean/is';
|
|
20
|
+
*
|
|
21
|
+
* console.log(isFunction(() => {})); // true
|
|
22
|
+
*
|
|
23
|
+
* console.log(isFunction(async () => {})); // false
|
|
24
|
+
* console.log(isFunction(function* () {})); // false
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function isFunction(input) {
|
|
28
|
+
return 'function' === typeOf(input);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* # 检测 `input` 是否是 `Promise` 类型
|
|
32
|
+
*
|
|
33
|
+
* @param input - 待检测的数据,任意类型
|
|
34
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Promise` ,且在 Typescript 中进行类型收缩
|
|
35
|
+
* @example
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { isPromise } from '@mudbean/is';
|
|
39
|
+
*
|
|
40
|
+
* console.log(isPromise(new Promise(() => {}))); // true
|
|
41
|
+
* console.log(isPromise(() => {})); // false
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function isPromise(input) {
|
|
45
|
+
return typeOf(input) === 'promise';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* # 检测 `input` 是否是 `AsyncFunction` 类型
|
|
49
|
+
*
|
|
50
|
+
* @param input - 待检测的数据,任意类型
|
|
51
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `async function` ,且在 Typescript 中进行类型收缩
|
|
52
|
+
* @example
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { isAsyncFunction } from '@mudbean/is';
|
|
56
|
+
*
|
|
57
|
+
* console.log(isAsyncFunction(async () => {})); // true
|
|
58
|
+
* console.log(isAsyncFunction(() => {})); // false
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function isAsyncFunction(input) {
|
|
62
|
+
return typeOf(input) === 'asyncfunction';
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* # 检测 `input` 是否是 `GeneratorFunction` 类型
|
|
66
|
+
*
|
|
67
|
+
* @param input - 待检测的数据,任意类型
|
|
68
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `generator function` ,且在 Typescript 中进行类型收缩
|
|
69
|
+
* @example
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { isGeneratorFunction } from '@mudbean/is';
|
|
73
|
+
*
|
|
74
|
+
* console.log(isGeneratorFunction(function* () {})); // true
|
|
75
|
+
* console.log(isGeneratorFunction(() => {})); // false
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
function isGeneratorFunction(input) {
|
|
80
|
+
return typeOf(input) === 'generatorfunction';
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* # 检测 `input` 是否是 `Generator` 类型
|
|
84
|
+
*
|
|
85
|
+
* @param input - 待检测的数据,任意类型
|
|
86
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Generator` ,且在 Typescript 中进行类型收缩
|
|
87
|
+
* @example
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* import { isGenerator } from '@mudbean/is';
|
|
91
|
+
*
|
|
92
|
+
* const log = (str) => console.log(str);
|
|
93
|
+
* const foo = function* () {
|
|
94
|
+
* yield "a";
|
|
95
|
+
* yield "b";
|
|
96
|
+
* yield "c";
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* const gen = foo();
|
|
100
|
+
*
|
|
101
|
+
* log(gen.next().value); // a
|
|
102
|
+
* log(gen.next().value); // b
|
|
103
|
+
* log(gen.next().value); // c
|
|
104
|
+
*
|
|
105
|
+
* console.log(isGenerator(foo)); // false
|
|
106
|
+
* console.log(isGenerator(gen)); // true
|
|
107
|
+
*
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
function isGenerator(input) {
|
|
111
|
+
return typeOf(input) === 'generator';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { isAsyncFunction, isFunction, isGenerator, isGeneratorFunction, isPromise };
|
package/es/isNull.js
CHANGED
|
@@ -1 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 检测 Javascript 数据类型工具之: null
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* 检测 `input` 是否是 `null` 类型
|
|
7
|
+
*
|
|
8
|
+
* @param input - 待检测的数据,任意类型
|
|
9
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `null` ,且在 Typescript 中进行类型收缩
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { isNull } from '@mudbean/is';
|
|
14
|
+
*
|
|
15
|
+
* console.log(isNull(null)); // true
|
|
16
|
+
*
|
|
17
|
+
* console.log(isNull(undefined)); // false
|
|
18
|
+
* console.log(isNull(1)); // false
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function isNull(input) {
|
|
22
|
+
return input === null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* 检测 `input` 是否是 `undefined` 类型
|
|
27
|
+
*
|
|
28
|
+
* @param input - 待检测的数据,任意类型
|
|
29
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `undefined` ,且在 Typescript 中进行类型收缩
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { isUndefined } from '@mudbean/is';
|
|
34
|
+
*
|
|
35
|
+
* console.log(isUndefined(undefined)); // true
|
|
36
|
+
*
|
|
37
|
+
* console.log(isUndefined(null)); // false
|
|
38
|
+
* console.log(isUndefined(1)); // false
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
function isUndefined(input) {
|
|
43
|
+
return input === undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { isNull, isUndefined };
|
package/es/isNumber.js
CHANGED
|
@@ -1 +1,141 @@
|
|
|
1
|
-
import{typeOf
|
|
1
|
+
import { typeOf } from './typeOf.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测 Javascript 数据类型工具之: number
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* 检测 `input` 是否是 `number` 类型
|
|
9
|
+
*
|
|
10
|
+
* @param input - 待检测的数据,任意类型
|
|
11
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `number` ,且在 Typescript 中进行类型收缩
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { isNumber } from '@mudbean/is';
|
|
16
|
+
*
|
|
17
|
+
* isNumber(123); // true
|
|
18
|
+
*
|
|
19
|
+
* isNumber('123'); // false
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function isNumber(input) {
|
|
23
|
+
return typeOf(input) === 'number';
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* 检测 `input` 是否是 `BigInt` 类型
|
|
28
|
+
*
|
|
29
|
+
* @param input - 待检测的数据,任意类型
|
|
30
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `bigint` ,且在 Typescript 中进行类型收缩
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { isBigInt } from '@mudbean/is';
|
|
35
|
+
*
|
|
36
|
+
* isBigInt(123n); // true
|
|
37
|
+
*
|
|
38
|
+
* isBigInt('123'); // false
|
|
39
|
+
* isBigInt(123); // false
|
|
40
|
+
* isBigInt(true); // false
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function isBigInt(input) {
|
|
44
|
+
return typeOf(input) === 'bigint';
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* 检测 `input` 是否是 `NaN` 类型
|
|
49
|
+
*
|
|
50
|
+
* @param input - 待检测的数据,任意类型
|
|
51
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `NaN` ,且在 Typescript 中进行类型收缩
|
|
52
|
+
* @example
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { isNaN } from '@mudbean/is';
|
|
56
|
+
*
|
|
57
|
+
* isNaN(NaN); // true
|
|
58
|
+
*
|
|
59
|
+
* isNaN('123'); // false
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function isNaN(input) {
|
|
63
|
+
return Number.isNaN(input);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* 是否为正整数
|
|
68
|
+
*
|
|
69
|
+
* @param input - 待检测的数据,任意类型
|
|
70
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为正整数
|
|
71
|
+
* @example
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { isPositiveInteger } from '@mudbean/is';
|
|
75
|
+
*
|
|
76
|
+
* isPositiveInteger(123); // true
|
|
77
|
+
* isPositiveInteger(1); // true
|
|
78
|
+
*
|
|
79
|
+
* isPositiveInteger(0); // false
|
|
80
|
+
* isPositiveInteger(-1); // true
|
|
81
|
+
* isPositiveInteger(false); // false
|
|
82
|
+
* isPositiveInteger(Infinity); // false
|
|
83
|
+
* isPositiveInteger('123'); // false
|
|
84
|
+
* isPositiveInteger(NaN); // false
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function isPositiveInteger(input) {
|
|
88
|
+
return Number.isInteger(input) && isNumber(input) && input > 0;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
*
|
|
92
|
+
* 是否为负整数
|
|
93
|
+
*
|
|
94
|
+
* @param input - 待检测的数据,任意类型
|
|
95
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为负整数
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* import { isPositiveInteger } from '@mudbean/is';
|
|
100
|
+
*
|
|
101
|
+
* isPositiveInteger(-123); // true
|
|
102
|
+
* isPositiveInteger(-1); // true
|
|
103
|
+
*
|
|
104
|
+
* isPositiveInteger(0); // false
|
|
105
|
+
* isPositiveInteger(1); // true
|
|
106
|
+
* isPositiveInteger(false); // false
|
|
107
|
+
* isPositiveInteger(Infinity); // false
|
|
108
|
+
* isPositiveInteger('123'); // false
|
|
109
|
+
* isPositiveInteger(NaN); // false
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
function isNegativeInteger(input) {
|
|
113
|
+
return Number.isInteger(input) && isNumber(input) && input < 0;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* 是否为正整数
|
|
118
|
+
*
|
|
119
|
+
* @param input - 待检测的数据,任意类型
|
|
120
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 0
|
|
121
|
+
* @example
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { isZero } from '@mudbean/is';
|
|
125
|
+
*
|
|
126
|
+
* isZero(0); // true
|
|
127
|
+
*
|
|
128
|
+
*
|
|
129
|
+
* isZero(123); // false
|
|
130
|
+
* isZero(1); // false
|
|
131
|
+
* isZero(false); // false
|
|
132
|
+
* isZero(Infinity); // false
|
|
133
|
+
* isZero('123'); // false
|
|
134
|
+
* isZero(NaN); // false
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function isZero(input) {
|
|
138
|
+
return input === 0;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export { isBigInt, isNaN, isNegativeInteger, isNumber, isPositiveInteger, isZero };
|
package/es/isObject.js
CHANGED
|
@@ -1 +1,193 @@
|
|
|
1
|
-
import{typeOf
|
|
1
|
+
import { typeOf } from './typeOf.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测 Javascript 数据类型工具之: object
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* # 检测 `input` 是否是类型 `plain object` (自定义的对象)
|
|
8
|
+
*
|
|
9
|
+
* @param input - 待检测的数据,任意类型
|
|
10
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `object` ,且在 Typescript 中进行类型收缩
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isPlainObject } from '@mudbean/is';
|
|
15
|
+
*
|
|
16
|
+
* console.log(isPlainObject({})); // true
|
|
17
|
+
*
|
|
18
|
+
* // false ( Array is not an plain object )
|
|
19
|
+
* console.log(isPlainObject([]));
|
|
20
|
+
* // false ( Date is not an plain object )
|
|
21
|
+
* console.log(isPlainObject(new Date()));
|
|
22
|
+
* // false ( Map is not an plain object )
|
|
23
|
+
* console.log(isPlainObject(new Map()));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function isPlainObject(input) {
|
|
27
|
+
return typeOf(input) === 'object';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* # 检测 `input` 是否是否是一个没有自己私有键的对象
|
|
31
|
+
*
|
|
32
|
+
* @param input - 待检测的数据,任意类型
|
|
33
|
+
* @returns 是否为空的对象。不包含任何属性,包括 Symbol 或是不可被枚举的属性
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { isEmptyObject } from '@mudbean/is';
|
|
38
|
+
*
|
|
39
|
+
* const a = new Object();
|
|
40
|
+
*
|
|
41
|
+
* console.log(isEmptyObject(a)); // true
|
|
42
|
+
* console.log(Reflect.ownKeys(input).length); // 0
|
|
43
|
+
* console.log(Object.keys(a).length); // 0
|
|
44
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
45
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
46
|
+
*
|
|
47
|
+
* Object.defineProperties(a, {
|
|
48
|
+
* a: {
|
|
49
|
+
* value: 10,
|
|
50
|
+
* enumerable: false,
|
|
51
|
+
* writable: false,
|
|
52
|
+
* configurable: false,
|
|
53
|
+
* },
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* console.log(isEmptyObject(a)); // false
|
|
57
|
+
* console.log(Reflect.ownKeys(input).length); // 1
|
|
58
|
+
* console.log(Object.keys(a).length); // 0
|
|
59
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 1
|
|
60
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
61
|
+
*
|
|
62
|
+
* const b = Symbol('private proto');
|
|
63
|
+
*
|
|
64
|
+
* a[b] = b;
|
|
65
|
+
*
|
|
66
|
+
* console.log(isEmptyObject(a)); // false
|
|
67
|
+
* console.log(Reflect.ownKeys(input).length); // 2
|
|
68
|
+
* console.log(Object.keys(a).length); // 0
|
|
69
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 1
|
|
70
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 1
|
|
71
|
+
*
|
|
72
|
+
* delete a[a];
|
|
73
|
+
*
|
|
74
|
+
* console.log(isEmptyObject(a)); // false
|
|
75
|
+
* console.log(Reflect.ownKeys(input).length); // 1
|
|
76
|
+
* console.log(Object.keys(a).length); // 0
|
|
77
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
78
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 1
|
|
79
|
+
*
|
|
80
|
+
*
|
|
81
|
+
* delete a[b];
|
|
82
|
+
*
|
|
83
|
+
* console.log(isEmptyObject(a)); // true
|
|
84
|
+
* console.log(Reflect.ownKeys(input).length); // 0
|
|
85
|
+
* console.log(Object.keys(a).length); // 0
|
|
86
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
87
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
88
|
+
*
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function isEmptyObject(input) {
|
|
92
|
+
return isPlainObject(input) && Reflect.ownKeys(input).length === 0;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* # 检测 data 是否是 类型{@link Date}
|
|
96
|
+
*
|
|
97
|
+
* @param input - 待检测的数据,任意类型
|
|
98
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Date` ,且在 Typescript 中进行类型收缩
|
|
99
|
+
* @example
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { isDate } from '@mudbean/is';
|
|
103
|
+
*
|
|
104
|
+
* console.log(isDate(new Date())); // true
|
|
105
|
+
* console.log(isDate(new Date(0))); // true
|
|
106
|
+
* console.log(isDate(new Date('2025-03-19'))); // true
|
|
107
|
+
* console.log(isDate(new Date(NaN))); // true
|
|
108
|
+
* console.log(isDate(new Date(undefined))); // true
|
|
109
|
+
* console.log(isDate(new Date(null))); // true
|
|
110
|
+
*
|
|
111
|
+
* // false ( Number 不是 Date )
|
|
112
|
+
* console.log(isDate(1));
|
|
113
|
+
* // false ( String 不是 Date )
|
|
114
|
+
* console.log(isDate('1'));
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function isDate(input) {
|
|
118
|
+
return typeOf(input) === 'date';
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* 检测 `input` 是否是 `DataView` 类型
|
|
123
|
+
*
|
|
124
|
+
* @param input - 待检测的数据,任意类型
|
|
125
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `DataView` ,且在 Typescript 中进行类型收缩
|
|
126
|
+
* @example
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* import { isDataView } from '@mudbean/is';
|
|
130
|
+
*
|
|
131
|
+
* console.log(isDataView(new DataView(new ArrayBuffer(8)))); // true
|
|
132
|
+
*
|
|
133
|
+
* // false (ArrayBuffer 不是 DataView)
|
|
134
|
+
* console.log(isDataView(new ArrayBuffer(8)));
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function isDataView(input) {
|
|
138
|
+
return typeOf(input) === 'dataview';
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
*
|
|
142
|
+
* 检测 `input` 是否是 `Map` 类型
|
|
143
|
+
*
|
|
144
|
+
* @param input - 待检测的数据,任意类型
|
|
145
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Map` ,且在 Typescript 中进行类型收缩
|
|
146
|
+
* @example
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { isMap } from '@mudbean/is';
|
|
150
|
+
*
|
|
151
|
+
* console.log(isMap(new Map())); // true
|
|
152
|
+
*
|
|
153
|
+
* // false (WeakMap 不是 Map)
|
|
154
|
+
* console.log(isMap(new WeakMap()));
|
|
155
|
+
* // false (Set 不是 Map)
|
|
156
|
+
* console.log(isMap(new Set()));
|
|
157
|
+
* // false (WeakSet 不是 Map)
|
|
158
|
+
* console.log(isMap(new WeakSet()));
|
|
159
|
+
* // false (Array 不是 Map)
|
|
160
|
+
* console.log(isMap(new Array()));
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
function isMap(input) {
|
|
164
|
+
return typeOf(input) === 'map';
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* 检测 `input` 是否是 `WeakMap` 类型
|
|
169
|
+
*
|
|
170
|
+
* @param input - 待检测的数据,任意类型
|
|
171
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `WeakMap` ,且在 Typescript 中进行类型收缩
|
|
172
|
+
* @example
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { isWeakMap } from '@mudbean/is';
|
|
176
|
+
*
|
|
177
|
+
* console.log(isWeakMap(new WeakMap())); // true
|
|
178
|
+
*
|
|
179
|
+
* // false (Map is not WeakMap)
|
|
180
|
+
* console.log(isWeakMap(new Map()));
|
|
181
|
+
* // false (Set is not WeakMap)
|
|
182
|
+
* console.log(isWeakMap(new Set()));
|
|
183
|
+
* // false (WeakSet is not WeakMap)
|
|
184
|
+
* console.log(isWeakMap(new WeakSet()));
|
|
185
|
+
* // false (Array is not WeakMap)
|
|
186
|
+
* console.log(isWeakMap(new Array()));
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
function isWeakMap(input) {
|
|
190
|
+
return typeOf(input) === 'weakmap';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { isDataView, isDate, isEmptyObject, isMap, isPlainObject, isWeakMap };
|
package/es/isString.js
CHANGED
|
@@ -1 +1,116 @@
|
|
|
1
|
-
import{typeOf
|
|
1
|
+
import { typeOf } from './typeOf.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 检测 Javascript 数据类型工具之: string
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* # 当前数据类型是否为 string
|
|
8
|
+
*
|
|
9
|
+
* @param input - 待检测的数据,任意类型
|
|
10
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` ,且在 Typescript 中进行类型收缩
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isString } from '@mudbean/is';
|
|
15
|
+
*
|
|
16
|
+
* console.log(isString('hello')); // true
|
|
17
|
+
*
|
|
18
|
+
* // false (number 不是 string)
|
|
19
|
+
* console.log(isString(123));
|
|
20
|
+
* // false (boolean 不是 string)
|
|
21
|
+
* console.log(isString(true));
|
|
22
|
+
* // false (null 不是 string)
|
|
23
|
+
* console.log(isString(null));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function isString(input) {
|
|
27
|
+
return typeOf(input) === 'string';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* # 检测 `input` 是否是 `RegExp` 类型
|
|
31
|
+
*
|
|
32
|
+
* @param input - 待检测的数据,任意类型
|
|
33
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `RegExp` ,且在 Typescript 中进行类型收缩
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { isRegExp } from '@mudbean/is';
|
|
38
|
+
*
|
|
39
|
+
* console.log(isRegExp(/abc/)); // true
|
|
40
|
+
* console.log(isRegExp(new RegExp('abc'))); // true
|
|
41
|
+
*
|
|
42
|
+
* // false (number 不是 RegExp)
|
|
43
|
+
* console.log(isRegExp(123));
|
|
44
|
+
* // false (string 不是 RegExp)
|
|
45
|
+
* console.log(isRegExp('abc'));
|
|
46
|
+
* // false (null 不是 RegExp)
|
|
47
|
+
* console.log(isRegExp(null));
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function isRegExp(input) {
|
|
51
|
+
return typeOf(input) === 'regexp';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* # 检测 `input` 是否是(绝对)空字符串
|
|
55
|
+
*
|
|
56
|
+
* @param input - 待检测的数据,任意类型
|
|
57
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` 且为 空字符串,且在 Typescript 中进行类型收缩
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { isEmptyString } from '@mudbean/is';
|
|
63
|
+
*
|
|
64
|
+
* console.log(isEmptyString('')); // true
|
|
65
|
+
* const.log(isEmptyString(new String())); // true
|
|
66
|
+
* const.log(isEmptyString(new String(''))); // true
|
|
67
|
+
*
|
|
68
|
+
*
|
|
69
|
+
* // 以下情况返回 false
|
|
70
|
+
* console.log(isEmptyString(' ')); // false
|
|
71
|
+
* console.log(isEmptyString('abc')); // false
|
|
72
|
+
* // false (number 不是 string)
|
|
73
|
+
* console.log(isEmptyString(123));
|
|
74
|
+
* // false (boolean 不是 string)
|
|
75
|
+
* console.log(isEmptyString(true));
|
|
76
|
+
* // false (null 不是 string)
|
|
77
|
+
* console.log(isEmptyString(null));
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
function isEmptyString(input) {
|
|
81
|
+
return isString(input) && input.valueOf() === '';
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* # 检测 `input` 是否是(业务)空字符串
|
|
85
|
+
*
|
|
86
|
+
* 业务空字符串:指字符串开头和结尾的空格,以及中间连续的空格,都算作空字符串
|
|
87
|
+
*
|
|
88
|
+
* @param input - 待检测的数据,任意类型
|
|
89
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` 且为 业务空字符串,且在 Typescript 中进行类型收缩
|
|
90
|
+
* @example
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { isBusinessEmptyString } from '@mudbean/is';
|
|
94
|
+
*
|
|
95
|
+
* console.log(isBusinessEmptyString('')); // true
|
|
96
|
+
* console.log(isBusinessEmptyString(' ')); // true
|
|
97
|
+
* console.log(isBusinessEmptyString(' ')); // true
|
|
98
|
+
* console.log(isBusinessEmptyString(new String())); // true
|
|
99
|
+
* console.log(isBusinessEmptyString(new String(''))); // true
|
|
100
|
+
* console.log(isBusinessEmptyString(new String(' '))); // true
|
|
101
|
+
*
|
|
102
|
+
* // 以下情况返回 false
|
|
103
|
+
* console.log(isBusinessEmptyString('abc')); // false
|
|
104
|
+
* // false (number 不是 string)
|
|
105
|
+
* console.log(isBusinessEmptyString(123));
|
|
106
|
+
* // false (boolean 不是 string)
|
|
107
|
+
* console.log(isBusinessEmptyString(true));
|
|
108
|
+
* // false (null 不是 string)
|
|
109
|
+
* console.log(isBusinessEmptyString(null));
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
function isBusinessEmptyString(input) {
|
|
113
|
+
return isString(input) && input.valueOf().trim() === '';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { isBusinessEmptyString, isEmptyString, isRegExp, isString };
|