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