moderndash 1.2.0 → 2.0.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/README.md +15 -4
- package/dist/index.cjs +85 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +160 -59
- package/dist/index.js +81 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,9 +25,12 @@
|
|
|
25
25
|
<a href="https://www.npmjs.com/package/moderndash">
|
|
26
26
|
<img alt="npm" src="https://img.shields.io/npm/dw/moderndash">
|
|
27
27
|
</a>
|
|
28
|
-
<
|
|
28
|
+
<a href="https://www.npmjs.com/package/moderndash">
|
|
29
|
+
<img alt="npm" src="https://img.shields.io/npm/v/moderndash?color=blue&label=version">
|
|
30
|
+
</a>
|
|
31
|
+
<img alt="node-current" src="https://img.shields.io/node/v/moderndash?color=blue">
|
|
29
32
|
<a href="https://github.com/Maggi64/moderndash/blob/main/LICENSE">
|
|
30
|
-
<img alt="GitHub" src="https://img.shields.io/github/license/maggi64/moderndash">
|
|
33
|
+
<img alt="GitHub" src="https://img.shields.io/github/license/maggi64/moderndash?color=orange">
|
|
31
34
|
</a>
|
|
32
35
|
</div>
|
|
33
36
|
|
|
@@ -58,7 +61,7 @@ arr.filter(Boolean)
|
|
|
58
61
|
npm install moderndash
|
|
59
62
|
```
|
|
60
63
|
|
|
61
|
-
## FAQ
|
|
64
|
+
## 🗃 FAQ
|
|
62
65
|
|
|
63
66
|
### Why is X lodash function not included?
|
|
64
67
|
Please refer to [You-Dont-Need-Lodash](https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore) or [youmightnotneed.com/lodash](https://youmightnotneed.com/lodash) for native replacements.
|
|
@@ -67,4 +70,12 @@ If you still believe a function is missing, please open an issue.
|
|
|
67
70
|
### Why no pipe utility functions?
|
|
68
71
|
The upcoming [pipe operator](https://github.com/tc39/proposal-pipeline-operator) in JavaScript will provide function composition, so the framework focuses on providing other useful utility functions that are not yet available.
|
|
69
72
|
|
|
70
|
-
The pipe operator can already be included via [babel](https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator).
|
|
73
|
+
The pipe operator can already be included via [babel](https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator).
|
|
74
|
+
|
|
75
|
+
## 💌 Love & Thanks
|
|
76
|
+
|
|
77
|
+
To [type-fest](https://github.com/sindresorhus/type-fest) for providing some valuable types.
|
|
78
|
+
|
|
79
|
+
## 🧰 Contribute
|
|
80
|
+
|
|
81
|
+
Check the [contribute](https://github.com/Maggi64/moderndash/blob/main/CONTRIBUTE.md) section!
|
package/dist/index.cjs
CHANGED
|
@@ -39,6 +39,7 @@ __export(src_exports, {
|
|
|
39
39
|
escapeHtml: () => escapeHtml,
|
|
40
40
|
escapeRegExp: () => escapeRegExp,
|
|
41
41
|
group: () => group,
|
|
42
|
+
hash: () => hash,
|
|
42
43
|
intersection: () => intersection,
|
|
43
44
|
isEmpty: () => isEmpty,
|
|
44
45
|
isEqual: () => isEqual,
|
|
@@ -54,12 +55,13 @@ __export(src_exports, {
|
|
|
54
55
|
pascalCase: () => pascalCase,
|
|
55
56
|
pick: () => pick,
|
|
56
57
|
races: () => races,
|
|
58
|
+
randomElem: () => randomElem,
|
|
57
59
|
randomFloat: () => randomFloat,
|
|
58
60
|
randomInt: () => randomInt,
|
|
61
|
+
randomString: () => randomString,
|
|
59
62
|
range: () => range,
|
|
60
63
|
retry: () => retry,
|
|
61
64
|
round: () => round,
|
|
62
|
-
sample: () => sample,
|
|
63
65
|
set: () => set,
|
|
64
66
|
shuffle: () => shuffle,
|
|
65
67
|
sleep: () => sleep,
|
|
@@ -74,6 +76,7 @@ __export(src_exports, {
|
|
|
74
76
|
times: () => times,
|
|
75
77
|
titleCase: () => titleCase,
|
|
76
78
|
toDecorator: () => toDecorator,
|
|
79
|
+
tryCatch: () => tryCatch,
|
|
77
80
|
unescapeHtml: () => unescapeHtml,
|
|
78
81
|
unique: () => unique
|
|
79
82
|
});
|
|
@@ -222,26 +225,6 @@ function* range(start, end, step = 1) {
|
|
|
222
225
|
}
|
|
223
226
|
}
|
|
224
227
|
|
|
225
|
-
// src/array/sample.ts
|
|
226
|
-
function sample(array, multi) {
|
|
227
|
-
if (multi === void 0) {
|
|
228
|
-
if (array.length === 0)
|
|
229
|
-
return void 0;
|
|
230
|
-
return getSingleSample(array);
|
|
231
|
-
}
|
|
232
|
-
if (multi && array.length === 0)
|
|
233
|
-
return [];
|
|
234
|
-
const result = new Array(multi);
|
|
235
|
-
for (let i = 0; i < multi; i++) {
|
|
236
|
-
result[i] = getSingleSample(array);
|
|
237
|
-
}
|
|
238
|
-
return result;
|
|
239
|
-
}
|
|
240
|
-
function getSingleSample(array) {
|
|
241
|
-
const randomIndex = Math.floor(Math.random() * array.length);
|
|
242
|
-
return array[randomIndex];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
228
|
// src/array/shuffle.ts
|
|
246
229
|
function shuffle(array) {
|
|
247
230
|
const shuffledArray = [...array];
|
|
@@ -308,6 +291,71 @@ function unique(array, compareFn = (a, b) => isEqual(a, b)) {
|
|
|
308
291
|
});
|
|
309
292
|
}
|
|
310
293
|
|
|
294
|
+
// src/crypto/hash.ts
|
|
295
|
+
async function hash(data, algorithm = "SHA-256") {
|
|
296
|
+
const encoder = new TextEncoder();
|
|
297
|
+
const dataBuffer = typeof data === "string" ? encoder.encode(data) : encoder.encode(JSON.stringify(data));
|
|
298
|
+
const hashBuffer = await crypto.subtle.digest(algorithm, dataBuffer);
|
|
299
|
+
const hashArray = [...new Uint8Array(hashBuffer)];
|
|
300
|
+
const hexValues = hashArray.map((b) => b.toString(16).padStart(2, "0"));
|
|
301
|
+
return hexValues.join("");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/crypto/randomInt.ts
|
|
305
|
+
function randomInt(min, max) {
|
|
306
|
+
if (min > max)
|
|
307
|
+
throw new Error("min must be less than or equal to max");
|
|
308
|
+
const range2 = max - min + 1;
|
|
309
|
+
const randomBuffer = new Uint32Array(1);
|
|
310
|
+
crypto.getRandomValues(randomBuffer);
|
|
311
|
+
return min + randomBuffer[0] % range2;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/crypto/randomElem.ts
|
|
315
|
+
function randomElem(array, multi) {
|
|
316
|
+
if (multi === void 0) {
|
|
317
|
+
if (array.length === 0)
|
|
318
|
+
return void 0;
|
|
319
|
+
return getSingleElement(array);
|
|
320
|
+
}
|
|
321
|
+
if (multi && array.length === 0)
|
|
322
|
+
return [];
|
|
323
|
+
const result = new Array(multi);
|
|
324
|
+
for (let i = 0; i < multi; i++) {
|
|
325
|
+
result[i] = getSingleElement(array);
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
function getSingleElement(array) {
|
|
330
|
+
const randomIndex = randomInt(0, array.length - 1);
|
|
331
|
+
return array[randomIndex];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/crypto/randomFloat.ts
|
|
335
|
+
function randomFloat(min, max) {
|
|
336
|
+
if (min > max)
|
|
337
|
+
throw new Error("min must be less than or equal to max");
|
|
338
|
+
const range2 = max - min;
|
|
339
|
+
const randomBuffer = new Uint32Array(1);
|
|
340
|
+
crypto.getRandomValues(randomBuffer);
|
|
341
|
+
const random = randomBuffer[0] / 4294967295;
|
|
342
|
+
return min + random * range2;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/crypto/randomString.ts
|
|
346
|
+
var DEFAULT_CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
347
|
+
function randomString(length, charSet = DEFAULT_CHARSET) {
|
|
348
|
+
const randomValues = new Uint32Array(length);
|
|
349
|
+
crypto.getRandomValues(randomValues);
|
|
350
|
+
let result = "";
|
|
351
|
+
const charSetLength = charSet.length;
|
|
352
|
+
for (let i = 0; i < length; i++) {
|
|
353
|
+
const randomIndex = randomValues[i] % charSetLength;
|
|
354
|
+
result += charSet[randomIndex];
|
|
355
|
+
}
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
|
|
311
359
|
// src/decorator/toDecorator.ts
|
|
312
360
|
function toDecorator(func) {
|
|
313
361
|
return function(...args) {
|
|
@@ -454,27 +502,6 @@ function median(numbers) {
|
|
|
454
502
|
return sortedArray.length % 2 === 0 ? (sortedArray[mid - 1] + sortedArray[mid]) / 2 : sortedArray[mid];
|
|
455
503
|
}
|
|
456
504
|
|
|
457
|
-
// src/number/randomFloat.ts
|
|
458
|
-
function randomFloat(min, max) {
|
|
459
|
-
if (min > max)
|
|
460
|
-
throw new Error("min must be less than or equal to max");
|
|
461
|
-
const range2 = max - min;
|
|
462
|
-
const randomBuffer = new Uint32Array(1);
|
|
463
|
-
crypto.getRandomValues(randomBuffer);
|
|
464
|
-
const random = randomBuffer[0] / 4294967295;
|
|
465
|
-
return min + random * range2;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
// src/number/randomInt.ts
|
|
469
|
-
function randomInt(min, max) {
|
|
470
|
-
if (min > max)
|
|
471
|
-
throw new Error("min must be less than or equal to max");
|
|
472
|
-
const range2 = max - min + 1;
|
|
473
|
-
const randomBuffer = new Uint32Array(1);
|
|
474
|
-
crypto.getRandomValues(randomBuffer);
|
|
475
|
-
return min + randomBuffer[0] % range2;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
505
|
// src/number/round.ts
|
|
479
506
|
function round(number, precision = 2) {
|
|
480
507
|
const factor = Math.pow(10, precision);
|
|
@@ -664,6 +691,18 @@ function timeout(promise, timeout2) {
|
|
|
664
691
|
});
|
|
665
692
|
}
|
|
666
693
|
|
|
694
|
+
// src/promise/tryCatch.ts
|
|
695
|
+
async function tryCatch(promise) {
|
|
696
|
+
try {
|
|
697
|
+
const data = await promise;
|
|
698
|
+
return [data, void 0];
|
|
699
|
+
} catch (error) {
|
|
700
|
+
if (error instanceof Error)
|
|
701
|
+
return [void 0, error];
|
|
702
|
+
throw error;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
667
706
|
// src/string/splitWords.ts
|
|
668
707
|
function splitWords(str) {
|
|
669
708
|
const regex = new RegExp(
|
|
@@ -819,6 +858,7 @@ function isUrl(str) {
|
|
|
819
858
|
escapeHtml,
|
|
820
859
|
escapeRegExp,
|
|
821
860
|
group,
|
|
861
|
+
hash,
|
|
822
862
|
intersection,
|
|
823
863
|
isEmpty,
|
|
824
864
|
isEqual,
|
|
@@ -834,12 +874,13 @@ function isUrl(str) {
|
|
|
834
874
|
pascalCase,
|
|
835
875
|
pick,
|
|
836
876
|
races,
|
|
877
|
+
randomElem,
|
|
837
878
|
randomFloat,
|
|
838
879
|
randomInt,
|
|
880
|
+
randomString,
|
|
839
881
|
range,
|
|
840
882
|
retry,
|
|
841
883
|
round,
|
|
842
|
-
sample,
|
|
843
884
|
set,
|
|
844
885
|
shuffle,
|
|
845
886
|
sleep,
|
|
@@ -854,6 +895,7 @@ function isUrl(str) {
|
|
|
854
895
|
times,
|
|
855
896
|
titleCase,
|
|
856
897
|
toDecorator,
|
|
898
|
+
tryCatch,
|
|
857
899
|
unescapeHtml,
|
|
858
900
|
unique
|
|
859
901
|
});
|