devcraft-js 0.0.0-stage → 0.1.0
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/LICENSE +17 -0
- package/README.md +1461 -2
- package/dist/index.cjs +903 -0
- package/dist/index.js +830 -0
- package/package.json +42 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
// src/array/array.js
|
|
2
|
+
function chunk(array, size) {
|
|
3
|
+
if (!Array.isArray(array)) {
|
|
4
|
+
throw new TypeError("Expected an array");
|
|
5
|
+
}
|
|
6
|
+
if (!Number.isInteger(size) || size <= 0) {
|
|
7
|
+
throw new RangeError("Chunk size must be a positive integer");
|
|
8
|
+
}
|
|
9
|
+
const result = [];
|
|
10
|
+
for (let i = 0; i < array.length; i += size) {
|
|
11
|
+
result.push(array.slice(i, i + size));
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function unique(array) {
|
|
16
|
+
if (!Array.isArray(array)) {
|
|
17
|
+
throw new TypeError("Expected an array");
|
|
18
|
+
}
|
|
19
|
+
return [...new Set(array)];
|
|
20
|
+
}
|
|
21
|
+
function uniqueBy(array, keyOrFn) {
|
|
22
|
+
if (!Array.isArray(array)) {
|
|
23
|
+
throw new TypeError("Expected an array");
|
|
24
|
+
}
|
|
25
|
+
if (typeof keyOrFn !== "string" && typeof keyOrFn !== "function") {
|
|
26
|
+
throw new TypeError("Expected a property name or callback");
|
|
27
|
+
}
|
|
28
|
+
const seen = /* @__PURE__ */ new Set();
|
|
29
|
+
return array.filter((item, index) => {
|
|
30
|
+
const key = typeof keyOrFn === "function" ? keyOrFn(item, index) : item == null ? void 0 : item[keyOrFn];
|
|
31
|
+
if (seen.has(key)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
seen.add(key);
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function groupBy(array, keyOrFn) {
|
|
39
|
+
if (!Array.isArray(array)) {
|
|
40
|
+
throw new TypeError("Expected an array");
|
|
41
|
+
}
|
|
42
|
+
if (typeof keyOrFn !== "string" && typeof keyOrFn !== "function") {
|
|
43
|
+
throw new TypeError("Expected a property name or callback");
|
|
44
|
+
}
|
|
45
|
+
return array.reduce((result, item, index) => {
|
|
46
|
+
const key = typeof keyOrFn === "function" ? keyOrFn(item, index) : item == null ? void 0 : item[keyOrFn];
|
|
47
|
+
const groupKey = String(key);
|
|
48
|
+
if (!Object.hasOwn(result, groupKey)) {
|
|
49
|
+
Object.defineProperty(result, groupKey, {
|
|
50
|
+
value: [],
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
result[groupKey].push(item);
|
|
57
|
+
return result;
|
|
58
|
+
}, {});
|
|
59
|
+
}
|
|
60
|
+
function partition(array, predicate) {
|
|
61
|
+
assertArray(array);
|
|
62
|
+
assertFunction(predicate, "predicate");
|
|
63
|
+
const matched = [];
|
|
64
|
+
const unmatched = [];
|
|
65
|
+
array.forEach((value, index) => (predicate(value, index) ? matched : unmatched).push(value));
|
|
66
|
+
return [matched, unmatched];
|
|
67
|
+
}
|
|
68
|
+
function shuffle(array) {
|
|
69
|
+
assertArray(array);
|
|
70
|
+
const result = array.slice();
|
|
71
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
72
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
73
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
function flatten(array, depth = 1) {
|
|
78
|
+
assertArray(array);
|
|
79
|
+
if (depth !== Infinity && (!Number.isInteger(depth) || depth < 0)) {
|
|
80
|
+
throw new RangeError("Depth must be a non-negative integer or Infinity");
|
|
81
|
+
}
|
|
82
|
+
return array.flat(depth);
|
|
83
|
+
}
|
|
84
|
+
function difference(array, other) {
|
|
85
|
+
assertArray(array);
|
|
86
|
+
assertArray(other, "second argument");
|
|
87
|
+
const excluded = new Set(other);
|
|
88
|
+
return array.filter((value) => !excluded.has(value));
|
|
89
|
+
}
|
|
90
|
+
function assertArray(value, name = "input") {
|
|
91
|
+
if (!Array.isArray(value)) throw new TypeError(`Expected ${name} to be an array`);
|
|
92
|
+
}
|
|
93
|
+
function assertFunction(value, name) {
|
|
94
|
+
if (typeof value !== "function") throw new TypeError(`Expected ${name} to be a function`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/object/object.js
|
|
98
|
+
var unsafeKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
99
|
+
function isPlainObject(value) {
|
|
100
|
+
if (value === null || typeof value !== "object") return false;
|
|
101
|
+
const prototype = Object.getPrototypeOf(value);
|
|
102
|
+
return prototype === Object.prototype || prototype === null;
|
|
103
|
+
}
|
|
104
|
+
function assertObject(value, name = "object") {
|
|
105
|
+
if (value === null || typeof value !== "object") {
|
|
106
|
+
throw new TypeError(`Expected ${name} to be an object`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function getPathParts(path) {
|
|
110
|
+
if (typeof path !== "string" && !Array.isArray(path)) {
|
|
111
|
+
throw new TypeError("Path must be a string or array");
|
|
112
|
+
}
|
|
113
|
+
const parts = Array.isArray(path) ? path : path.split(".");
|
|
114
|
+
const hasInvalidPart = parts.some(
|
|
115
|
+
(part) => part === "" || part === null || part === void 0 || unsafeKeys.has(String(part))
|
|
116
|
+
);
|
|
117
|
+
if (parts.length === 0 || hasInvalidPart) {
|
|
118
|
+
throw new TypeError("Path contains an invalid or unsafe segment");
|
|
119
|
+
}
|
|
120
|
+
return parts;
|
|
121
|
+
}
|
|
122
|
+
function cloneEnumerableProperties(source, target, seen, skipArrayIndexes = false) {
|
|
123
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
124
|
+
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
125
|
+
if (!descriptor.enumerable) continue;
|
|
126
|
+
if (skipArrayIndexes && typeof key === "string" && /^(0|[1-9]\d*)$/u.test(key)) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if ("value" in descriptor) {
|
|
130
|
+
descriptor.value = deepClone(descriptor.value, seen);
|
|
131
|
+
}
|
|
132
|
+
Object.defineProperty(target, key, descriptor);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function deepClone(value, seen = /* @__PURE__ */ new WeakMap()) {
|
|
136
|
+
if (value === null || typeof value !== "object") return value;
|
|
137
|
+
if (seen.has(value)) return seen.get(value);
|
|
138
|
+
if (value instanceof Promise || value instanceof WeakMap || value instanceof WeakSet || typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer) {
|
|
139
|
+
throw new TypeError("Cannot deep-clone Promise, weak collection, or shared buffer values");
|
|
140
|
+
}
|
|
141
|
+
if (value instanceof Date) {
|
|
142
|
+
const clone2 = new Date(value.getTime());
|
|
143
|
+
seen.set(value, clone2);
|
|
144
|
+
cloneEnumerableProperties(value, clone2, seen);
|
|
145
|
+
return clone2;
|
|
146
|
+
}
|
|
147
|
+
if (value instanceof RegExp) {
|
|
148
|
+
const clone2 = new RegExp(value.source, value.flags);
|
|
149
|
+
seen.set(value, clone2);
|
|
150
|
+
clone2.lastIndex = value.lastIndex;
|
|
151
|
+
cloneEnumerableProperties(value, clone2, seen);
|
|
152
|
+
return clone2;
|
|
153
|
+
}
|
|
154
|
+
if (value instanceof Map) {
|
|
155
|
+
const clone2 = /* @__PURE__ */ new Map();
|
|
156
|
+
seen.set(value, clone2);
|
|
157
|
+
for (const [key, entryValue] of value) {
|
|
158
|
+
clone2.set(deepClone(key, seen), deepClone(entryValue, seen));
|
|
159
|
+
}
|
|
160
|
+
cloneEnumerableProperties(value, clone2, seen);
|
|
161
|
+
return clone2;
|
|
162
|
+
}
|
|
163
|
+
if (value instanceof Set) {
|
|
164
|
+
const clone2 = /* @__PURE__ */ new Set();
|
|
165
|
+
seen.set(value, clone2);
|
|
166
|
+
for (const entry of value) clone2.add(deepClone(entry, seen));
|
|
167
|
+
cloneEnumerableProperties(value, clone2, seen);
|
|
168
|
+
return clone2;
|
|
169
|
+
}
|
|
170
|
+
if (ArrayBuffer.isView(value)) {
|
|
171
|
+
const clonedBuffer = deepClone(value.buffer, seen);
|
|
172
|
+
const clone2 = value instanceof DataView ? new DataView(clonedBuffer, value.byteOffset, value.byteLength) : new value.constructor(clonedBuffer, value.byteOffset, value.length);
|
|
173
|
+
seen.set(value, clone2);
|
|
174
|
+
cloneEnumerableProperties(value, clone2, seen, !(value instanceof DataView));
|
|
175
|
+
return clone2;
|
|
176
|
+
}
|
|
177
|
+
if (value instanceof ArrayBuffer) {
|
|
178
|
+
const clone2 = value.slice(0);
|
|
179
|
+
seen.set(value, clone2);
|
|
180
|
+
cloneEnumerableProperties(value, clone2, seen);
|
|
181
|
+
return clone2;
|
|
182
|
+
}
|
|
183
|
+
const clone = Array.isArray(value) ? [] : Object.create(Object.getPrototypeOf(value));
|
|
184
|
+
seen.set(value, clone);
|
|
185
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
186
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
187
|
+
if ("value" in descriptor) {
|
|
188
|
+
descriptor.value = deepClone(descriptor.value, seen);
|
|
189
|
+
}
|
|
190
|
+
Object.defineProperty(clone, key, descriptor);
|
|
191
|
+
}
|
|
192
|
+
return clone;
|
|
193
|
+
}
|
|
194
|
+
function deepMerge(target, ...sources) {
|
|
195
|
+
assertObject(target, "target");
|
|
196
|
+
if (!isPlainObject(target)) {
|
|
197
|
+
throw new TypeError("Target must be a plain object");
|
|
198
|
+
}
|
|
199
|
+
function mergeInto(result2, source) {
|
|
200
|
+
const activeSources = /* @__PURE__ */ new WeakSet();
|
|
201
|
+
function mergeSource(sourceObject, targetObject) {
|
|
202
|
+
if (activeSources.has(sourceObject)) {
|
|
203
|
+
throw new TypeError("Cannot deep-merge a cyclic source object");
|
|
204
|
+
}
|
|
205
|
+
activeSources.add(sourceObject);
|
|
206
|
+
try {
|
|
207
|
+
for (const key of Object.keys(sourceObject)) {
|
|
208
|
+
if (unsafeKeys.has(key)) continue;
|
|
209
|
+
const sourceValue = sourceObject[key];
|
|
210
|
+
if (isPlainObject(sourceValue)) {
|
|
211
|
+
const targetValue = isPlainObject(targetObject[key]) ? targetObject[key] : {};
|
|
212
|
+
targetObject[key] = mergeSource(sourceValue, targetValue);
|
|
213
|
+
} else {
|
|
214
|
+
targetObject[key] = deepClone(sourceValue);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} finally {
|
|
218
|
+
activeSources.delete(sourceObject);
|
|
219
|
+
}
|
|
220
|
+
return targetObject;
|
|
221
|
+
}
|
|
222
|
+
return mergeSource(source, result2);
|
|
223
|
+
}
|
|
224
|
+
const result = deepClone(target);
|
|
225
|
+
for (const source of sources) {
|
|
226
|
+
assertObject(source, "source");
|
|
227
|
+
if (!isPlainObject(source)) {
|
|
228
|
+
throw new TypeError("Sources must be plain objects");
|
|
229
|
+
}
|
|
230
|
+
mergeInto(result, source);
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
function pick(object, keys) {
|
|
235
|
+
assertObject(object);
|
|
236
|
+
if (!Array.isArray(keys)) throw new TypeError("Keys must be an array");
|
|
237
|
+
const result = {};
|
|
238
|
+
for (const key of keys) {
|
|
239
|
+
if (!Object.hasOwn(object, key)) continue;
|
|
240
|
+
Object.defineProperty(result, key, {
|
|
241
|
+
value: object[key],
|
|
242
|
+
enumerable: true,
|
|
243
|
+
configurable: true,
|
|
244
|
+
writable: true
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
function omit(object, keys) {
|
|
250
|
+
assertObject(object);
|
|
251
|
+
if (!Array.isArray(keys)) throw new TypeError("Keys must be an array");
|
|
252
|
+
const excludedKeys = new Set(keys);
|
|
253
|
+
const result = {};
|
|
254
|
+
for (const key of Object.keys(object)) {
|
|
255
|
+
if (excludedKeys.has(key)) continue;
|
|
256
|
+
Object.defineProperty(result, key, {
|
|
257
|
+
value: object[key],
|
|
258
|
+
enumerable: true,
|
|
259
|
+
configurable: true,
|
|
260
|
+
writable: true
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
function get(object, path, fallback) {
|
|
266
|
+
const parts = getPathParts(path);
|
|
267
|
+
let current = object;
|
|
268
|
+
for (const part of parts) {
|
|
269
|
+
if (current === null || current === void 0 || !Object.hasOwn(Object(current), part)) {
|
|
270
|
+
return fallback;
|
|
271
|
+
}
|
|
272
|
+
current = current[part];
|
|
273
|
+
}
|
|
274
|
+
return current;
|
|
275
|
+
}
|
|
276
|
+
function set(root, path, value) {
|
|
277
|
+
assertObject(root, "root");
|
|
278
|
+
const parts = getPathParts(path);
|
|
279
|
+
let current = root;
|
|
280
|
+
for (let index = 0; index < parts.length - 1; index += 1) {
|
|
281
|
+
const part = parts[index];
|
|
282
|
+
if (current[part] === null || typeof current[part] !== "object") {
|
|
283
|
+
current[part] = /^\d+$/.test(String(parts[index + 1])) ? [] : {};
|
|
284
|
+
}
|
|
285
|
+
current = current[part];
|
|
286
|
+
}
|
|
287
|
+
Object.defineProperty(current, parts.at(-1), {
|
|
288
|
+
value,
|
|
289
|
+
enumerable: true,
|
|
290
|
+
configurable: true,
|
|
291
|
+
writable: true
|
|
292
|
+
});
|
|
293
|
+
return root;
|
|
294
|
+
}
|
|
295
|
+
function has(object, path) {
|
|
296
|
+
const parts = getPathParts(path);
|
|
297
|
+
let current = object;
|
|
298
|
+
for (const part of parts) {
|
|
299
|
+
if (current === null || current === void 0 || !Object.hasOwn(Object(current), part)) {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
current = current[part];
|
|
303
|
+
}
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
function compareValues(left, right) {
|
|
307
|
+
if (left == null) return right == null ? 0 : 1;
|
|
308
|
+
if (right == null) return -1;
|
|
309
|
+
if (typeof left === "string" && typeof right === "string") {
|
|
310
|
+
return left.localeCompare(right, void 0, { sensitivity: "base" });
|
|
311
|
+
}
|
|
312
|
+
if (left < right) return -1;
|
|
313
|
+
if (left > right) return 1;
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
function sortByKey(array, key, order = "asc") {
|
|
317
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
318
|
+
if (typeof key !== "string") throw new TypeError("Key must be a string");
|
|
319
|
+
if (order !== "asc" && order !== "desc") {
|
|
320
|
+
throw new RangeError("Order must be 'asc' or 'desc'");
|
|
321
|
+
}
|
|
322
|
+
return array.map((value, index) => ({ value, index })).sort((left, right) => {
|
|
323
|
+
var _a, _b;
|
|
324
|
+
const leftValue = (_a = left.value) == null ? void 0 : _a[key];
|
|
325
|
+
const rightValue = (_b = right.value) == null ? void 0 : _b[key];
|
|
326
|
+
if (leftValue == null || rightValue == null) {
|
|
327
|
+
return compareValues(leftValue, rightValue) || left.index - right.index;
|
|
328
|
+
}
|
|
329
|
+
const direction = order === "asc" ? 1 : -1;
|
|
330
|
+
return compareValues(leftValue, rightValue) * direction || left.index - right.index;
|
|
331
|
+
}).map(({ value }) => value);
|
|
332
|
+
}
|
|
333
|
+
function sortByMultiple(array, criteria) {
|
|
334
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
335
|
+
const invalidCriteria = !Array.isArray(criteria) || criteria.some(
|
|
336
|
+
(criterion) => !criterion || typeof criterion.key !== "string" || !["asc", "desc"].includes(criterion.order)
|
|
337
|
+
);
|
|
338
|
+
if (invalidCriteria) {
|
|
339
|
+
throw new TypeError("Criteria must contain { key, order: 'asc'|'desc' }");
|
|
340
|
+
}
|
|
341
|
+
return array.map((value, index) => ({ value, index })).sort((left, right) => {
|
|
342
|
+
var _a, _b;
|
|
343
|
+
for (const { key, order } of criteria) {
|
|
344
|
+
const leftValue = (_a = left.value) == null ? void 0 : _a[key];
|
|
345
|
+
const rightValue = (_b = right.value) == null ? void 0 : _b[key];
|
|
346
|
+
const direction = order === "asc" ? 1 : -1;
|
|
347
|
+
const comparison = leftValue == null || rightValue == null ? compareValues(leftValue, rightValue) : compareValues(leftValue, rightValue) * direction;
|
|
348
|
+
if (comparison !== 0) return comparison;
|
|
349
|
+
}
|
|
350
|
+
return left.index - right.index;
|
|
351
|
+
}).map(({ value }) => value);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/string/string.js
|
|
355
|
+
function assertString(value, name = "input") {
|
|
356
|
+
if (typeof value !== "string") {
|
|
357
|
+
throw new TypeError(`Expected ${name} to be a string`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function splitWords(value) {
|
|
361
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z])([A-Z][a-z])/g, "$1 $2").split(/[\s_-]+/u).filter(Boolean);
|
|
362
|
+
}
|
|
363
|
+
function capitalize(str) {
|
|
364
|
+
assertString(str);
|
|
365
|
+
if (str.length === 0) return str;
|
|
366
|
+
const [firstCharacter, ...remainingCharacters] = Array.from(str);
|
|
367
|
+
return firstCharacter.toUpperCase() + remainingCharacters.join("");
|
|
368
|
+
}
|
|
369
|
+
function camelCase(str) {
|
|
370
|
+
assertString(str);
|
|
371
|
+
const words = splitWords(str);
|
|
372
|
+
if (words.length === 0) return "";
|
|
373
|
+
return words.map((word, index) => {
|
|
374
|
+
const lowerWord = word.toLowerCase();
|
|
375
|
+
return index === 0 ? lowerWord : lowerWord.charAt(0).toUpperCase() + lowerWord.slice(1);
|
|
376
|
+
}).join("");
|
|
377
|
+
}
|
|
378
|
+
function convertSeparatedCase(str, separator) {
|
|
379
|
+
return splitWords(str).map((word) => word.toLowerCase()).join(separator);
|
|
380
|
+
}
|
|
381
|
+
function kebabCase(str) {
|
|
382
|
+
assertString(str);
|
|
383
|
+
return convertSeparatedCase(str, "-");
|
|
384
|
+
}
|
|
385
|
+
function snakeCase(str) {
|
|
386
|
+
assertString(str);
|
|
387
|
+
return convertSeparatedCase(str, "_");
|
|
388
|
+
}
|
|
389
|
+
function truncate(str, length, suffix = "...") {
|
|
390
|
+
assertString(str);
|
|
391
|
+
assertString(suffix, "suffix");
|
|
392
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
393
|
+
throw new RangeError("Length must be a non-negative integer");
|
|
394
|
+
}
|
|
395
|
+
const characters = Array.from(str);
|
|
396
|
+
if (characters.length <= length) return str;
|
|
397
|
+
if (length === 0) return "";
|
|
398
|
+
const suffixCharacters = Array.from(suffix);
|
|
399
|
+
if (suffixCharacters.length >= length) {
|
|
400
|
+
return suffixCharacters.slice(0, length).join("");
|
|
401
|
+
}
|
|
402
|
+
return characters.slice(0, length - suffixCharacters.length).join("") + suffix;
|
|
403
|
+
}
|
|
404
|
+
function reverse(str) {
|
|
405
|
+
assertString(str);
|
|
406
|
+
return Array.from(str).reverse().join("");
|
|
407
|
+
}
|
|
408
|
+
function countWords(str) {
|
|
409
|
+
assertString(str);
|
|
410
|
+
const trimmed = str.trim();
|
|
411
|
+
return trimmed === "" ? 0 : trimmed.split(/\s+/u).length;
|
|
412
|
+
}
|
|
413
|
+
function isPalindrome(str) {
|
|
414
|
+
assertString(str);
|
|
415
|
+
const normalized = Array.from(str.toLocaleLowerCase()).filter(
|
|
416
|
+
(character) => /[\p{L}\p{N}]/u.test(character)
|
|
417
|
+
);
|
|
418
|
+
return normalized.every(
|
|
419
|
+
(character, index) => character === normalized[normalized.length - 1 - index]
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// src/async/async.js
|
|
424
|
+
function validateDuration(milliseconds, name) {
|
|
425
|
+
if (typeof milliseconds !== "number" || !Number.isFinite(milliseconds) || milliseconds < 0) {
|
|
426
|
+
throw new RangeError(`${name} must be a finite non-negative number`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
function validateFunction(fn, name = "fn") {
|
|
430
|
+
if (typeof fn !== "function") throw new TypeError(`${name} must be a function`);
|
|
431
|
+
}
|
|
432
|
+
function sleep(ms) {
|
|
433
|
+
validateDuration(ms, "Duration");
|
|
434
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
435
|
+
}
|
|
436
|
+
async function retry(fn, options = {}) {
|
|
437
|
+
validateFunction(fn);
|
|
438
|
+
if (options === null || typeof options !== "object") throw new TypeError("Options must be an object");
|
|
439
|
+
const { retries = 3, delay = 1e3, backoff = 1 } = options;
|
|
440
|
+
if (!Number.isInteger(retries) || retries < 0) throw new RangeError("Retries must be a non-negative integer");
|
|
441
|
+
validateDuration(delay, "Delay");
|
|
442
|
+
if (typeof backoff !== "number" || !Number.isFinite(backoff) || backoff < 0) {
|
|
443
|
+
throw new RangeError("Backoff must be a finite non-negative number");
|
|
444
|
+
}
|
|
445
|
+
let attempt = 0;
|
|
446
|
+
while (true) {
|
|
447
|
+
try {
|
|
448
|
+
return await fn();
|
|
449
|
+
} catch (error) {
|
|
450
|
+
if (attempt >= retries) throw error;
|
|
451
|
+
await sleep(delay * backoff ** attempt);
|
|
452
|
+
attempt += 1;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function withTimeout(promise, ms) {
|
|
457
|
+
validateDuration(ms, "Timeout");
|
|
458
|
+
return new Promise((resolve, reject) => {
|
|
459
|
+
const timer = setTimeout(() => reject(new Error(`Operation timed out after ${ms} ms`)), ms);
|
|
460
|
+
Promise.resolve(promise).then(
|
|
461
|
+
(value) => {
|
|
462
|
+
clearTimeout(timer);
|
|
463
|
+
resolve(value);
|
|
464
|
+
},
|
|
465
|
+
(error) => {
|
|
466
|
+
clearTimeout(timer);
|
|
467
|
+
reject(error);
|
|
468
|
+
}
|
|
469
|
+
);
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
async function mapAsync(array, fn, concurrency = Infinity) {
|
|
473
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
474
|
+
validateFunction(fn);
|
|
475
|
+
if (concurrency !== Infinity && (!Number.isInteger(concurrency) || concurrency < 1)) {
|
|
476
|
+
throw new RangeError("Concurrency must be a positive integer or Infinity");
|
|
477
|
+
}
|
|
478
|
+
const results = new Array(array.length);
|
|
479
|
+
let nextIndex = 0;
|
|
480
|
+
let failed = false;
|
|
481
|
+
async function worker() {
|
|
482
|
+
while (!failed) {
|
|
483
|
+
const index = nextIndex++;
|
|
484
|
+
if (index >= array.length) return;
|
|
485
|
+
try {
|
|
486
|
+
results[index] = await fn(array[index], index, array);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
failed = true;
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const workers = Array.from({ length: Math.min(array.length, concurrency) }, () => worker());
|
|
494
|
+
await Promise.all(workers);
|
|
495
|
+
return results;
|
|
496
|
+
}
|
|
497
|
+
function pLimit(concurrency) {
|
|
498
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) throw new RangeError("Concurrency must be a positive integer");
|
|
499
|
+
let activeCount = 0;
|
|
500
|
+
const queue = [];
|
|
501
|
+
function runNext() {
|
|
502
|
+
if (activeCount >= concurrency || queue.length === 0) return;
|
|
503
|
+
const task = queue.shift();
|
|
504
|
+
activeCount += 1;
|
|
505
|
+
Promise.resolve().then(task.fn).then(task.resolve, task.reject).finally(() => {
|
|
506
|
+
activeCount -= 1;
|
|
507
|
+
runNext();
|
|
508
|
+
});
|
|
509
|
+
runNext();
|
|
510
|
+
}
|
|
511
|
+
return (fn, ...args) => {
|
|
512
|
+
validateFunction(fn);
|
|
513
|
+
return new Promise((resolve, reject) => {
|
|
514
|
+
queue.push({ fn: () => fn(...args), resolve, reject });
|
|
515
|
+
runNext();
|
|
516
|
+
});
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
function debounceAsync(fn, delay) {
|
|
520
|
+
validateFunction(fn);
|
|
521
|
+
validateDuration(delay, "Delay");
|
|
522
|
+
let timer;
|
|
523
|
+
let pendingCalls = [];
|
|
524
|
+
let latestContext;
|
|
525
|
+
let latestArguments;
|
|
526
|
+
return function(...args) {
|
|
527
|
+
latestContext = this;
|
|
528
|
+
latestArguments = args;
|
|
529
|
+
clearTimeout(timer);
|
|
530
|
+
const result = new Promise((resolve, reject) => pendingCalls.push({ resolve, reject }));
|
|
531
|
+
timer = setTimeout(async () => {
|
|
532
|
+
const callers = pendingCalls;
|
|
533
|
+
pendingCalls = [];
|
|
534
|
+
try {
|
|
535
|
+
const value = await fn.apply(latestContext, latestArguments);
|
|
536
|
+
callers.forEach(({ resolve }) => resolve(value));
|
|
537
|
+
} catch (error) {
|
|
538
|
+
callers.forEach(({ reject }) => reject(error));
|
|
539
|
+
}
|
|
540
|
+
}, delay);
|
|
541
|
+
return result;
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
function promiseAllSettled(promises) {
|
|
545
|
+
if (promises === null || promises === void 0 || typeof promises[Symbol.iterator] !== "function") {
|
|
546
|
+
throw new TypeError("Expected an iterable of promises or values");
|
|
547
|
+
}
|
|
548
|
+
return Promise.allSettled(promises);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// src/function/function.js
|
|
552
|
+
function assertFunction2(fn, name = "fn") {
|
|
553
|
+
if (typeof fn !== "function") {
|
|
554
|
+
throw new TypeError(`${name} must be a function`);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
function once(fn) {
|
|
558
|
+
assertFunction2(fn);
|
|
559
|
+
let hasResult = false;
|
|
560
|
+
let result;
|
|
561
|
+
return function(...args) {
|
|
562
|
+
if (hasResult) return result;
|
|
563
|
+
const nextResult = fn.apply(this, args);
|
|
564
|
+
result = nextResult;
|
|
565
|
+
hasResult = true;
|
|
566
|
+
return result;
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
function createCacheNode() {
|
|
570
|
+
return { values: /* @__PURE__ */ new Map(), result: void 0, hasResult: false };
|
|
571
|
+
}
|
|
572
|
+
function getArgumentCache(root, args) {
|
|
573
|
+
let node = root;
|
|
574
|
+
for (const argument of args) {
|
|
575
|
+
if (!node.values.has(argument)) node.values.set(argument, createCacheNode());
|
|
576
|
+
node = node.values.get(argument);
|
|
577
|
+
}
|
|
578
|
+
return node;
|
|
579
|
+
}
|
|
580
|
+
function memoize(fn, options = {}) {
|
|
581
|
+
assertFunction2(fn);
|
|
582
|
+
if (options === null || typeof options !== "object") {
|
|
583
|
+
throw new TypeError("Options must be an object");
|
|
584
|
+
}
|
|
585
|
+
const { resolver } = options;
|
|
586
|
+
if (resolver !== void 0) assertFunction2(resolver, "resolver");
|
|
587
|
+
const receiverCaches = /* @__PURE__ */ new WeakMap();
|
|
588
|
+
const primitiveReceiverCaches = /* @__PURE__ */ new Map();
|
|
589
|
+
return function(...args) {
|
|
590
|
+
const receiver = this;
|
|
591
|
+
const receiverIsObject = typeof receiver === "object" && receiver !== null || typeof receiver === "function";
|
|
592
|
+
const receiverCachesMap = receiverIsObject ? receiverCaches : primitiveReceiverCaches;
|
|
593
|
+
let root = receiverCachesMap.get(receiver);
|
|
594
|
+
if (!root) {
|
|
595
|
+
root = resolver ? /* @__PURE__ */ new Map() : createCacheNode();
|
|
596
|
+
receiverCachesMap.set(receiver, root);
|
|
597
|
+
}
|
|
598
|
+
let entry;
|
|
599
|
+
if (resolver) {
|
|
600
|
+
const key = resolver.apply(receiver, args);
|
|
601
|
+
if (!root.has(key)) root.set(key, { result: void 0, hasResult: false });
|
|
602
|
+
entry = root.get(key);
|
|
603
|
+
} else {
|
|
604
|
+
entry = getArgumentCache(root, args);
|
|
605
|
+
}
|
|
606
|
+
if (entry.hasResult) return entry.result;
|
|
607
|
+
const result = fn.apply(receiver, args);
|
|
608
|
+
entry.result = result;
|
|
609
|
+
entry.hasResult = true;
|
|
610
|
+
return result;
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
function curry(fn) {
|
|
614
|
+
assertFunction2(fn);
|
|
615
|
+
const arity = fn.length;
|
|
616
|
+
if (arity === 0) return function(...args) {
|
|
617
|
+
return fn.apply(this, args);
|
|
618
|
+
};
|
|
619
|
+
function collect(previousArguments, receiver) {
|
|
620
|
+
return function(...nextArguments) {
|
|
621
|
+
const allArguments = [...previousArguments, ...nextArguments];
|
|
622
|
+
const currentReceiver = this === void 0 ? receiver : this;
|
|
623
|
+
if (allArguments.length >= arity) {
|
|
624
|
+
return fn.apply(currentReceiver, allArguments);
|
|
625
|
+
}
|
|
626
|
+
return collect(allArguments, currentReceiver);
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
return collect([], void 0);
|
|
630
|
+
}
|
|
631
|
+
function validateFunctions(functions, name) {
|
|
632
|
+
functions.forEach((fn, index) => assertFunction2(fn, `${name}[${index}]`));
|
|
633
|
+
}
|
|
634
|
+
function compose(...functions) {
|
|
635
|
+
validateFunctions(functions, "functions");
|
|
636
|
+
if (functions.length === 0) return (value) => value;
|
|
637
|
+
return function(...args) {
|
|
638
|
+
let result = functions.at(-1).apply(this, args);
|
|
639
|
+
for (let index = functions.length - 2; index >= 0; index -= 1) {
|
|
640
|
+
result = functions[index].call(this, result);
|
|
641
|
+
}
|
|
642
|
+
return result;
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function pipe(...functions) {
|
|
646
|
+
validateFunctions(functions, "functions");
|
|
647
|
+
if (functions.length === 0) return (value) => value;
|
|
648
|
+
return function(...args) {
|
|
649
|
+
let result = functions[0].apply(this, args);
|
|
650
|
+
for (let index = 1; index < functions.length; index += 1) {
|
|
651
|
+
result = functions[index].call(this, result);
|
|
652
|
+
}
|
|
653
|
+
return result;
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
function partial(fn, ...presetArguments) {
|
|
657
|
+
assertFunction2(fn);
|
|
658
|
+
return function(...laterArguments) {
|
|
659
|
+
return fn.apply(this, [...presetArguments, ...laterArguments]);
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
function debounce(fn, delay) {
|
|
663
|
+
assertFunction2(fn);
|
|
664
|
+
if (typeof delay !== "number" || !Number.isFinite(delay) || delay < 0) {
|
|
665
|
+
throw new RangeError("Delay must be a finite non-negative number");
|
|
666
|
+
}
|
|
667
|
+
let timer;
|
|
668
|
+
return function(...args) {
|
|
669
|
+
const context = this;
|
|
670
|
+
clearTimeout(timer);
|
|
671
|
+
timer = setTimeout(() => fn.apply(context, args), delay);
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// src/validation/validation.js
|
|
676
|
+
function isString(value) {
|
|
677
|
+
return typeof value === "string";
|
|
678
|
+
}
|
|
679
|
+
function isEmail(value) {
|
|
680
|
+
if (!isString(value) || value !== value.trim()) return false;
|
|
681
|
+
const parts = value.split("@");
|
|
682
|
+
if (parts.length !== 2) return false;
|
|
683
|
+
const [localPart, domain] = parts;
|
|
684
|
+
if (!localPart || !domain || /\s/u.test(value)) return false;
|
|
685
|
+
if (localPart.startsWith(".") || localPart.endsWith(".") || localPart.includes("..")) return false;
|
|
686
|
+
if (!/^[\w.!#$%&'*+/=?^`{|}~-]+$/u.test(localPart)) return false;
|
|
687
|
+
const labels = domain.split(".");
|
|
688
|
+
return labels.length >= 2 && labels.every(
|
|
689
|
+
(label) => /^[a-z\d](?:[a-z\d-]*[a-z\d])?$/iu.test(label)
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
function isURL(value) {
|
|
693
|
+
if (!isString(value) || value.trim() !== value || /\s/u.test(value)) return false;
|
|
694
|
+
try {
|
|
695
|
+
const url = new URL(value);
|
|
696
|
+
if (!["http:", "https:"].includes(url.protocol) || !url.hostname) return false;
|
|
697
|
+
const hostname = url.hostname;
|
|
698
|
+
if (hostname.startsWith("[") && hostname.endsWith("]")) return true;
|
|
699
|
+
if (hostname === "localhost") return true;
|
|
700
|
+
const labels = hostname.split(".");
|
|
701
|
+
return labels.every(
|
|
702
|
+
(label) => /^[a-z\d](?:[a-z\d-]*[a-z\d])?$/iu.test(label)
|
|
703
|
+
);
|
|
704
|
+
} catch {
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
function isPhoneNumber(value) {
|
|
709
|
+
if (!isString(value) || !value || !/^\+?[\d ()-]+$/u.test(value)) return false;
|
|
710
|
+
const openingParentheses = value.match(/\(/gu) ?? [];
|
|
711
|
+
const closingParentheses = value.match(/\)/gu) ?? [];
|
|
712
|
+
if (openingParentheses.length !== closingParentheses.length || openingParentheses.length > 1) {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
if (openingParentheses.length === 1) {
|
|
716
|
+
const openingIndex = value.indexOf("(");
|
|
717
|
+
const closingIndex = value.indexOf(")");
|
|
718
|
+
if (closingIndex < openingIndex || !/^\d+$/u.test(value.slice(openingIndex + 1, closingIndex))) {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
const digits = value.replace(/\D/gu, "");
|
|
723
|
+
return digits.length >= 7 && digits.length <= 15;
|
|
724
|
+
}
|
|
725
|
+
function isStrongPassword(value, options = {}) {
|
|
726
|
+
if (!isString(value)) return false;
|
|
727
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
728
|
+
throw new TypeError("Options must be an object");
|
|
729
|
+
}
|
|
730
|
+
const settings = {
|
|
731
|
+
minLength: 8,
|
|
732
|
+
requireUppercase: true,
|
|
733
|
+
requireLowercase: true,
|
|
734
|
+
requireNumbers: true,
|
|
735
|
+
requireSpecialChars: true,
|
|
736
|
+
...options
|
|
737
|
+
};
|
|
738
|
+
if (!Number.isInteger(settings.minLength) || settings.minLength < 1) {
|
|
739
|
+
throw new RangeError("minLength must be a positive integer");
|
|
740
|
+
}
|
|
741
|
+
for (const optionName of ["requireUppercase", "requireLowercase", "requireNumbers", "requireSpecialChars"]) {
|
|
742
|
+
if (typeof settings[optionName] !== "boolean") {
|
|
743
|
+
throw new TypeError(`${optionName} must be a boolean`);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (value.length === 0 || Array.from(value).length < settings.minLength) return false;
|
|
747
|
+
if (settings.requireUppercase && !new RegExp("\\p{Lu}", "u").test(value)) return false;
|
|
748
|
+
if (settings.requireLowercase && !new RegExp("\\p{Ll}", "u").test(value)) return false;
|
|
749
|
+
if (settings.requireNumbers && !new RegExp("\\p{N}", "u").test(value)) return false;
|
|
750
|
+
if (settings.requireSpecialChars && !/[^\p{L}\p{N}]/u.test(value)) return false;
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
function isEmpty(value) {
|
|
754
|
+
if (value === null || value === void 0) return true;
|
|
755
|
+
if (isString(value)) return value.trim().length === 0;
|
|
756
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
757
|
+
if (typeof value !== "object") return false;
|
|
758
|
+
const prototype = Object.getPrototypeOf(value);
|
|
759
|
+
return (prototype === Object.prototype || prototype === null) && Object.keys(value).length === 0;
|
|
760
|
+
}
|
|
761
|
+
function isNumeric(value) {
|
|
762
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
763
|
+
if (!isString(value) || value.trim() === "") return false;
|
|
764
|
+
return Number.isFinite(Number(value));
|
|
765
|
+
}
|
|
766
|
+
function isJSON(value) {
|
|
767
|
+
if (!isString(value)) return false;
|
|
768
|
+
try {
|
|
769
|
+
JSON.parse(value);
|
|
770
|
+
return true;
|
|
771
|
+
} catch {
|
|
772
|
+
return false;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
function isAlpha(value) {
|
|
776
|
+
return isString(value) && value.length > 0 && new RegExp("^\\p{L}+$", "u").test(value);
|
|
777
|
+
}
|
|
778
|
+
function isAlphanumeric(value) {
|
|
779
|
+
return isString(value) && value.length > 0 && /^[\p{L}\p{N}]+$/u.test(value);
|
|
780
|
+
}
|
|
781
|
+
export {
|
|
782
|
+
camelCase,
|
|
783
|
+
capitalize,
|
|
784
|
+
chunk,
|
|
785
|
+
compose,
|
|
786
|
+
countWords,
|
|
787
|
+
curry,
|
|
788
|
+
debounce,
|
|
789
|
+
debounceAsync,
|
|
790
|
+
deepClone,
|
|
791
|
+
deepMerge,
|
|
792
|
+
difference,
|
|
793
|
+
flatten,
|
|
794
|
+
get,
|
|
795
|
+
groupBy,
|
|
796
|
+
has,
|
|
797
|
+
isAlpha,
|
|
798
|
+
isAlphanumeric,
|
|
799
|
+
isEmail,
|
|
800
|
+
isEmpty,
|
|
801
|
+
isJSON,
|
|
802
|
+
isNumeric,
|
|
803
|
+
isPalindrome,
|
|
804
|
+
isPhoneNumber,
|
|
805
|
+
isStrongPassword,
|
|
806
|
+
isURL,
|
|
807
|
+
kebabCase,
|
|
808
|
+
mapAsync,
|
|
809
|
+
memoize,
|
|
810
|
+
omit,
|
|
811
|
+
once,
|
|
812
|
+
pLimit,
|
|
813
|
+
partial,
|
|
814
|
+
partition,
|
|
815
|
+
pick,
|
|
816
|
+
pipe,
|
|
817
|
+
promiseAllSettled,
|
|
818
|
+
retry,
|
|
819
|
+
reverse,
|
|
820
|
+
set,
|
|
821
|
+
shuffle,
|
|
822
|
+
sleep,
|
|
823
|
+
snakeCase,
|
|
824
|
+
sortByKey,
|
|
825
|
+
sortByMultiple,
|
|
826
|
+
truncate,
|
|
827
|
+
unique,
|
|
828
|
+
uniqueBy,
|
|
829
|
+
withTimeout
|
|
830
|
+
};
|