@pawover/kit 0.0.0-alpha.23 → 0.0.0-alpha.25

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]",
@@ -25,6 +26,19 @@ const prototypeStrings = {
25
26
  webSocket: "[object WebSocket]",
26
27
  URLSearchParams: "[object URLSearchParams]"
27
28
  };
29
+ const TYPED_ARRAY_TAGS = new Set([
30
+ "[object Int8Array]",
31
+ "[object Uint8Array]",
32
+ "[object Uint8ClampedArray]",
33
+ "[object Int16Array]",
34
+ "[object Uint16Array]",
35
+ "[object Int32Array]",
36
+ "[object Uint32Array]",
37
+ "[object Float32Array]",
38
+ "[object Float64Array]",
39
+ "[object BigInt64Array]",
40
+ "[object BigUint64Array]"
41
+ ]);
28
42
  function resolvePrototypeString(value) {
29
43
  return Object.prototype.toString.call(value);
30
44
  }
@@ -32,43 +46,67 @@ function resolvePrototypeString(value) {
32
46
  //#endregion
33
47
  //#region src/utils/typeof/isArray.ts
34
48
  function isArray(value) {
35
- return resolvePrototypeString(value) === prototypeStrings.array;
49
+ return Array.isArray(value);
36
50
  }
37
-
38
- //#endregion
39
- //#region src/utils/typeof/isAsyncFunction.ts
40
- function isAsyncFunction(value) {
41
- return resolvePrototypeString(value) === prototypeStrings.asyncFunction;
51
+ function isTypedArray(value) {
52
+ return typeof value === "object" && value !== null && TYPED_ARRAY_TAGS.has(resolvePrototypeString(value));
42
53
  }
43
54
 
44
55
  //#endregion
45
56
  //#region src/utils/typeof/isBigInt.ts
46
57
  function isBigInt(value) {
47
- return resolvePrototypeString(value) === prototypeStrings.bigInt;
58
+ return typeof value === "bigint";
48
59
  }
49
60
 
50
61
  //#endregion
51
62
  //#region src/utils/typeof/isBoolean.ts
52
63
  function isBoolean(value) {
53
- return resolvePrototypeString(value) === prototypeStrings.boolean;
64
+ return typeof value === "boolean";
54
65
  }
55
66
 
56
67
  //#endregion
57
68
  //#region src/utils/typeof/isClass.ts
69
+ function isConstructable(fn) {
70
+ try {
71
+ Reflect.construct(fn, []);
72
+ return true;
73
+ } catch {
74
+ return false;
75
+ }
76
+ }
58
77
  function isClass(value) {
59
- return resolvePrototypeString(value).startsWith("class ");
78
+ return isFunction(value) && !isAsyncFunction(value) && Function.prototype.toString.call(value).startsWith("class ") && isConstructable(value) && value.prototype !== void 0;
79
+ }
80
+
81
+ //#endregion
82
+ //#region src/utils/typeof/isObject.ts
83
+ /**
84
+ * 判断是否为对象类型
85
+ * - 可选是否检查原型为 `Object.prototype`,防止原型链污染
86
+ *
87
+ * @param value - 待检查值
88
+ * @param prototypeCheck - 是否进行原型检查,默认 `true`
89
+ */
90
+ function isObject(value, prototypeCheck = true) {
91
+ const check = resolvePrototypeString(value) === PROTOTYPE_TAGS.object;
92
+ return prototypeCheck ? check && Object.getPrototypeOf(value) === Object.prototype : check;
60
93
  }
61
94
 
62
95
  //#endregion
63
96
  //#region src/utils/typeof/isDate.ts
64
97
  function isDate(value) {
65
- return resolvePrototypeString(value) === prototypeStrings.date;
98
+ if (!isObject(value)) return false;
99
+ try {
100
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.date && typeof value["getTime"] === "function";
101
+ } catch (error) {
102
+ return false;
103
+ }
66
104
  }
67
105
 
68
106
  //#endregion
69
107
  //#region src/utils/typeof/isEqual.ts
70
108
  /**
71
- * 判断给定的值是否相等
109
+ * 检查给定的值是否相等
72
110
  * @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
73
111
  *
74
112
  * @param {T} x
@@ -76,8 +114,8 @@ function isDate(value) {
76
114
  */
77
115
  function isEqual(x, y) {
78
116
  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();
117
+ if (isDate(x) && isDate(y)) return x.getTime() === y.getTime();
118
+ if (isRegExp(x) && isRegExp(y)) return x.toString() === y.toString();
81
119
  if (typeof x !== "object" || x === null || typeof y !== "object" || y === null) return false;
82
120
  const keysX = Reflect.ownKeys(x);
83
121
  const keysY = Reflect.ownKeys(y);
@@ -92,83 +130,39 @@ function isEqual(x, y) {
92
130
  //#endregion
93
131
  //#region src/utils/typeof/isError.ts
94
132
  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);
133
+ return value instanceof Error || resolvePrototypeString(value) === PROTOTYPE_TAGS.error;
102
134
  }
103
135
 
104
136
  //#endregion
105
137
  //#region src/utils/typeof/isFalsy.ts
106
138
  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);
139
+ if (isNaN(value) || isNull(value) || isUndefined(value)) return true;
140
+ return value === false || value === 0 || value === 0n || value === "";
116
141
  }
