@mudbean/is 2.0.2
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 +134 -0
- package/LICENSE +16 -0
- package/README.md +136 -0
- package/cjs/error.js +1 -0
- package/cjs/index.js +1 -0
- package/cjs/intl.js +1 -0
- package/cjs/isArray.js +1 -0
- package/cjs/isBoolean.js +1 -0
- package/cjs/isFunction.js +1 -0
- package/cjs/isNull.js +1 -0
- package/cjs/isNumber.js +1 -0
- package/cjs/isObject.js +1 -0
- package/cjs/isString.js +1 -0
- package/cjs/isSymbol.js +1 -0
- package/cjs/isType.js +1 -0
- package/cjs/typeOf.js +1 -0
- package/es/error.js +1 -0
- package/es/index.js +1 -0
- package/es/intl.js +1 -0
- package/es/isArray.js +1 -0
- package/es/isBoolean.js +1 -0
- package/es/isFunction.js +1 -0
- package/es/isNull.js +1 -0
- package/es/isNumber.js +1 -0
- package/es/isObject.js +1 -0
- package/es/isString.js +1 -0
- package/es/isSymbol.js +1 -0
- package/es/isType.js +1 -0
- package/es/typeOf.js +1 -0
- package/es/types/error.d.ts +179 -0
- package/es/types/index.d.ts +13 -0
- package/es/types/intl.d.ts +120 -0
- package/es/types/isArray.d.ts +281 -0
- package/es/types/isBoolean.d.ts +54 -0
- package/es/types/isFunction.d.ts +97 -0
- package/es/types/isNull.d.ts +40 -0
- package/es/types/isNumber.d.ts +125 -0
- package/es/types/isObject.d.ts +174 -0
- package/es/types/isString.d.ts +101 -0
- package/es/types/isSymbol.d.ts +25 -0
- package/es/types/isType.d.ts +77 -0
- package/es/types/typeOf.d.ts +29 -0
- package/es/types/types.d.ts +4 -0
- package/package.json +133 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检测 Javascript 数据类型工具之: number
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* 检测 `input` 是否是 `number` 类型
|
|
7
|
+
*
|
|
8
|
+
* @param input - 待检测的数据,任意类型
|
|
9
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `number` ,且在 Typescript 中进行类型收缩
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { isNumber } from '@mudbean/is';
|
|
14
|
+
*
|
|
15
|
+
* isNumber(123); // true
|
|
16
|
+
*
|
|
17
|
+
* isNumber('123'); // false
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function isNumber(input: any): input is number;
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* 检测 `input` 是否是 `BigInt` 类型
|
|
24
|
+
*
|
|
25
|
+
* @param input - 待检测的数据,任意类型
|
|
26
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `bigint` ,且在 Typescript 中进行类型收缩
|
|
27
|
+
* @example
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { isBigInt } from '@mudbean/is';
|
|
31
|
+
*
|
|
32
|
+
* isBigInt(123n); // true
|
|
33
|
+
*
|
|
34
|
+
* isBigInt('123'); // false
|
|
35
|
+
* isBigInt(123); // false
|
|
36
|
+
* isBigInt(true); // false
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function isBigInt(input: any): input is bigint;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* 检测 `input` 是否是 `NaN` 类型
|
|
43
|
+
*
|
|
44
|
+
* @param input - 待检测的数据,任意类型
|
|
45
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `NaN` ,且在 Typescript 中进行类型收缩
|
|
46
|
+
* @example
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { isNaN } from '@mudbean/is';
|
|
50
|
+
*
|
|
51
|
+
* isNaN(NaN); // true
|
|
52
|
+
*
|
|
53
|
+
* isNaN('123'); // false
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function isNaN(input: any): input is typeof NaN;
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* 是否为正整数
|
|
60
|
+
*
|
|
61
|
+
* @param input - 待检测的数据,任意类型
|
|
62
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为正整数
|
|
63
|
+
* @example
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { isPositiveInteger } from '@mudbean/is';
|
|
67
|
+
*
|
|
68
|
+
* isPositiveInteger(123); // true
|
|
69
|
+
* isPositiveInteger(1); // true
|
|
70
|
+
*
|
|
71
|
+
* isPositiveInteger(0); // false
|
|
72
|
+
* isPositiveInteger(-1); // true
|
|
73
|
+
* isPositiveInteger(false); // false
|
|
74
|
+
* isPositiveInteger(Infinity); // false
|
|
75
|
+
* isPositiveInteger('123'); // false
|
|
76
|
+
* isPositiveInteger(NaN); // false
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function isPositiveInteger(input: any): input is number;
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* 是否为负整数
|
|
83
|
+
*
|
|
84
|
+
* @param input - 待检测的数据,任意类型
|
|
85
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为负整数
|
|
86
|
+
* @example
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { isPositiveInteger } from '@mudbean/is';
|
|
90
|
+
*
|
|
91
|
+
* isPositiveInteger(-123); // true
|
|
92
|
+
* isPositiveInteger(-1); // true
|
|
93
|
+
*
|
|
94
|
+
* isPositiveInteger(0); // false
|
|
95
|
+
* isPositiveInteger(1); // true
|
|
96
|
+
* isPositiveInteger(false); // false
|
|
97
|
+
* isPositiveInteger(Infinity); // false
|
|
98
|
+
* isPositiveInteger('123'); // false
|
|
99
|
+
* isPositiveInteger(NaN); // false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function isNegativeInteger(input: any): input is number;
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* 是否为正整数
|
|
106
|
+
*
|
|
107
|
+
* @param input - 待检测的数据,任意类型
|
|
108
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 0
|
|
109
|
+
* @example
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { isZero } from '@mudbean/is';
|
|
113
|
+
*
|
|
114
|
+
* isZero(0); // true
|
|
115
|
+
*
|
|
116
|
+
*
|
|
117
|
+
* isZero(123); // false
|
|
118
|
+
* isZero(1); // false
|
|
119
|
+
* isZero(false); // false
|
|
120
|
+
* isZero(Infinity); // false
|
|
121
|
+
* isZero('123'); // false
|
|
122
|
+
* isZero(NaN); // false
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function isZero(input: any): input is 0;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 检测 `input` 是否是类型 `plain object` (自定义的对象)
|
|
3
|
+
*
|
|
4
|
+
* @param input - 待检测的数据,任意类型
|
|
5
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `object` ,且在 Typescript 中进行类型收缩
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { isPlainObject } from '@mudbean/is';
|
|
10
|
+
*
|
|
11
|
+
* console.log(isPlainObject({})); // true
|
|
12
|
+
*
|
|
13
|
+
* // false ( Array is not an plain object )
|
|
14
|
+
* console.log(isPlainObject([]));
|
|
15
|
+
* // false ( Date is not an plain object )
|
|
16
|
+
* console.log(isPlainObject(new Date()));
|
|
17
|
+
* // false ( Map is not an plain object )
|
|
18
|
+
* console.log(isPlainObject(new Map()));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function isPlainObject<T extends object>(input: any): input is T;
|
|
22
|
+
/**
|
|
23
|
+
* # 检测 `input` 是否是否是一个没有自己私有键的对象
|
|
24
|
+
*
|
|
25
|
+
* @param input - 待检测的数据,任意类型
|
|
26
|
+
* @returns 是否为空的对象。不包含任何属性,包括 Symbol 或是不可被枚举的属性
|
|
27
|
+
* @example
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { isEmptyObject } from '@mudbean/is';
|
|
31
|
+
*
|
|
32
|
+
* const a = new Object();
|
|
33
|
+
*
|
|
34
|
+
* console.log(isEmptyObject(a)); // true
|
|
35
|
+
* console.log(Reflect.ownKeys(input).length); // 0
|
|
36
|
+
* console.log(Object.keys(a).length); // 0
|
|
37
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
38
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
39
|
+
*
|
|
40
|
+
* Object.defineProperties(a, {
|
|
41
|
+
* a: {
|
|
42
|
+
* value: 10,
|
|
43
|
+
* enumerable: false,
|
|
44
|
+
* writable: false,
|
|
45
|
+
* configurable: false,
|
|
46
|
+
* },
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log(isEmptyObject(a)); // false
|
|
50
|
+
* console.log(Reflect.ownKeys(input).length); // 1
|
|
51
|
+
* console.log(Object.keys(a).length); // 0
|
|
52
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 1
|
|
53
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
54
|
+
*
|
|
55
|
+
* const b = Symbol('private proto');
|
|
56
|
+
*
|
|
57
|
+
* a[b] = b;
|
|
58
|
+
*
|
|
59
|
+
* console.log(isEmptyObject(a)); // false
|
|
60
|
+
* console.log(Reflect.ownKeys(input).length); // 2
|
|
61
|
+
* console.log(Object.keys(a).length); // 0
|
|
62
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 1
|
|
63
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 1
|
|
64
|
+
*
|
|
65
|
+
* delete a[a];
|
|
66
|
+
*
|
|
67
|
+
* console.log(isEmptyObject(a)); // false
|
|
68
|
+
* console.log(Reflect.ownKeys(input).length); // 1
|
|
69
|
+
* console.log(Object.keys(a).length); // 0
|
|
70
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
71
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 1
|
|
72
|
+
*
|
|
73
|
+
*
|
|
74
|
+
* delete a[b];
|
|
75
|
+
*
|
|
76
|
+
* console.log(isEmptyObject(a)); // true
|
|
77
|
+
* console.log(Reflect.ownKeys(input).length); // 0
|
|
78
|
+
* console.log(Object.keys(a).length); // 0
|
|
79
|
+
* console.log(Object.getOwnPropertyNames(a).length); // 0
|
|
80
|
+
* console.log(Object.getOwnPropertySymbols(a).length); // 0
|
|
81
|
+
*
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function isEmptyObject(input: any): input is Record<string, never>;
|
|
85
|
+
/**
|
|
86
|
+
* # 检测 data 是否是 类型{@link Date}
|
|
87
|
+
*
|
|
88
|
+
* @param input - 待检测的数据,任意类型
|
|
89
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Date` ,且在 Typescript 中进行类型收缩
|
|
90
|
+
* @example
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { isDate } from '@mudbean/is';
|
|
94
|
+
*
|
|
95
|
+
* console.log(isDate(new Date())); // true
|
|
96
|
+
* console.log(isDate(new Date(0))); // true
|
|
97
|
+
* console.log(isDate(new Date('2025-03-19'))); // true
|
|
98
|
+
* console.log(isDate(new Date(NaN))); // true
|
|
99
|
+
* console.log(isDate(new Date(undefined))); // true
|
|
100
|
+
* console.log(isDate(new Date(null))); // true
|
|
101
|
+
*
|
|
102
|
+
* // false ( Number 不是 Date )
|
|
103
|
+
* console.log(isDate(1));
|
|
104
|
+
* // false ( String 不是 Date )
|
|
105
|
+
* console.log(isDate('1'));
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function isDate(input: any): input is Date;
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* 检测 `input` 是否是 `DataView` 类型
|
|
112
|
+
*
|
|
113
|
+
* @param input - 待检测的数据,任意类型
|
|
114
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `DataView` ,且在 Typescript 中进行类型收缩
|
|
115
|
+
* @example
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* import { isDataView } from '@mudbean/is';
|
|
119
|
+
*
|
|
120
|
+
* console.log(isDataView(new DataView(new ArrayBuffer(8)))); // true
|
|
121
|
+
*
|
|
122
|
+
* // false (ArrayBuffer 不是 DataView)
|
|
123
|
+
* console.log(isDataView(new ArrayBuffer(8)));
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function isDataView<T extends ArrayBufferLike = ArrayBufferLike>(input: any): input is DataView<T>;
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* 检测 `input` 是否是 `Map` 类型
|
|
130
|
+
*
|
|
131
|
+
* @param input - 待检测的数据,任意类型
|
|
132
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `Map` ,且在 Typescript 中进行类型收缩
|
|
133
|
+
* @example
|
|
134
|
+
*
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { isMap } from '@mudbean/is';
|
|
137
|
+
*
|
|
138
|
+
* console.log(isMap(new Map())); // true
|
|
139
|
+
*
|
|
140
|
+
* // false (WeakMap 不是 Map)
|
|
141
|
+
* console.log(isMap(new WeakMap()));
|
|
142
|
+
* // false (Set 不是 Map)
|
|
143
|
+
* console.log(isMap(new Set()));
|
|
144
|
+
* // false (WeakSet 不是 Map)
|
|
145
|
+
* console.log(isMap(new WeakSet()));
|
|
146
|
+
* // false (Array 不是 Map)
|
|
147
|
+
* console.log(isMap(new Array()));
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function isMap<K extends string, V = any>(input: any): input is Map<K, V>;
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* 检测 `input` 是否是 `WeakMap` 类型
|
|
154
|
+
*
|
|
155
|
+
* @param input - 待检测的数据,任意类型
|
|
156
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `WeakMap` ,且在 Typescript 中进行类型收缩
|
|
157
|
+
* @example
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { isWeakMap } from '@mudbean/is';
|
|
161
|
+
*
|
|
162
|
+
* console.log(isWeakMap(new WeakMap())); // true
|
|
163
|
+
*
|
|
164
|
+
* // false (Map is not WeakMap)
|
|
165
|
+
* console.log(isWeakMap(new Map()));
|
|
166
|
+
* // false (Set is not WeakMap)
|
|
167
|
+
* console.log(isWeakMap(new Set()));
|
|
168
|
+
* // false (WeakSet is not WeakMap)
|
|
169
|
+
* console.log(isWeakMap(new WeakSet()));
|
|
170
|
+
* // false (Array is not WeakMap)
|
|
171
|
+
* console.log(isWeakMap(new Array()));
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export declare function isWeakMap(input: any): input is WeakMap<object, any>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 当前数据类型是否为 string
|
|
3
|
+
*
|
|
4
|
+
* @param input - 待检测的数据,任意类型
|
|
5
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` ,且在 Typescript 中进行类型收缩
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { isString } from '@mudbean/is';
|
|
10
|
+
*
|
|
11
|
+
* console.log(isString('hello')); // true
|
|
12
|
+
*
|
|
13
|
+
* // false (number 不是 string)
|
|
14
|
+
* console.log(isString(123));
|
|
15
|
+
* // false (boolean 不是 string)
|
|
16
|
+
* console.log(isString(true));
|
|
17
|
+
* // false (null 不是 string)
|
|
18
|
+
* console.log(isString(null));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function isString(input: any): input is string;
|
|
22
|
+
/**
|
|
23
|
+
* # 检测 `input` 是否是 `RegExp` 类型
|
|
24
|
+
*
|
|
25
|
+
* @param input - 待检测的数据,任意类型
|
|
26
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `RegExp` ,且在 Typescript 中进行类型收缩
|
|
27
|
+
* @example
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { isRegExp } from '@mudbean/is';
|
|
31
|
+
*
|
|
32
|
+
* console.log(isRegExp(/abc/)); // true
|
|
33
|
+
* console.log(isRegExp(new RegExp('abc'))); // true
|
|
34
|
+
*
|
|
35
|
+
* // false (number 不是 RegExp)
|
|
36
|
+
* console.log(isRegExp(123));
|
|
37
|
+
* // false (string 不是 RegExp)
|
|
38
|
+
* console.log(isRegExp('abc'));
|
|
39
|
+
* // false (null 不是 RegExp)
|
|
40
|
+
* console.log(isRegExp(null));
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function isRegExp(input: any): input is RegExp;
|
|
44
|
+
/**
|
|
45
|
+
* # 检测 `input` 是否是(绝对)空字符串
|
|
46
|
+
*
|
|
47
|
+
* @param input - 待检测的数据,任意类型
|
|
48
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` 且为 空字符串,且在 Typescript 中进行类型收缩
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { isEmptyString } from '@mudbean/is';
|
|
54
|
+
*
|
|
55
|
+
* console.log(isEmptyString('')); // true
|
|
56
|
+
* const.log(isEmptyString(new String())); // true
|
|
57
|
+
* const.log(isEmptyString(new String(''))); // true
|
|
58
|
+
*
|
|
59
|
+
*
|
|
60
|
+
* // 以下情况返回 false
|
|
61
|
+
* console.log(isEmptyString(' ')); // false
|
|
62
|
+
* console.log(isEmptyString('abc')); // false
|
|
63
|
+
* // false (number 不是 string)
|
|
64
|
+
* console.log(isEmptyString(123));
|
|
65
|
+
* // false (boolean 不是 string)
|
|
66
|
+
* console.log(isEmptyString(true));
|
|
67
|
+
* // false (null 不是 string)
|
|
68
|
+
* console.log(isEmptyString(null));
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function isEmptyString(input: any): input is '';
|
|
72
|
+
/**
|
|
73
|
+
* # 检测 `input` 是否是(业务)空字符串
|
|
74
|
+
*
|
|
75
|
+
* 业务空字符串:指字符串开头和结尾的空格,以及中间连续的空格,都算作空字符串
|
|
76
|
+
*
|
|
77
|
+
* @param input - 待检测的数据,任意类型
|
|
78
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `string` 且为 业务空字符串,且在 Typescript 中进行类型收缩
|
|
79
|
+
* @example
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { isBusinessEmptyString } from '@mudbean/is';
|
|
83
|
+
*
|
|
84
|
+
* console.log(isBusinessEmptyString('')); // true
|
|
85
|
+
* console.log(isBusinessEmptyString(' ')); // true
|
|
86
|
+
* console.log(isBusinessEmptyString(' ')); // true
|
|
87
|
+
* console.log(isBusinessEmptyString(new String())); // true
|
|
88
|
+
* console.log(isBusinessEmptyString(new String(''))); // true
|
|
89
|
+
* console.log(isBusinessEmptyString(new String(' '))); // true
|
|
90
|
+
*
|
|
91
|
+
* // 以下情况返回 false
|
|
92
|
+
* console.log(isBusinessEmptyString('abc')); // false
|
|
93
|
+
* // false (number 不是 string)
|
|
94
|
+
* console.log(isBusinessEmptyString(123));
|
|
95
|
+
* // false (boolean 不是 string)
|
|
96
|
+
* console.log(isBusinessEmptyString(true));
|
|
97
|
+
* // false (null 不是 string)
|
|
98
|
+
* console.log(isBusinessEmptyString(null));
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare function isBusinessEmptyString(input: any): input is '';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 检测 `input` 是否是 `Symbol` 类型
|
|
3
|
+
*
|
|
4
|
+
* @param input - 待检测的数据,任意类型
|
|
5
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `symbol` ,且在 Typescript 中进行类型收缩
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { isSymbol } from '@mudbean/is';
|
|
10
|
+
*
|
|
11
|
+
* console.log(isSymbol(Symbol('test'))); // true
|
|
12
|
+
*
|
|
13
|
+
* // false (string 非 symbol)
|
|
14
|
+
* console.log(isSymbol('test'));
|
|
15
|
+
* // false (number 非 symbol)
|
|
16
|
+
* console.log(isSymbol(123));
|
|
17
|
+
* // false (boolean 非 symbol)
|
|
18
|
+
* console.log(isSymbol(true));
|
|
19
|
+
* // false (null 非 symbol)
|
|
20
|
+
* console.log(isSymbol(null));
|
|
21
|
+
* // false (undefined 非 symbol)
|
|
22
|
+
* console.log(isSymbol(undefined));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function isSymbol(input: any): input is symbol;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `input` 类型收缩
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* # 给出一个类型 `T` ,以返回值真假来判断 `input` 是否为 `T` 类型
|
|
6
|
+
*
|
|
7
|
+
* @param input 待检测的数据
|
|
8
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `ReferenceError` ,且在 Typescript 中进行类型收缩
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { isType } from '@mudbean/is';
|
|
13
|
+
*
|
|
14
|
+
* console.log(isType<number>(1)); // true
|
|
15
|
+
* console.log(isType<string>('1')); // true
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function isType<T = string>(input: any): input is T;
|
|
19
|
+
/**
|
|
20
|
+
* # 给出一个类型 `T` ,以返回值真假来判断 `input` 是否为 `T` 类型
|
|
21
|
+
*
|
|
22
|
+
* @param input 待检测的数据
|
|
23
|
+
* @param judgingConditions 返回 boolean 来反向确认该值的类型
|
|
24
|
+
* @returns 返回 `true` 则说明该数据 `input` 类型为 `ReferenceError` ,且在 Typescript 中进行类型收缩
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { isType } from '@mudbean/is';
|
|
29
|
+
*
|
|
30
|
+
* console.log(isType<number>(1, true)); // true
|
|
31
|
+
* console.log(isType<number>(1, () => true)); // true
|
|
32
|
+
* console.log(isType<string>(1, false)); // false
|
|
33
|
+
* console.log(isType<string>(1, () => false)); // false
|
|
34
|
+
*
|
|
35
|
+
* console.log(isType<string>('1')); // true
|
|
36
|
+
* console.log(isType<string>('1', true)); // true
|
|
37
|
+
* console.log(isType<string>('1', () => true)); // true
|
|
38
|
+
*
|
|
39
|
+
* console.log(isType<number>('1', false)); // false
|
|
40
|
+
* console.log(isType<number>('1', () => false)); // false
|
|
41
|
+
* // 下面的例子尽然有些画蛇添足,但是在实际使用的时候却是方便的很
|
|
42
|
+
* console.log(isType<number>(1, (input) => typeof input === 'number' )); // true
|
|
43
|
+
*
|
|
44
|
+
* // 譬如 (下面的用法 (需求出自于 \@qqi/table ))
|
|
45
|
+
*
|
|
46
|
+
* type BorderStyle = "dash" | "bold" | "double" | "simple";
|
|
47
|
+
*
|
|
48
|
+
* type UnilateralBorder = {
|
|
49
|
+
* color: string | undefined;
|
|
50
|
+
* style: BorderStyle
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* type Border = {
|
|
54
|
+
* left: UnilateralBorder;
|
|
55
|
+
* right: UnilateralBorder;
|
|
56
|
+
* top: UnilateralBorder;
|
|
57
|
+
* bottom: UnilateralBorder;
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
*
|
|
61
|
+
* function fn (border: BorderStyle | UnilateralBorder | Border) {
|
|
62
|
+
* if (isType<BorderStyle>(border, input => isString(input))) {
|
|
63
|
+
* // 然后这里 `border` 就可以当 `BorderStyle` 使用了
|
|
64
|
+
* // 而不是每一次使用都添加上 `as BorderStyle`
|
|
65
|
+
* } else if (
|
|
66
|
+
* isType<UnilateralBorder>(border, input =>
|
|
67
|
+
* isString(input.color) || isString(input.style))) {
|
|
68
|
+
* // 然后这里 `border` 就可以当 `UnilateralBorder` 使用了
|
|
69
|
+
* // 而不是每一次使用都添加上 `as UnilateralBorder`
|
|
70
|
+
* // 更关键的是上面的判断区域中不会报错,即 `input` 类型也是 `UnilateralBorder`
|
|
71
|
+
*
|
|
72
|
+
* }
|
|
73
|
+
* ...
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function isType<T = string>(input: any, judgingConditions: boolean | ((input: T) => boolean)): input is T;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检测 Javascript 数据类型工具
|
|
3
|
+
*/
|
|
4
|
+
import { Typeof } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* # 数据类型检测
|
|
7
|
+
*
|
|
8
|
+
* @param input - 待检测的数据,任意类型
|
|
9
|
+
* @returns 检测数据类型的字符串表示(小写字母)
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { typeOf } from '@mudbean/is';
|
|
13
|
+
*
|
|
14
|
+
* console.log(typeOf(1) === 'number'); // true
|
|
15
|
+
* console.log(typeOf('1') === 'string'); // true
|
|
16
|
+
* console.log(typeOf(null) === 'null'); // true
|
|
17
|
+
* console.log(typeOf(undefined) === 'undefined'); // true
|
|
18
|
+
* console.log(typeOf(true) === 'boolean'); // true
|
|
19
|
+
* console.log(typeOf(Symbol()) === 'symbol');
|
|
20
|
+
* console.log(typeOf(new Date()) === 'date');
|
|
21
|
+
* console.log(typeOf([]) === 'array');
|
|
22
|
+
* console.log(typeOf(new Set()) === 'set');
|
|
23
|
+
* console.log(typeOf(new Map()) === 'map');
|
|
24
|
+
* console.log(typeOf(new Error()) === 'error');
|
|
25
|
+
* console.log(typeOf(new Promise(() => {})) === 'promise');
|
|
26
|
+
* ...
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function typeOf(input: any): Typeof;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 支持的类型值
|
|
3
|
+
*/
|
|
4
|
+
export type Typeof = 'number' | 'function' | 'string' | 'boolean' | 'object' | 'undefined' | 'null' | 'array' | 'date' | 'set' | 'map' | 'symbol' | 'bigint' | 'bigint64array' | 'biguint64array' | 'regexp' | 'int8array' | 'uint8array' | 'int16array' | 'uint16array' | 'float32array' | 'float64array' | 'int32array' | 'uint32array' | 'weakmap' | 'weakset' | 'generatorfunction' | 'generator' | 'error' | 'evalerror' | 'rangeerror' | 'referenceerror' | 'syntaxerror' | 'typeerror' | 'urierror' | 'aggregateerror' | 'uint8clampedarray' | 'sharedarraybuffer' | 'promise' | 'asyncfunction' | 'dataview' | 'arraybuffer' | 'audiobuffer' | 'intl.collator' | 'intl.datetimeformat' | 'intl.displaynames' | 'intl.listformat' | 'intl.locale' | 'intl.numberformat' | 'intl.pluralrules' | 'intl.relativetimeformat' | 'intl.segmenter';
|
package/package.json
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mudbean/is",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "2.0.2",
|
|
5
|
+
"main": "cjs/index.js",
|
|
6
|
+
"module": "es/index.js",
|
|
7
|
+
"types": "es/types/index.d.ts",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "泥豆君",
|
|
10
|
+
"email": "Mr.MudBean@outlook.com",
|
|
11
|
+
"url": "https://lmssee.com"
|
|
12
|
+
},
|
|
13
|
+
"description": "JavaScript/TypeScript 的类型检测工具,支持 TypeScript 类型收缩",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=14.0.0"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"files": [
|
|
20
|
+
"cjs",
|
|
21
|
+
"es",
|
|
22
|
+
"es/types",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./es/index.js",
|
|
30
|
+
"default": "./es/index.js",
|
|
31
|
+
"require": "./cjs/index.js",
|
|
32
|
+
"types": "./es/types/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./error": {
|
|
35
|
+
"default": "./es/error.js",
|
|
36
|
+
"import": "./es/error.js",
|
|
37
|
+
"require": "./cjs/error.js",
|
|
38
|
+
"types": "./es/types/error.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./index": {
|
|
41
|
+
"default": "./es/index.js",
|
|
42
|
+
"import": "./es/index.js",
|
|
43
|
+
"require": "./cjs/index.js",
|
|
44
|
+
"types": "./es/types/index.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./intl": {
|
|
47
|
+
"default": "./es/intl.js",
|
|
48
|
+
"import": "./es/intl.js",
|
|
49
|
+
"require": "./cjs/intl.js",
|
|
50
|
+
"types": "./es/types/intl.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./isArray": {
|
|
53
|
+
"default": "./es/isArray.js",
|
|
54
|
+
"import": "./es/isArray.js",
|
|
55
|
+
"require": "./cjs/isArray.js",
|
|
56
|
+
"types": "./es/types/isArray.d.ts"
|
|
57
|
+
},
|
|
58
|
+
"./isBoolean": {
|
|
59
|
+
"default": "./es/isBoolean.js",
|
|
60
|
+
"import": "./es/isBoolean.js",
|
|
61
|
+
"require": "./cjs/isBoolean.js",
|
|
62
|
+
"types": "./es/types/isBoolean.d.ts"
|
|
63
|
+
},
|
|
64
|
+
"./isFunction": {
|
|
65
|
+
"default": "./es/isFunction.js",
|
|
66
|
+
"import": "./es/isFunction.js",
|
|
67
|
+
"require": "./cjs/isFunction.js",
|
|
68
|
+
"types": "./es/types/isFunction.d.ts"
|
|
69
|
+
},
|
|
70
|
+
"./isNull": {
|
|
71
|
+
"default": "./es/isNull.js",
|
|
72
|
+
"import": "./es/isNull.js",
|
|
73
|
+
"require": "./cjs/isNull.js",
|
|
74
|
+
"types": "./es/types/isNull.d.ts"
|
|
75
|
+
},
|
|
76
|
+
"./isNumber": {
|
|
77
|
+
"default": "./es/isNumber.js",
|
|
78
|
+
"import": "./es/isNumber.js",
|
|
79
|
+
"require": "./cjs/isNumber.js",
|
|
80
|
+
"types": "./es/types/isNumber.d.ts"
|
|
81
|
+
},
|
|
82
|
+
"./isObject": {
|
|
83
|
+
"default": "./es/isObject.js",
|
|
84
|
+
"import": "./es/isObject.js",
|
|
85
|
+
"require": "./cjs/isObject.js",
|
|
86
|
+
"types": "./es/types/isObject.d.ts"
|
|
87
|
+
},
|
|
88
|
+
"./isString": {
|
|
89
|
+
"default": "./es/isString.js",
|
|
90
|
+
"import": "./es/isString.js",
|
|
91
|
+
"require": "./cjs/isString.js",
|
|
92
|
+
"types": "./es/types/isString.d.ts"
|
|
93
|
+
},
|
|
94
|
+
"./isSymbol": {
|
|
95
|
+
"default": "./es/isSymbol.js",
|
|
96
|
+
"import": "./es/isSymbol.js",
|
|
97
|
+
"require": "./cjs/isSymbol.js",
|
|
98
|
+
"types": "./es/types/isSymbol.d.ts"
|
|
99
|
+
},
|
|
100
|
+
"./isType": {
|
|
101
|
+
"default": "./es/isType.js",
|
|
102
|
+
"import": "./es/isType.js",
|
|
103
|
+
"require": "./cjs/isType.js",
|
|
104
|
+
"types": "./es/types/isType.d.ts"
|
|
105
|
+
},
|
|
106
|
+
"./typeOf": {
|
|
107
|
+
"default": "./es/typeOf.js",
|
|
108
|
+
"import": "./es/typeOf.js",
|
|
109
|
+
"require": "./cjs/typeOf.js",
|
|
110
|
+
"types": "./es/types/typeOf.d.ts"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"keywords": [
|
|
114
|
+
"is",
|
|
115
|
+
"mudbean",
|
|
116
|
+
"typeof",
|
|
117
|
+
"javascript type",
|
|
118
|
+
"类型检测"
|
|
119
|
+
],
|
|
120
|
+
"homepage": "https://npm.lmssee.com/is",
|
|
121
|
+
"bugs": {
|
|
122
|
+
"url": "https://github.com/MrMudBean/is/issues",
|
|
123
|
+
"email": "Mr.MudBean@outlook.com"
|
|
124
|
+
},
|
|
125
|
+
"repository": {
|
|
126
|
+
"type": "git",
|
|
127
|
+
"url": "git+https://github.com/MrMudBean/is.git"
|
|
128
|
+
},
|
|
129
|
+
"publishConfig": {
|
|
130
|
+
"access": "public",
|
|
131
|
+
"registry": "https://registry.npmjs.org/"
|
|
132
|
+
}
|
|
133
|
+
}
|