@pawover/kit 0.0.0-alpha.24 → 0.0.0-alpha.26

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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/utils/typeof/types.ts
2
- const prototypeStrings = {
2
+ const PROTOTYPE_TAGS = {
3
3
  string: "[object String]",
4
4
  number: "[object Number]",
5
5
  boolean: "[object Boolean]",
@@ -8,8 +8,9 @@ const prototypeStrings = {
8
8
  bigInt: "[object BigInt]",
9
9
  symbol: "[object Symbol]",
10
10
  function: "[object Function]",
11
- generatorFunction: "[object GeneratorFunction]",
12
11
  asyncFunction: "[object AsyncFunction]",
12
+ generatorFunction: "[object GeneratorFunction]",
13
+ asyncGeneratorFunction: "[object AsyncGeneratorFunction]",
13
14
  promise: "[object Promise]",
14
15
  null: "[object Null]",
15
16
  undefined: "[object Undefined]",
@@ -23,8 +24,22 @@ const prototypeStrings = {
23
24
  weakSet: "[object WeakSet]",
24
25
  window: "[object Window]",
25
26
  webSocket: "[object WebSocket]",
26
- URLSearchParams: "[object URLSearchParams]"
27
+ URLSearchParams: "[object URLSearchParams]",
28
+ blob: "[object Blob]"
27
29
  };
30
+ const TYPED_ARRAY_TAGS = new Set([
31
+ "[object Int8Array]",
32
+ "[object Uint8Array]",
33
+ "[object Uint8ClampedArray]",
34
+ "[object Int16Array]",
35
+ "[object Uint16Array]",
36
+ "[object Int32Array]",
37
+ "[object Uint32Array]",
38
+ "[object Float32Array]",
39
+ "[object Float64Array]",
40
+ "[object BigInt64Array]",
41
+ "[object BigUint64Array]"
42
+ ]);
28
43
  function resolvePrototypeString(value) {
29
44
  return Object.prototype.toString.call(value);
30
45
  }
@@ -32,43 +47,73 @@ function resolvePrototypeString(value) {
32
47
  //#endregion
33
48
  //#region src/utils/typeof/isArray.ts
34
49
  function isArray(value) {
35
- return resolvePrototypeString(value) === prototypeStrings.array;
50
+ return Array.isArray(value);
36
51
  }
37
-
38
- //#endregion
39
- //#region src/utils/typeof/isAsyncFunction.ts
40
- function isAsyncFunction(value) {
41
- return resolvePrototypeString(value) === prototypeStrings.asyncFunction;
52
+ function isTypedArray(value) {
53
+ return typeof value === "object" && value !== null && TYPED_ARRAY_TAGS.has(resolvePrototypeString(value));
42
54
  }
43
55
 
44
56
  //#endregion
45
57
  //#region src/utils/typeof/isBigInt.ts
46
58
  function isBigInt(value) {
47
- return resolvePrototypeString(value) === prototypeStrings.bigInt;
59
+ return typeof value === "bigint";
60
+ }
61
+
62
+ //#endregion
63
+ //#region src/utils/typeof/isBlob.ts
64
+ function isBlob(value) {
65
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.blob;
48
66
  }
49
67
 
50
68
  //#endregion
51
69
  //#region src/utils/typeof/isBoolean.ts
52
70
  function isBoolean(value) {
53
- return resolvePrototypeString(value) === prototypeStrings.boolean;
71
+ return typeof value === "boolean";
54
72
  }
55
73
 
56
74
  //#endregion
57
75
  //#region src/utils/typeof/isClass.ts
76
+ function isConstructable(fn) {
77
+ try {
78
+ Reflect.construct(fn, []);
79
+ return true;
80
+ } catch {
81
+ return false;
82
+ }
83
+ }
58
84
  function isClass(value) {
59
- return resolvePrototypeString(value).startsWith("class ");
85
+ return isFunction(value) && !isAsyncFunction(value) && Function.prototype.toString.call(value).startsWith("class ") && isConstructable(value) && value.prototype !== void 0;
86
+ }
87
+
88
+ //#endregion
89
+ //#region src/utils/typeof/isObject.ts
90
+ /**
91
+ * 判断是否为对象类型
92
+ * - 可选是否检查原型为 `Object.prototype`,防止原型链污染
93
+ *
94
+ * @param value - 待检查值
95
+ * @param prototypeCheck - 是否进行原型检查,默认 `true`
96
+ */
97
+ function isObject(value, prototypeCheck = true) {
98
+ const check = resolvePrototypeString(value) === PROTOTYPE_TAGS.object;
99
+ return prototypeCheck ? check && Object.getPrototypeOf(value) === Object.prototype : check;
60
100
  }
61
101
 
62
102
  //#endregion
63
103
  //#region src/utils/typeof/isDate.ts
64
104
  function isDate(value) {
65
- return resolvePrototypeString(value) === prototypeStrings.date;
105
+ if (!isObject(value)) return false;
106
+ try {
107
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.date && typeof value["getTime"] === "function";
108
+ } catch (error) {
109
+ return false;
110
+ }
66
111
  }
67
112
 
68
113
  //#endregion
69
114
  //#region src/utils/typeof/isEqual.ts
70
115
  /**
71
- * 判断给定的值是否相等
116
+ * 检查给定的值是否相等
72
117
  * @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
73
118
  *
74
119
  * @param {T} x
@@ -76,8 +121,8 @@ function isDate(value) {
76
121
  */
77
122
  function isEqual(x, y) {
78
123
  if (Object.is(x, y)) return true;
79
- if (x instanceof Date && y instanceof Date) return x.getTime() === y.getTime();
80
- if (x instanceof RegExp && y instanceof RegExp) return x.toString() === y.toString();
124
+ if (isDate(x) && isDate(y)) return x.getTime() === y.getTime();
125
+ if (isRegExp(x) && isRegExp(y)) return x.toString() === y.toString();
81
126
  if (typeof x !== "object" || x === null || typeof y !== "object" || y === null) return false;
82
127
  const keysX = Reflect.ownKeys(x);
83
128
  const keysY = Reflect.ownKeys(y);
@@ -92,92 +137,39 @@ function isEqual(x, y) {
92
137
  //#endregion
93
138
  //#region src/utils/typeof/isError.ts
94
139
  function isError(value) {
95
- return resolvePrototypeString(value) === prototypeStrings.error;
96
- }
97
-
98
- //#endregion
99
- //#region src/utils/typeof/isNaN.ts
100
- function isNaN(value) {
101
- return Number.isNaN(value);
140
+ return value instanceof Error || resolvePrototypeString(value) === PROTOTYPE_TAGS.error;
102
141
  }
103
142
 
104
143
  //#endregion
105
144
  //#region src/utils/typeof/isFalsy.ts
106
145
  function isFalsy(value) {
107
- if (isNaN(value)) return true;
108
- return [
109
- null,
110
- void 0,
111
- false,
112
- 0,
113
- 0n,
114
- ""
115
- ].includes(value);
146
+ if (isNaN(value) || isNull(value) || isUndefined(value)) return true;
147
+ return value === false || value === 0 || value === 0n || value === "";
116
148
  }
117
149
  function isFalsyLike(value) {
118
- return [
119
- "null",
120
- "undefined",
121
- "false",
122
- "NaN"
123
- ].includes(value);
150
+ if (isFalsy(value)) return true;
151
+ return typeof value === "string" && (value === "null" || value === "undefined" || value === "NaN" || value === "false" || value === "0" || value === "0n");
124
152
  }
125
153
 
126
154
  //#endregion
127
155
  //#region src/utils/typeof/isFile.ts
128
156
  function isFile(value) {
129
- return resolvePrototypeString(value) === prototypeStrings.file;
157
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.file;
130
158
  }
131
159
 
132
160
  //#endregion
133
161
  //#region src/utils/typeof/isFunction.ts
134
162
  function isFunction(value) {
135
- return [
136
- prototypeStrings.function,
137
- prototypeStrings.generatorFunction,
138
- prototypeStrings.asyncFunction
139
- ].includes(resolvePrototypeString(value));
140
- }
141
-
142
- //#endregion
143
- //#region src/utils/typeof/isGeneratorFunction.ts
144
- function isGeneratorFunction(value) {
145
- return resolvePrototypeString(value) === prototypeStrings.generatorFunction;
146
- }
147
-
148
- //#endregion
149
- //#region src/utils/typeof/isInfinity.ts
150
- function isInfinity(value) {
151
- if (isNaN(value)) return true;
152
- return [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].includes(value);
163
+ return typeof value === "function";
153
164
  }
154
- function isInfinityLike(value) {
155
- return [
156
- "Infinity",
157
- "-Infinity",
158
- "NaN"
159
- ].includes(value);
165
+ function isAsyncFunction(value) {
166
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncFunction;
160
167
  }
161
-
162
- //#endregion
163
- //#region src/utils/typeof/isInteger.ts
164
- function isInteger(value) {
165
- return Number.isInteger(value);
168
+ function isGeneratorFunction(value) {
169
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.generatorFunction;
166
170
  }
167
-
168
- //#endregion
169
- //#region src/utils/typeof/isObject.ts
170
- /**
171
- * 判断是否为对象类型
172
- * - 可选是否检查原型为 `Object.prototype`,防止原型链污染
173
- *
174
- * @param value - 待检查值
175
- * @param prototypeCheck - 是否进行原型检查,默认 `true`
176
- */
177
- function isObject(value, prototypeCheck = true) {
178
- const checkValue = resolvePrototypeString(value) === prototypeStrings.object;
179
- if (prototypeCheck && checkValue) return Object.getPrototypeOf(value) === Object.prototype;
180
- else return checkValue;
171
+ function isAsyncGeneratorFunction(value) {
172
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncGeneratorFunction;
181
173
  }
182
174
 
183
175
  //#endregion
@@ -189,29 +181,71 @@ function isIterable(value) {
189
181
  //#endregion
190
182
  //#region src/utils/typeof/isMap.ts
191
183
  function isMap(value) {
192
- return resolvePrototypeString(value) === prototypeStrings.map;
184
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.map;
185
+ }
186
+ function isWeakMap(value) {
187
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakMap;
188
+ }
189
+
190
+ //#endregion
191
+ //#region src/utils/typeof/isNaN.ts
192
+ function isNaN(value) {
193
+ return Number.isNaN(value);
193
194
  }
194
195
 
195
196
  //#endregion
196
197
  //#region src/utils/typeof/isNull.ts
197
198
  function isNull(value) {
198
- return resolvePrototypeString(value) === prototypeStrings.null;
199
+ return value === null;
199
200
  }
200
201
 
201
202
  //#endregion
202
203
  //#region src/utils/typeof/isNumber.ts
203
204
  function isNumber(value) {
204
- return resolvePrototypeString(value) === prototypeStrings.number;
205
+ return typeof value === "number";
206
+ }
207
+ /**
208
+ * 检查 value 是否为整数
209
+ *
210
+ * @param value - 待检查值
211
+ * @param safeCheck - 是否附加安全数检查
212
+ */
213
+ function isInteger(value, safeCheck = true) {
214
+ const check = Number.isInteger(value);
215
+ return safeCheck ? check && Number.isSafeInteger(value) : check;
216
+ }
217
+ /**
218
+ * 检查 value 是否为正整数
219
+ * - 未考虑 value 为 0 的情况
220
+ *
221
+ * @param value - 待检查值
222
+ * @param safeCheck - 是否附加安全数检查
223
+ */
224
+ function isPositiveInteger(value, safeCheck = true) {
225
+ return isInteger(value, safeCheck) && value > 0;
226
+ }
227
+ /**
228
+ * 检查 value 是否为负整数
229
+ * - 未考虑 value 为 0 的情况
230
+ *
231
+ * @param value - 待检查值
232
+ * @param safeCheck - 是否附加安全数检查
233
+ */
234
+ function isNegativeInteger(value, safeCheck = true) {
235
+ return isInteger(value, safeCheck) && value < 0;
236
+ }
237
+ function isInfinity(value) {
238
+ return isNumber(value) && (Number.POSITIVE_INFINITY === value || Number.NEGATIVE_INFINITY === value);
239
+ }
240
+ function isInfinityLike(value) {
241
+ return isInfinity(value) && typeof value === "string" && (value === "Infinity" || value === "-Infinity" || value === "+Infinity");
205
242
  }
206
243
 
207
244
  //#endregion
208
245
  //#region src/utils/typeof/isPromise.ts
209
246
  function isPromise(value) {
210
- return resolvePrototypeString(value) === prototypeStrings.promise;
247
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.promise;
211
248
  }
212
-
213
- //#endregion
214
- //#region src/utils/typeof/isPromiseLike.ts
215
249
  function isPromiseLike(value) {
216
250
  return isPromise(value) || isObject(value) && isFunction(value["then"]);
217
251
  }
@@ -219,61 +253,58 @@ function isPromiseLike(value) {
219
253
  //#endregion
220
254
  //#region src/utils/typeof/isRegExp.ts
221
255
  function isRegExp(value) {
222
- return resolvePrototypeString(value) === prototypeStrings.regExp;
256
+ if (!isObject(value)) return false;
257
+ try {
258
+ const regex = value;
259
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.regExp && isString(regex.source) && isString(regex.flags) && isBoolean(regex.global) && isFunction(regex.test);
260
+ } catch (error) {
261
+ return false;
262
+ }
223
263
  }
224
264
 
225
265
  //#endregion
226
266
  //#region src/utils/typeof/isSet.ts
227
267
  function isSet(value) {
228
- return resolvePrototypeString(value) === prototypeStrings.set;
268
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.set;
269
+ }
270
+ function isWeakSet(value) {
271
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakSet;
229
272
  }
230
273
 
231
274
  //#endregion
232
275
  //#region src/utils/typeof/isString.ts
233
276
  function isString(value) {
234
- return resolvePrototypeString(value) === prototypeStrings.string;
277
+ return typeof value === "string";
235
278
  }
236
279
 
237
280
  //#endregion
238
281
  //#region src/utils/typeof/isSymbol.ts
239
282
  function isSymbol(value) {
240
- return resolvePrototypeString(value) === prototypeStrings.symbol;
283
+ return typeof value === "symbol";
241
284
  }
242
285
 
243
286
  //#endregion
244
287
  //#region src/utils/typeof/isUndefined.ts
245
288
  function isUndefined(value) {
246
- return resolvePrototypeString(value) === prototypeStrings.undefined;
289
+ return typeof value === "undefined";
247
290
  }
248
291
 
249
292
  //#endregion
250
293
  //#region src/utils/typeof/isURLSearchParams.ts
251
294
  function isURLSearchParams(value) {
252
- return resolvePrototypeString(value) === prototypeStrings.URLSearchParams;
253
- }
254
-
255
- //#endregion
256
- //#region src/utils/typeof/isWeakMap.ts
257
- function isWeakMap(value) {
258
- return resolvePrototypeString(value) === prototypeStrings.weakMap;
259
- }
260
-
261
- //#endregion
262
- //#region src/utils/typeof/isWeakSet.ts
263
- function isWeakSet(value) {
264
- return resolvePrototypeString(value) === prototypeStrings.weakSet;
295
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.URLSearchParams;
265
296
  }
266
297
 
267
298
  //#endregion
268
299
  //#region src/utils/typeof/isWebSocket.ts
269
300
  function isWebSocket(value) {
270
- return resolvePrototypeString(value) === prototypeStrings.webSocket;
301
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.webSocket;
271
302
  }
272
303
 
273
304
  //#endregion
274
305
  //#region src/utils/typeof/isWindow.ts
275
306
  function isWindow(value) {
276
- return resolvePrototypeString(value) === prototypeStrings.window;
307
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.window;
277
308
  }
278
309
 
279
310
  //#endregion
@@ -552,7 +583,7 @@ function stringToNumber(value) {
552
583
  */
553
584
  function toMathBignumber(mathJsInstance, value, saveValue) {
554
585
  const errorValue = saveValue ?? mathJsInstance.bignumber(0);
555
- if (isFalsy(value) || isFalsyLike(value) || isInfinity(value) || isInfinityLike(value)) return errorValue;
586
+ if (isFalsyLike(value) || isInfinityLike(value)) return errorValue;
556
587
  try {
557
588
  return mathJsInstance.bignumber(stringToNumber(`${value}`));
558
589
  } catch (error) {
@@ -1243,5 +1274,5 @@ function treeToRows(tree, options = {}) {
1243
1274
  }
1244
1275
 
1245
1276
  //#endregion
1246
- export { arrayCast, arrayCompete, arrayCounting, arrayDifference, arrayFirst, arrayFork, arrayIntersection, arrayLast, arrayMerge, arrayPick, arrayReplace, arraySplit, cloneDeep, enumEntries, enumKeys, enumTypeCheck, enumValues, isArray, isAsyncFunction, isBigInt, isBoolean, isClass, isDate, isEqual, isError, isFalsy, isFalsyLike, isFile, isFunction, isGeneratorFunction, isInfinity, isInfinityLike, isInteger, isIterable, isMap, isNaN, isNull, isNumber, isObject, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isURLSearchParams, isUndefined, isWeakMap, isWeakSet, isWebSocket, isWindow, mapEntries, objectAssign, objectEntries, objectKeys, objectPick, objectSwitch, objectValues, rowsToTree, stringInitialCase, stringReplace, stringToJson, stringToNumber, stringToValues, to, toMathBignumber, toMathDecimal, toMathEvaluate, treeFilter, treeFind, treeForEach, treeMap, treeToRows };
1277
+ export { arrayCast, arrayCompete, arrayCounting, arrayDifference, arrayFirst, arrayFork, arrayIntersection, arrayLast, arrayMerge, arrayPick, arrayReplace, arraySplit, cloneDeep, enumEntries, enumKeys, enumTypeCheck, enumValues, isArray, isAsyncFunction, isAsyncGeneratorFunction, isBigInt, isBlob, isBoolean, isClass, isDate, isEqual, isError, isFalsy, isFalsyLike, isFile, isFunction, isGeneratorFunction, isInfinity, isInfinityLike, isInteger, isIterable, isMap, isNaN, isNegativeInteger, isNull, isNumber, isObject, isPositiveInteger, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURLSearchParams, isUndefined, isWeakMap, isWeakSet, isWebSocket, isWindow, mapEntries, objectAssign, objectEntries, objectKeys, objectPick, objectSwitch, objectValues, rowsToTree, stringInitialCase, stringReplace, stringToJson, stringToNumber, stringToValues, to, toMathBignumber, toMathDecimal, toMathEvaluate, treeFilter, treeFind, treeForEach, treeMap, treeToRows };
1247
1278
  //# sourceMappingURL=index.js.map