117
142
  function isFalsyLike(value) {
118
- return [
119
- "null",
120
- "undefined",
121
- "false",
122
- "NaN"
123
- ].includes(value);
143
+ if (isFalsy(value)) return true;
144
+ return typeof value === "string" && (value === "null" || value === "undefined" || value === "NaN" || value === "false" || value === "0" || value === "0n");
124
145
  }
125
146
 
126
147
  //#endregion
127
148
  //#region src/utils/typeof/isFile.ts
128
149
  function isFile(value) {
129
- return resolvePrototypeString(value) === prototypeStrings.file;
150
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.file;
130
151
  }
131
152
 
132
153
  //#endregion
133
154
  //#region src/utils/typeof/isFunction.ts
134
155
  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;
156
+ return typeof value === "function";
146
157
  }
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);
153
- }
154
- function isInfinityLike(value) {
155
- return [
156
- "Infinity",
157
- "-Infinity",
158
- "NaN"
159
- ].includes(value);
158
+ function isAsyncFunction(value) {
159
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncFunction;
160
160
  }
161
-
162
- //#endregion
163
- //#region src/utils/typeof/isInteger.ts
164
- function isInteger(value) {
165
- return Number.isInteger(value);
161
+ function isGeneratorFunction(value) {
162
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.generatorFunction;
166
163
  }
167
-
168
- //#endregion
169
- //#region src/utils/typeof/isObject.ts
170
- function isObject(value) {
171
- return resolvePrototypeString(value) === prototypeStrings.object;
164
+ function isAsyncGeneratorFunction(value) {
165
+ return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncGeneratorFunction;
172
166
  }
173
167
 
174
168
  //#endregion
@@ -180,29 +174,71 @@ function isIterable(value) {
180
174
  //#endregion
181
175
  //#region src/utils/typeof/isMap.ts
182
176
  function isMap(value) {
183
- return resolvePrototypeString(value) === prototypeStrings.map;
177
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.map;
178
+ }
179
+ function isWeakMap(value) {
180
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakMap;
181
+ }
182
+
183
+ //#endregion
184
+ //#region src/utils/typeof/isNaN.ts
185
+ function isNaN(value) {
186
+ return Number.isNaN(value);
184
187
  }
185
188
 
186
189
  //#endregion
187
190
  //#region src/utils/typeof/isNull.ts
188
191
  function isNull(value) {
189
- return resolvePrototypeString(value) === prototypeStrings.null;
192
+ return value === null;
190
193
  }
191
194
 
192
195
  //#endregion
193
196
  //#region src/utils/typeof/isNumber.ts
194
197
  function isNumber(value) {
195
- return resolvePrototypeString(value) === prototypeStrings.number;
198
+ return typeof value === "number";
199
+ }
200
+ /**
201
+ * 检查 value 是否为整数
202
+ *
203
+ * @param value - 待检查值
204
+ * @param safeCheck - 是否附加安全数检查
205
+ */
206
+ function isInteger(value, safeCheck = true) {
207
+ const check = Number.isInteger(value);
208
+ return safeCheck ? check && Number.isSafeInteger(value) : check;
209
+ }
210
+ /**
211
+ * 检查 value 是否为正整数
212
+ * - 未考虑 value 为 0 的情况
213
+ *
214
+ * @param value - 待检查值
215
+ * @param safeCheck - 是否附加安全数检查
216
+ */
217
+ function isPositiveInteger(value, safeCheck = true) {
218
+ return isInteger(value, safeCheck) && value > 0;
219
+ }
220
+ /**
221
+ * 检查 value 是否为负整数
222
+ * - 未考虑 value 为 0 的情况
223
+ *
224
+ * @param value - 待检查值
225
+ * @param safeCheck - 是否附加安全数检查
226
+ */
227
+ function isNegativeInteger(value, safeCheck = true) {
228
+ return isInteger(value, safeCheck) && value < 0;
229
+ }
230
+ function isInfinity(value) {
231
+ return isNumber(value) && (Number.POSITIVE_INFINITY === value || Number.NEGATIVE_INFINITY === value);
232
+ }
233
+ function isInfinityLike(value) {
234
+ return isInfinity(value) && typeof value === "string" && (value === "Infinity" || value === "-Infinity" || value === "+Infinity");
196
235
  }
197
236
 
198
237
  //#endregion
199
238
  //#region src/utils/typeof/isPromise.ts
200
239
  function isPromise(value) {
201
- return resolvePrototypeString(value) === prototypeStrings.promise;
240
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.promise;
202
241
  }
203
-
204
- //#endregion
205
- //#region src/utils/typeof/isPromiseLike.ts
206
242
  function isPromiseLike(value) {
207
243
  return isPromise(value) || isObject(value) && isFunction(value["then"]);
208
244
  }
@@ -210,61 +246,58 @@ function isPromiseLike(value) {
210
246
  //#endregion
211
247
  //#region src/utils/typeof/isRegExp.ts
212
248
  function isRegExp(value) {
213
- return resolvePrototypeString(value) === prototypeStrings.regExp;
249
+ if (!isObject(value)) return false;
250
+ try {
251
+ const regex = value;
252
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.regExp && isString(regex.source) && isString(regex.flags) && isBoolean(regex.global) && isFunction(regex.test);
253
+ } catch (error) {
254
+ return false;
255
+ }
214
256
  }
215
257
 
216
258
  //#endregion
217
259
  //#region src/utils/typeof/isSet.ts
218
260
  function isSet(value) {
219
- return resolvePrototypeString(value) === prototypeStrings.set;
261
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.set;
262
+ }
263
+ function isWeakSet(value) {
264
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakSet;
220
265
  }
221
266
 
222
267
  //#endregion
223
268
  //#region src/utils/typeof/isString.ts
224
269
  function isString(value) {
225
- return resolvePrototypeString(value) === prototypeStrings.string;
270
+ return typeof value === "string";
226
271
  }
227
272
 
228
273
  //#endregion
229
274
  //#region src/utils/typeof/isSymbol.ts
230
275
  function isSymbol(value) {
231
- return resolvePrototypeString(value) === prototypeStrings.symbol;
276
+ return typeof value === "symbol";
232
277
  }
233
278
 
234
279
  //#endregion
235
280
  //#region src/utils/typeof/isUndefined.ts
236
281
  function isUndefined(value) {
237
- return resolvePrototypeString(value) === prototypeStrings.undefined;
282
+ return typeof value === "undefined";
238
283
  }
239
284
 
240
285
  //#endregion
241
286
  //#region src/utils/typeof/isURLSearchParams.ts
242
287
  function isURLSearchParams(value) {
243
- return resolvePrototypeString(value) === prototypeStrings.URLSearchParams;
244
- }
245
-
246
- //#endregion
247
- //#region src/utils/typeof/isWeakMap.ts
248
- function isWeakMap(value) {
249
- return resolvePrototypeString(value) === prototypeStrings.weakMap;
250
- }
251
-
252
- //#endregion
253
- //#region src/utils/typeof/isWeakSet.ts
254
- function isWeakSet(value) {
255
- return resolvePrototypeString(value) === prototypeStrings.weakSet;
288
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.URLSearchParams;
256
289
  }
257
290
 
258
291
  //#endregion
259
292
  //#region src/utils/typeof/isWebSocket.ts
260
293
  function isWebSocket(value) {
261
- return resolvePrototypeString(value) === prototypeStrings.webSocket;
294
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.webSocket;
262
295
  }
263
296
 
264
297
  //#endregion
265
298
  //#region src/utils/typeof/isWindow.ts
266
299
  function isWindow(value) {
267
- return resolvePrototypeString(value) === prototypeStrings.window;
300
+ return resolvePrototypeString(value) === PROTOTYPE_TAGS.window;
268
301
  }
269
302
 
270
303
  //#endregion
@@ -543,7 +576,7 @@ function stringToNumber(value) {
543
576
  */
544
577
  function toMathBignumber(mathJsInstance, value, saveValue) {
545
578
  const errorValue = saveValue ?? mathJsInstance.bignumber(0);
546
- if (isFalsy(value) || isFalsyLike(value) || isInfinity(value) || isInfinityLike(value)) return errorValue;
579
+ if (isFalsyLike(value) || isInfinityLike(value)) return errorValue;
547
580
  try {
548
581
  return mathJsInstance.bignumber(stringToNumber(`${value}`));
549
582
  } catch (error) {
@@ -735,24 +768,22 @@ function mapEntries(obj, toEntry) {
735
768
  }
736
769
 
737
770
  //#endregion
738
- //#region node_modules/.pnpm/radashi@12.7.1/node_modules/radashi/dist/radashi.js
739
- function assign(initial, override) {
740
- if (!initial || !override) return initial ?? override ?? {};
771
+ //#region src/utils/object/objectAssign.ts
772
+ /**
773
+ * 递归地将第二个对象合并到第一个对象的副本中
774
+ * - 只有普通对象才会递归合并
775
+ *
776
+ * @param initial 初始对象
777
+ * @param override 待合并对象
778
+ */
779
+ function objectAssign(initial, override) {
780
+ if (!isObject(initial) || !isObject(override)) return initial ?? override ?? {};
741
781
  const proto = Object.getPrototypeOf(initial);
742
- const merged = proto ? { ...initial } : Object.assign(Object.create(proto), initial);
743
- for (const key of Object.keys(override)) merged[key] = isPlainObject(initial[key]) && isPlainObject(override[key]) ? assign(initial[key], override[key]) : override[key];
744
- return merged;
745
- }
746
- function isPlainObject(value) {
747
- if (typeof value !== "object" || value === null) return false;
748
- const prototype = Object.getPrototypeOf(value);
749
- return prototype === Object.prototype || prototype === null || Object.getPrototypeOf(prototype) === null;
782
+ const assigned = proto ? { ...initial } : Object.assign(Object.create(proto), initial);
783
+ for (const key of Object.keys(override)) assigned[key] = isObject(initial[key]) && isObject(override[key]) ? objectAssign(initial[key], override[key]) : override[key];
784
+ return assigned;
750
785
  }
751
786
 
752
- //#endregion
753
- //#region src/utils/object/objectAssign.ts
754
- const objectAssign = assign;
755
-
756
787
  //#endregion
757
788
  //#region src/utils/object/objectPick.ts
758
789
  function objectPick(obj, keys) {
@@ -1236,5 +1267,5 @@ function treeToRows(tree, options = {}) {
1236
1267
  }
1237
1268
 
1238
1269
  //#endregion
1239
- 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 };
1270
+ export { arrayCast, arrayCompete, arrayCounting, arrayDifference, arrayFirst, arrayFork, arrayIntersection, arrayLast, arrayMerge, arrayPick, arrayReplace, arraySplit, cloneDeep, enumEntries, enumKeys, enumTypeCheck, enumValues, isArray, isAsyncFunction, isAsyncGeneratorFunction, isBigInt, 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 };
1240
1271
  //# sourceMappingURL=index.js.map