foreslash 0.3.1 → 0.3.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.
- package/CHANGELOG.md +30 -0
- package/lib/index.cmn.cjs +504 -4
- package/lib/index.d.ts +442 -20
- package/lib/index.mjs +487 -5
- package/lib/index.umd.js +504 -4
- package/package.json +7 -6
package/lib/index.mjs
CHANGED
|
@@ -9,6 +9,57 @@ EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
|
9
9
|
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
10
10
|
See the Mulan PSL v2 for more details.
|
|
11
11
|
*/
|
|
12
|
+
function cartesianProduct(...arrList) {
|
|
13
|
+
const arrLen = arrList.length;
|
|
14
|
+
const pList = Array(arrLen).fill(0);
|
|
15
|
+
const lastIndex = arrLen - 1;
|
|
16
|
+
const res = [];
|
|
17
|
+
for (let i = 0; i < arrLen; i++) {
|
|
18
|
+
if (!arrList[i].length)
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
while (1) {
|
|
22
|
+
const temp = [];
|
|
23
|
+
for (let i = 0; i < arrLen; i++) {
|
|
24
|
+
temp.push(arrList[i][pList[i]]);
|
|
25
|
+
}
|
|
26
|
+
res.push(temp);
|
|
27
|
+
pList[lastIndex] += 1;
|
|
28
|
+
for (let i = lastIndex; i >= 0; i--) {
|
|
29
|
+
if (pList[i] >= arrList[i].length) {
|
|
30
|
+
if (i > 0) {
|
|
31
|
+
pList[i] = 0;
|
|
32
|
+
pList[i - 1] += 1;
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
else
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
if (pList[0] >= arrList[0].length)
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
return res;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function castArray(value) {
|
|
47
|
+
return Array.isArray(value) ? value.slice() : [value];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function chunk(array, size = 2) {
|
|
51
|
+
const res = [];
|
|
52
|
+
if (size < 1 || isNaN(size))
|
|
53
|
+
return res;
|
|
54
|
+
let start = 0, end = size;
|
|
55
|
+
while (start < array.length) {
|
|
56
|
+
res.push(Array.prototype.slice.call(array, start, end));
|
|
57
|
+
start = end;
|
|
58
|
+
end += size;
|
|
59
|
+
}
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
|
|
12
63
|
function range(start, end, stepOrOptions) {
|
|
13
64
|
if (!isFinite(start))
|
|
14
65
|
throw new Error('start must be finite');
|
|
@@ -200,6 +251,22 @@ function isNumber(value) {
|
|
|
200
251
|
return typeof value === 'number';
|
|
201
252
|
}
|
|
202
253
|
|
|
254
|
+
const fnToString = Function.prototype.toString;
|
|
255
|
+
const objProtoContString = fnToString.call(Object.prototype.constructor);
|
|
256
|
+
function isPlainObject(value) {
|
|
257
|
+
if (typeof value !== 'object' || value === null)
|
|
258
|
+
return false;
|
|
259
|
+
const prototype = Object.getPrototypeOf(value);
|
|
260
|
+
if (prototype === null)
|
|
261
|
+
return true;
|
|
262
|
+
if (!Object.prototype.hasOwnProperty.call(prototype, "constructor"))
|
|
263
|
+
return false;
|
|
264
|
+
const constructorFn = prototype.constructor;
|
|
265
|
+
if (!isFunction(constructorFn) || fnToString.call(constructorFn) !== objProtoContString)
|
|
266
|
+
return false;
|
|
267
|
+
return (Object.getPrototypeOf(prototype) === null);
|
|
268
|
+
}
|
|
269
|
+
|
|
203
270
|
function isPrimitive(value) {
|
|
204
271
|
return value === undefined || value === null || (typeof value !== 'object' && typeof value !== 'function');
|
|
205
272
|
}
|
|
@@ -372,6 +439,373 @@ function clamp(num, min, max, options) {
|
|
|
372
439
|
return num < min ? defaultMin : num > max ? defaultMax : num;
|
|
373
440
|
}
|
|
374
441
|
|
|
442
|
+
function decimalNotation(num) {
|
|
443
|
+
const n = isNumber(num) ? num : Number(num);
|
|
444
|
+
if (!isFinite(n))
|
|
445
|
+
return String(n);
|
|
446
|
+
let str = String(n);
|
|
447
|
+
if (!/e/i.test(str))
|
|
448
|
+
return str;
|
|
449
|
+
const match = str.match(/^(-?)(\d+)\.?(\d*)e([+-]?)(\d+)$/);
|
|
450
|
+
const [_, sign, mantissaI, mantissaF, expSign, _exponent] = match;
|
|
451
|
+
const exponent = Number(_exponent);
|
|
452
|
+
let mantissa = mantissaI + mantissaF;
|
|
453
|
+
let dotPos = mantissaI.length;
|
|
454
|
+
if (expSign === '-') {
|
|
455
|
+
const zeroCount = exponent - dotPos + 1;
|
|
456
|
+
if (zeroCount > 0)
|
|
457
|
+
mantissa = '0'.repeat(zeroCount) + mantissa;
|
|
458
|
+
return sign + mantissa[0] + '.' + mantissa.substring(1);
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
const zeroCount = exponent + dotPos - mantissa.length;
|
|
462
|
+
if (zeroCount > 0)
|
|
463
|
+
mantissa = mantissa + '0'.repeat(zeroCount);
|
|
464
|
+
return sign + mantissa.substring(0, exponent + dotPos);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function isOdd(num) {
|
|
469
|
+
return !!(num & 1);
|
|
470
|
+
}
|
|
471
|
+
function isEven(num) {
|
|
472
|
+
return !(num & 1);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function round(num, precision, type) {
|
|
476
|
+
const str = decimalNotation(num);
|
|
477
|
+
if (/NaN|Inf/.test(str))
|
|
478
|
+
return str;
|
|
479
|
+
let [_integer, _fractional] = str.split('.');
|
|
480
|
+
let sign = '';
|
|
481
|
+
if (/^-/.test(_integer)) {
|
|
482
|
+
_integer = _integer.substring(1);
|
|
483
|
+
sign = '-';
|
|
484
|
+
}
|
|
485
|
+
_fractional = _fractional !== null && _fractional !== void 0 ? _fractional : '';
|
|
486
|
+
const roundMap = { round: roundBase, banker: roundBank, floor: roundFloor, ceil: roundCeil };
|
|
487
|
+
const [integer, fractional] = (roundMap[type || 'round'] || roundBase)(_integer, _fractional, precision, !!sign);
|
|
488
|
+
if (precision > 0)
|
|
489
|
+
return sign + integer + '.' + fractional;
|
|
490
|
+
else
|
|
491
|
+
return sign + integer;
|
|
492
|
+
}
|
|
493
|
+
function roundBase(integer, fractional, precision) {
|
|
494
|
+
if (fractional.length > precision) {
|
|
495
|
+
const splitF = fractional.substring(0, precision);
|
|
496
|
+
if (Number(fractional[precision]) > 4)
|
|
497
|
+
return increase(integer, splitF);
|
|
498
|
+
return [integer, splitF];
|
|
499
|
+
}
|
|
500
|
+
if (fractional.length < precision) {
|
|
501
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
502
|
+
}
|
|
503
|
+
return [integer, fractional];
|
|
504
|
+
}
|
|
505
|
+
function roundBank(integer, fractional, precision) {
|
|
506
|
+
if (fractional.length > precision) {
|
|
507
|
+
const splitF = fractional.substring(0, precision);
|
|
508
|
+
const rightNumber = Number(fractional[precision]);
|
|
509
|
+
if (rightNumber < 5)
|
|
510
|
+
return [integer, splitF];
|
|
511
|
+
if (rightNumber > 5)
|
|
512
|
+
return increase(integer, splitF);
|
|
513
|
+
const rightNumbers = fractional.substring(precision);
|
|
514
|
+
if (!/^50*$/.test(rightNumbers))
|
|
515
|
+
return increase(integer, splitF);
|
|
516
|
+
const leftNumber = Number(precision ? fractional[precision - 1] : integer[integer.length - 1]);
|
|
517
|
+
if (isEven(leftNumber))
|
|
518
|
+
return [integer, splitF];
|
|
519
|
+
else
|
|
520
|
+
return increase(integer, splitF);
|
|
521
|
+
}
|
|
522
|
+
if (fractional.length < precision) {
|
|
523
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
524
|
+
}
|
|
525
|
+
return [integer, fractional];
|
|
526
|
+
}
|
|
527
|
+
function roundFloor(integer, fractional, precision, isNegative) {
|
|
528
|
+
if (fractional.length > precision) {
|
|
529
|
+
const splitF = fractional.substring(0, precision);
|
|
530
|
+
if (isNegative)
|
|
531
|
+
return increase(integer, splitF);
|
|
532
|
+
return [integer, splitF];
|
|
533
|
+
}
|
|
534
|
+
if (fractional.length < precision) {
|
|
535
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
536
|
+
}
|
|
537
|
+
return [integer, fractional];
|
|
538
|
+
}
|
|
539
|
+
function roundCeil(integer, fractional, precision, isNegative) {
|
|
540
|
+
if (fractional.length > precision) {
|
|
541
|
+
const splitF = fractional.substring(0, precision);
|
|
542
|
+
if (!isNegative)
|
|
543
|
+
return increase(integer, splitF);
|
|
544
|
+
return [integer, splitF];
|
|
545
|
+
}
|
|
546
|
+
if (fractional.length < precision) {
|
|
547
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
548
|
+
}
|
|
549
|
+
return [integer, fractional];
|
|
550
|
+
}
|
|
551
|
+
function increase(integer, fractional) {
|
|
552
|
+
if (fractional) {
|
|
553
|
+
const _fractional = _increase(fractional);
|
|
554
|
+
if (_fractional.length > fractional.length) {
|
|
555
|
+
return [_increase(integer), _fractional.substring(1)];
|
|
556
|
+
}
|
|
557
|
+
return [integer, _fractional];
|
|
558
|
+
}
|
|
559
|
+
return [_increase(integer), ''];
|
|
560
|
+
}
|
|
561
|
+
function _increase(num) {
|
|
562
|
+
const numList = num.split('').map(Number).reverse();
|
|
563
|
+
numList[0] += 1;
|
|
564
|
+
numList.push(0);
|
|
565
|
+
for (let i = 0; i < numList.length; i++) {
|
|
566
|
+
if (numList[i] > 9) {
|
|
567
|
+
numList[i] -= 10;
|
|
568
|
+
numList[i + 1] += 1;
|
|
569
|
+
}
|
|
570
|
+
else
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
if (numList[numList.length - 1] === 0)
|
|
574
|
+
numList.pop();
|
|
575
|
+
return numList.reverse().map(String).join('');
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function format(num, options) {
|
|
579
|
+
var _a;
|
|
580
|
+
const str = decimalNotation(num);
|
|
581
|
+
if (/NaN|Inf/.test(str))
|
|
582
|
+
return str;
|
|
583
|
+
const separator = (options || {}).separator || ',';
|
|
584
|
+
const separate = clamp((options || {}).separate || 3, 1, Infinity);
|
|
585
|
+
const decimal = (options || {}).decimal || '.';
|
|
586
|
+
const precision = clamp((_a = (options || {}).precision) !== null && _a !== void 0 ? _a : 2, 0, Infinity);
|
|
587
|
+
const round = (options || {}).round || 'round';
|
|
588
|
+
let [integer, fractional] = str.split('.');
|
|
589
|
+
let sign = '';
|
|
590
|
+
if (/^-/.test(integer)) {
|
|
591
|
+
integer = integer.substring(1);
|
|
592
|
+
sign = '-';
|
|
593
|
+
}
|
|
594
|
+
fractional = fractional !== null && fractional !== void 0 ? fractional : '';
|
|
595
|
+
if (fractional.length > precision) {
|
|
596
|
+
const roundMap = { round: roundBase, banker: roundBank, floor: roundFloor, ceil: roundCeil };
|
|
597
|
+
[integer, fractional] = (roundMap[round] || roundBase)(integer, fractional, precision, !!sign);
|
|
598
|
+
}
|
|
599
|
+
else if (fractional.length < precision) {
|
|
600
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
601
|
+
}
|
|
602
|
+
if (integer.length > separate) {
|
|
603
|
+
integer = chunk(Array.from(integer).reverse(), separate)
|
|
604
|
+
.map((part) => part.join(''))
|
|
605
|
+
.join(separator);
|
|
606
|
+
integer = Array.from(integer).reverse().join('');
|
|
607
|
+
}
|
|
608
|
+
if (precision > 0)
|
|
609
|
+
return sign + integer + decimal + fractional;
|
|
610
|
+
else
|
|
611
|
+
return sign + integer;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function lerp(val1, val2, t) {
|
|
615
|
+
if (isNumber(val1)) {
|
|
616
|
+
if (!isNumber(val2))
|
|
617
|
+
throw new Error('Invalid val2 parameter: val2 should be a number');
|
|
618
|
+
return _lerp(val1, val2, t);
|
|
619
|
+
}
|
|
620
|
+
else if (isArray(val1)) {
|
|
621
|
+
if (!isArray(val2))
|
|
622
|
+
throw new Error('Invalid val2 parameter: val2 should be an Array');
|
|
623
|
+
if (val1.length !== val2.length) {
|
|
624
|
+
throw new Error(`Invalid val2 parameter: the length of val2 (${val2.length}) should be consistent with val1 (${val1.length})`);
|
|
625
|
+
}
|
|
626
|
+
if (val1.length === 0)
|
|
627
|
+
return [];
|
|
628
|
+
if (isNumber(val1[0])) {
|
|
629
|
+
if (!isNumber(val2[0]))
|
|
630
|
+
throw new Error('Invalid val2 parameter: val2 should be an Array<number>');
|
|
631
|
+
return val1.map((v1, index) => _lerp(v1, val2[index], t));
|
|
632
|
+
}
|
|
633
|
+
else if (isArray(val1[0])) {
|
|
634
|
+
const res = [];
|
|
635
|
+
for (let i = 0; i < val1.length; i++) {
|
|
636
|
+
const arr1 = val1[i];
|
|
637
|
+
const arr2 = val2[i];
|
|
638
|
+
if (arr1.length !== arr2.length) {
|
|
639
|
+
throw new Error(`Invalid val2 parameter: The length of array at index ${i} in val2 (${arr2.length}) must match the length of the corresponding array in val1 (${arr1.length})`);
|
|
640
|
+
}
|
|
641
|
+
res.push(arr1.map((v1, index) => _lerp(v1, arr2[index], t)));
|
|
642
|
+
}
|
|
643
|
+
return res;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
throw new Error('Invalid val1 parameter: val1 should be a number or Array');
|
|
647
|
+
}
|
|
648
|
+
function _lerp(val1, val2, t) {
|
|
649
|
+
return val1 + (val2 - val1) * t;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function romanNumerals(num, options) {
|
|
653
|
+
const str = decimalNotation(num);
|
|
654
|
+
if (/NaN|Inf/.test(str))
|
|
655
|
+
return str;
|
|
656
|
+
const type = (options || {}).type || 'unicode';
|
|
657
|
+
const thousand = (options || {}).thousand || 'normal';
|
|
658
|
+
const forceSplitThousand = thousand === 'strict';
|
|
659
|
+
const integer = str.split('.')[0].replace('-', '');
|
|
660
|
+
if (integer === '0') {
|
|
661
|
+
const zero = (options || {}).zero || '0';
|
|
662
|
+
if (type === 'json')
|
|
663
|
+
return `["${zero}"]`;
|
|
664
|
+
else
|
|
665
|
+
return zero;
|
|
666
|
+
}
|
|
667
|
+
if (integer === '1') {
|
|
668
|
+
const one = (options || {}).one || 'I';
|
|
669
|
+
if (type === 'json')
|
|
670
|
+
return `["${one}"]`;
|
|
671
|
+
else
|
|
672
|
+
return one;
|
|
673
|
+
}
|
|
674
|
+
const chunks = chunk(Array.from(integer).reverse(), 3);
|
|
675
|
+
const romanChunks = [];
|
|
676
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
677
|
+
const val = Number(chunks[i].reverse().join(''));
|
|
678
|
+
if (i === chunks.length - 2 && !forceSplitThousand && Number(chunks[i + 1]) < 4) {
|
|
679
|
+
romanChunks.push(number2roman(val + Number(chunks[i + 1]) * 1000));
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
romanChunks.push(number2roman(val));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
switch (type) {
|
|
687
|
+
case 'js':
|
|
688
|
+
return romanChunks
|
|
689
|
+
.map((str, index) => str
|
|
690
|
+
.split('')
|
|
691
|
+
.map((s) => s + '\\\\u0305'.repeat(index))
|
|
692
|
+
.join(''))
|
|
693
|
+
.reverse()
|
|
694
|
+
.join('');
|
|
695
|
+
case 'html':
|
|
696
|
+
return romanChunks
|
|
697
|
+
.map((str, index) => str
|
|
698
|
+
.split('')
|
|
699
|
+
.map((s) => s + '̅'.repeat(index))
|
|
700
|
+
.join(''))
|
|
701
|
+
.reverse()
|
|
702
|
+
.join('');
|
|
703
|
+
case 'json':
|
|
704
|
+
return `[${romanChunks.map((str) => `"${str}"`).join(', ')}]`;
|
|
705
|
+
case 'unicode':
|
|
706
|
+
default:
|
|
707
|
+
return romanChunks
|
|
708
|
+
.map((str, index) => str
|
|
709
|
+
.split('')
|
|
710
|
+
.map((s) => s + '\u0305'.repeat(index))
|
|
711
|
+
.join(''))
|
|
712
|
+
.reverse()
|
|
713
|
+
.join('');
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
function number2roman(num) {
|
|
717
|
+
const symbols = [
|
|
718
|
+
[1000, 'M'],
|
|
719
|
+
[900, 'CM'],
|
|
720
|
+
[500, 'D'],
|
|
721
|
+
[400, 'CD'],
|
|
722
|
+
[100, 'C'],
|
|
723
|
+
[90, 'XC'],
|
|
724
|
+
[50, 'L'],
|
|
725
|
+
[40, 'XL'],
|
|
726
|
+
[10, 'X'],
|
|
727
|
+
[9, 'IX'],
|
|
728
|
+
[5, 'V'],
|
|
729
|
+
[4, 'IV'],
|
|
730
|
+
[1, 'I'],
|
|
731
|
+
];
|
|
732
|
+
let roman = '';
|
|
733
|
+
for (const [val, str] of symbols) {
|
|
734
|
+
while (num >= val) {
|
|
735
|
+
num -= val;
|
|
736
|
+
roman += str;
|
|
737
|
+
}
|
|
738
|
+
if (num == 0)
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
return roman;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function scientificNotation(num, options) {
|
|
745
|
+
const str = decimalNotation(num);
|
|
746
|
+
if (/NaN|Inf/.test(str))
|
|
747
|
+
return str;
|
|
748
|
+
const type = (options || {}).type || 'unicode';
|
|
749
|
+
const _precision = (options || {}).precision;
|
|
750
|
+
const precision = isNumber(_precision) ? clamp(_precision !== null && _precision !== void 0 ? _precision : 2, 0, Infinity) : null;
|
|
751
|
+
const round = (options || {}).round || 'round';
|
|
752
|
+
let [integer, fractional] = str.split('.');
|
|
753
|
+
let sign = '';
|
|
754
|
+
if (/^-/.test(integer)) {
|
|
755
|
+
integer = integer.substring(1);
|
|
756
|
+
sign = '-';
|
|
757
|
+
}
|
|
758
|
+
fractional = fractional !== null && fractional !== void 0 ? fractional : '';
|
|
759
|
+
let exp = 0;
|
|
760
|
+
let n = '';
|
|
761
|
+
if (integer === '0') {
|
|
762
|
+
exp = /^(0+)/.test(fractional) ? -(fractional.match(/^(0+)/)[0].length + 1) : -1;
|
|
763
|
+
[integer, fractional] = [fractional.slice(-exp - 1, -exp), fractional.slice(-exp)];
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
exp = integer.length - 1;
|
|
767
|
+
[integer, fractional] = [integer.slice(0, 1), integer.slice(1) + fractional];
|
|
768
|
+
}
|
|
769
|
+
if (isNumber(precision)) {
|
|
770
|
+
if (fractional.length > precision) {
|
|
771
|
+
const roundMap = { round: roundBase, banker: roundBank, floor: roundFloor, ceil: roundCeil };
|
|
772
|
+
[integer, fractional] = (roundMap[round] || roundBase)(integer, fractional, precision, !!sign);
|
|
773
|
+
}
|
|
774
|
+
else if (fractional.length < precision) {
|
|
775
|
+
fractional += '0'.repeat(precision - fractional.length);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
fractional = fractional.replace(/0+$/, '');
|
|
780
|
+
}
|
|
781
|
+
if ((precision === null && fractional) || (isNumber(precision) && precision > 0)) {
|
|
782
|
+
n = sign + integer + '.' + fractional;
|
|
783
|
+
}
|
|
784
|
+
else
|
|
785
|
+
n = sign + integer;
|
|
786
|
+
switch (type) {
|
|
787
|
+
case 'exp':
|
|
788
|
+
return `${n}e${exp < 0 ? '' : '+'}${exp}`;
|
|
789
|
+
case 'js':
|
|
790
|
+
return `${n}*10**${exp}`;
|
|
791
|
+
case 'code':
|
|
792
|
+
return `${n}*10^${exp}`;
|
|
793
|
+
case 'html':
|
|
794
|
+
return `${n}×10<sup>${exp}</sup>`;
|
|
795
|
+
case 'json':
|
|
796
|
+
return `{"number":"${n}","exp":${exp}}`;
|
|
797
|
+
case 'unicode':
|
|
798
|
+
default:
|
|
799
|
+
return `${n}×10${transferNumberToUniCode(String(exp))}`;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function transferNumberToUniCode(n) {
|
|
803
|
+
const strMap = Object.assign(Object.assign({}, Array.from('⁰¹²³⁴⁵⁶⁷⁸⁹')), { '-': '⁻', '+': '⁺' });
|
|
804
|
+
return Array.from(n)
|
|
805
|
+
.map((s) => (strMap[s] ? strMap[s] : s))
|
|
806
|
+
.join('');
|
|
807
|
+
}
|
|
808
|
+
|
|
375
809
|
function parallel(args, fn, options) {
|
|
376
810
|
return __awaiter(this, void 0, void 0, function* () {
|
|
377
811
|
if (!args.length)
|
|
@@ -1003,6 +1437,54 @@ function randomChoice(arr, weights) {
|
|
|
1003
1437
|
return arr[index];
|
|
1004
1438
|
}
|
|
1005
1439
|
|
|
1440
|
+
function randomDistribution(p, options) {
|
|
1441
|
+
const threshold = (options === null || options === void 0 ? void 0 : options.threshold) ? Math.max(options.threshold, Number.MIN_VALUE * 2) : 5e-6;
|
|
1442
|
+
const C = getInitP(p, threshold);
|
|
1443
|
+
let current = C;
|
|
1444
|
+
return function getRandomDistribution() {
|
|
1445
|
+
const res = Math.random() < current;
|
|
1446
|
+
if (res)
|
|
1447
|
+
current = C;
|
|
1448
|
+
else
|
|
1449
|
+
current += C;
|
|
1450
|
+
return res;
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
function getInitP(targetP, threshold = 5e-6) {
|
|
1454
|
+
if (targetP <= 0)
|
|
1455
|
+
return 0;
|
|
1456
|
+
if (targetP >= 1)
|
|
1457
|
+
return 1;
|
|
1458
|
+
let [down, up] = [0, 1];
|
|
1459
|
+
let mid = 1;
|
|
1460
|
+
let tempP = 1;
|
|
1461
|
+
let tempPLast = 1;
|
|
1462
|
+
let step = 64;
|
|
1463
|
+
while (step-- > 0) {
|
|
1464
|
+
mid = (down + up) / 2;
|
|
1465
|
+
tempP = getRealPFromInitP(mid);
|
|
1466
|
+
if (Math.abs(tempPLast - tempP) < threshold)
|
|
1467
|
+
break;
|
|
1468
|
+
if (tempP > targetP)
|
|
1469
|
+
up = mid;
|
|
1470
|
+
else
|
|
1471
|
+
down = mid;
|
|
1472
|
+
}
|
|
1473
|
+
return mid;
|
|
1474
|
+
}
|
|
1475
|
+
function getRealPFromInitP(initP) {
|
|
1476
|
+
let sum = 0;
|
|
1477
|
+
let prod = 0;
|
|
1478
|
+
let cur = 0;
|
|
1479
|
+
let max = Math.ceil(1 / initP);
|
|
1480
|
+
for (let n = 1; n <= max; n++) {
|
|
1481
|
+
cur = Math.min(1, n * initP) * (1 - prod);
|
|
1482
|
+
prod += cur;
|
|
1483
|
+
sum += n * cur;
|
|
1484
|
+
}
|
|
1485
|
+
return 1 / sum;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1006
1488
|
const radix32 = '0123456789abcdefghijklmnopqrstuv';
|
|
1007
1489
|
const base32Chars = 'abcdefghijklmnopqrstuvwxyz234567';
|
|
1008
1490
|
const base32Crockford = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
@@ -1629,22 +2111,22 @@ function _deepClone(obj, map, options) {
|
|
|
1629
2111
|
if (isFormData(obj))
|
|
1630
2112
|
return _cloneFormData(obj, map, _deepClone, options);
|
|
1631
2113
|
let res;
|
|
1632
|
-
if (obj
|
|
2114
|
+
if (isDate(obj)) {
|
|
1633
2115
|
res = new Date(obj.valueOf());
|
|
1634
2116
|
map.set(obj, res);
|
|
1635
2117
|
}
|
|
1636
|
-
else if (obj
|
|
2118
|
+
else if (isRegExp(obj)) {
|
|
1637
2119
|
res = new RegExp(obj.source, obj.flags);
|
|
1638
2120
|
map.set(obj, res);
|
|
1639
2121
|
}
|
|
1640
|
-
else if (obj
|
|
2122
|
+
else if (isArrayBuffer(obj)) {
|
|
1641
2123
|
res = _cloneArrayBuffer(obj, map);
|
|
1642
2124
|
}
|
|
1643
2125
|
else if (isTypedArray(obj)) {
|
|
1644
2126
|
res = new obj.constructor(_cloneArrayBuffer(obj.buffer, map), obj.byteOffset, obj.length);
|
|
1645
2127
|
map.set(obj, res);
|
|
1646
2128
|
}
|
|
1647
|
-
else if (obj
|
|
2129
|
+
else if (isDataView(obj)) {
|
|
1648
2130
|
res = new DataView(map.has(obj.buffer) ? map.get(obj.buffer) : _cloneArrayBuffer(obj.buffer, map), obj.byteOffset, obj.byteLength);
|
|
1649
2131
|
map.set(obj, res);
|
|
1650
2132
|
}
|
|
@@ -2040,4 +2522,4 @@ function throttle(fn, delay, options) {
|
|
|
2040
2522
|
return _throttle(fn, delay, Object.assign({ trailing: false, leading: true }, options));
|
|
2041
2523
|
}
|
|
2042
2524
|
|
|
2043
|
-
export { $$Empty, _, acceptableFileName, acceptableFileType, camelCase, capitalize, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, clamp, compose, _curryMore as curry, debounce, dedent, deepClone, deepMerge, defer, fastClone, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getTag, indent, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBlob, isBoolean, isBuffer, isDataView, isDate, isEmpty, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isMergeEmptyPlaceholder, isNil, isNull, isNumber, isObject, isPlaceholder, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, memo, noop, not, parallel, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomHexString, randomInt, randomIntFloor, randomString, range, remove, retry, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uncapitalize, uuidNil, uuidV4, withResolvers };
|
|
2525
|
+
export { $$Empty, _, acceptableFileName, acceptableFileType, camelCase, capitalize, cartesianProduct, caseCamel, caseConvert, caseKebab, casePascal, caseSnake, castArray, chunk, clamp, compose, _curryMore as curry, debounce, decimalNotation, dedent, deepClone, deepMerge, defer, fastClone, format, getAcceptableExtByMIME, getAcceptableMIMEByExt, getGlobalThis, getInitP, getTag, indent, isArray, isArrayBuffer, isArrayLike, isBigInt, isBigInt64Array, isBigUint64Array, isBlob, isBoolean, isBuffer, isDataView, isDate, isEmpty, isEven, isFile, isFloat32Array, isFloat64Array, isFormData, isFunction, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isMap, isMergeEmptyPlaceholder, isNil, isNull, isNumber, isObject, isOdd, isPlaceholder, isPlainObject, isPrimitive, isPromise, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isWrapperBigInt, isWrapperBoolean, isWrapperNumber, isWrapperObject, isWrapperString, isWrapperSymbol, kebabCase, lerp, memo, noop, not, parallel, pascalCase, pass, passWith, pipe, randomBase32String, randomChoice, randomDistribution, randomHexString, randomInt, randomIntFloor, randomString, range, remove, retry, romanNumerals, round, roundBank, roundBase, roundCeil, roundFloor, scientificNotation, shuffle, sleep, snakeCase, splitWords, throttle, titleCase, tryit, ulid, uncapitalize, uuidNil, uuidV4, withResolvers };
|