radashi 12.2.0-beta.dc9ade1 → 12.2.0-beta.ef0154b
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/radashi.cjs +59 -25
- package/dist/radashi.d.cts +93 -7
- package/dist/radashi.d.ts +93 -7
- package/dist/radashi.js +56 -25
- package/package.json +1 -1
package/dist/radashi.cjs
CHANGED
|
@@ -43,6 +43,7 @@ __export(mod_exports, {
|
|
|
43
43
|
filterKey: () => filterKey,
|
|
44
44
|
first: () => first,
|
|
45
45
|
flat: () => flat,
|
|
46
|
+
flip: () => flip,
|
|
46
47
|
fork: () => fork,
|
|
47
48
|
get: () => get,
|
|
48
49
|
group: () => group,
|
|
@@ -68,6 +69,7 @@ __export(mod_exports, {
|
|
|
68
69
|
iterate: () => iterate,
|
|
69
70
|
keys: () => keys,
|
|
70
71
|
last: () => last,
|
|
72
|
+
lerp: () => lerp,
|
|
71
73
|
list: () => list,
|
|
72
74
|
listify: () => listify,
|
|
73
75
|
lowerize: () => lowerize,
|
|
@@ -82,6 +84,7 @@ __export(mod_exports, {
|
|
|
82
84
|
min: () => min,
|
|
83
85
|
objectify: () => objectify,
|
|
84
86
|
omit: () => omit,
|
|
87
|
+
once: () => once,
|
|
85
88
|
parallel: () => parallel,
|
|
86
89
|
partial: () => partial,
|
|
87
90
|
partob: () => partob,
|
|
@@ -657,7 +660,7 @@ async function retry(options, func) {
|
|
|
657
660
|
if (err._exited) {
|
|
658
661
|
throw err._exited;
|
|
659
662
|
}
|
|
660
|
-
if (++i
|
|
663
|
+
if (++i >= times) {
|
|
661
664
|
throw err;
|
|
662
665
|
}
|
|
663
666
|
if (delay) {
|
|
@@ -739,6 +742,11 @@ function debounce({ delay }, func) {
|
|
|
739
742
|
return debounced;
|
|
740
743
|
}
|
|
741
744
|
|
|
745
|
+
// src/curry/flip.ts
|
|
746
|
+
function flip(fn) {
|
|
747
|
+
return (arg2, arg1, ...args) => fn(arg1, arg2, ...args);
|
|
748
|
+
}
|
|
749
|
+
|
|
742
750
|
// src/curry/memo.ts
|
|
743
751
|
function memoize(cache, func, keyFunc, ttl) {
|
|
744
752
|
return function callWithMemo(...args) {
|
|
@@ -764,6 +772,22 @@ function memo(func, options = {}) {
|
|
|
764
772
|
return memoize({}, func, options.key ?? null, options.ttl ?? null);
|
|
765
773
|
}
|
|
766
774
|
|
|
775
|
+
// src/curry/once.ts
|
|
776
|
+
var onceSymbol = Symbol();
|
|
777
|
+
var once = (fn) => {
|
|
778
|
+
const onceFn = function(...args) {
|
|
779
|
+
if (onceFn[onceSymbol] === onceSymbol) {
|
|
780
|
+
onceFn[onceSymbol] = fn.apply(this, args);
|
|
781
|
+
}
|
|
782
|
+
return onceFn[onceSymbol];
|
|
783
|
+
};
|
|
784
|
+
onceFn[onceSymbol] = onceSymbol;
|
|
785
|
+
return onceFn;
|
|
786
|
+
};
|
|
787
|
+
once.reset = (fn) => {
|
|
788
|
+
fn[onceSymbol] = onceSymbol;
|
|
789
|
+
};
|
|
790
|
+
|
|
767
791
|
// src/curry/partial.ts
|
|
768
792
|
function partial(fn, ...args) {
|
|
769
793
|
return (...rest) => fn(...[...args, ...rest]);
|
|
@@ -818,6 +842,11 @@ function inRange(number, start, end) {
|
|
|
818
842
|
return number >= Math.min(start, end) && number < Math.max(start, end);
|
|
819
843
|
}
|
|
820
844
|
|
|
845
|
+
// src/number/lerp.ts
|
|
846
|
+
function lerp(from, to, amount) {
|
|
847
|
+
return from + (to - from) * amount;
|
|
848
|
+
}
|
|
849
|
+
|
|
821
850
|
// src/number/round.ts
|
|
822
851
|
function round(value, precision, toInteger = Math.round) {
|
|
823
852
|
if (precision) {
|
|
@@ -831,22 +860,14 @@ function round(value, precision, toInteger = Math.round) {
|
|
|
831
860
|
|
|
832
861
|
// src/number/toFloat.ts
|
|
833
862
|
function toFloat(value, defaultValue) {
|
|
834
|
-
const
|
|
835
|
-
|
|
836
|
-
return def;
|
|
837
|
-
}
|
|
838
|
-
const result = Number.parseFloat(value);
|
|
839
|
-
return Number.isNaN(result) ? def : result;
|
|
863
|
+
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseFloat(value);
|
|
864
|
+
return Number.isNaN(parsedValue) ? defaultValue !== void 0 ? defaultValue : 0 : parsedValue;
|
|
840
865
|
}
|
|
841
866
|
|
|
842
867
|
// src/number/toInt.ts
|
|
843
868
|
function toInt(value, defaultValue) {
|
|
844
|
-
const
|
|
845
|
-
|
|
846
|
-
return def;
|
|
847
|
-
}
|
|
848
|
-
const result = Number.parseInt(value);
|
|
849
|
-
return Number.isNaN(result) ? def : result;
|
|
869
|
+
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseInt(value);
|
|
870
|
+
return Number.isNaN(parsedValue) ? defaultValue !== void 0 ? defaultValue : 0 : parsedValue;
|
|
850
871
|
}
|
|
851
872
|
|
|
852
873
|
// src/object/assign.ts
|
|
@@ -1050,19 +1071,22 @@ function omit(obj, keys2) {
|
|
|
1050
1071
|
}
|
|
1051
1072
|
|
|
1052
1073
|
// src/object/pick.ts
|
|
1053
|
-
function pick(obj,
|
|
1074
|
+
function pick(obj, filter) {
|
|
1054
1075
|
if (!obj) {
|
|
1055
1076
|
return {};
|
|
1056
1077
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
{
|
|
1065
|
-
|
|
1078
|
+
let keys2 = filter;
|
|
1079
|
+
if (isArray(filter)) {
|
|
1080
|
+
filter = null;
|
|
1081
|
+
} else {
|
|
1082
|
+
keys2 = Reflect.ownKeys(obj);
|
|
1083
|
+
}
|
|
1084
|
+
return keys2.reduce((acc, key) => {
|
|
1085
|
+
if (filterKey(obj, key, filter)) {
|
|
1086
|
+
acc[key] = obj[key];
|
|
1087
|
+
}
|
|
1088
|
+
return acc;
|
|
1089
|
+
}, {});
|
|
1066
1090
|
}
|
|
1067
1091
|
|
|
1068
1092
|
// src/object/set.ts
|
|
@@ -1119,8 +1143,15 @@ function random(min2, max2) {
|
|
|
1119
1143
|
}
|
|
1120
1144
|
|
|
1121
1145
|
// src/random/shuffle.ts
|
|
1122
|
-
function shuffle(array) {
|
|
1123
|
-
|
|
1146
|
+
function shuffle(array, random2 = random) {
|
|
1147
|
+
const newArray = array.slice();
|
|
1148
|
+
for (let idx = 0, randomIdx, item; idx < array.length; idx++) {
|
|
1149
|
+
randomIdx = random2(0, array.length - 1);
|
|
1150
|
+
item = newArray[randomIdx];
|
|
1151
|
+
newArray[randomIdx] = newArray[idx];
|
|
1152
|
+
newArray[idx] = item;
|
|
1153
|
+
}
|
|
1154
|
+
return newArray;
|
|
1124
1155
|
}
|
|
1125
1156
|
|
|
1126
1157
|
// src/random/uid.ts
|
|
@@ -1444,6 +1475,7 @@ function isSymbol(value) {
|
|
|
1444
1475
|
filterKey,
|
|
1445
1476
|
first,
|
|
1446
1477
|
flat,
|
|
1478
|
+
flip,
|
|
1447
1479
|
fork,
|
|
1448
1480
|
get,
|
|
1449
1481
|
group,
|
|
@@ -1469,6 +1501,7 @@ function isSymbol(value) {
|
|
|
1469
1501
|
iterate,
|
|
1470
1502
|
keys,
|
|
1471
1503
|
last,
|
|
1504
|
+
lerp,
|
|
1472
1505
|
list,
|
|
1473
1506
|
listify,
|
|
1474
1507
|
lowerize,
|
|
@@ -1483,6 +1516,7 @@ function isSymbol(value) {
|
|
|
1483
1516
|
min,
|
|
1484
1517
|
objectify,
|
|
1485
1518
|
omit,
|
|
1519
|
+
once,
|
|
1486
1520
|
parallel,
|
|
1487
1521
|
partial,
|
|
1488
1522
|
partob,
|
package/dist/radashi.d.cts
CHANGED
|
@@ -274,7 +274,8 @@ declare function replaceOrAppend<T>(array: readonly T[], newItem: T, match: (a:
|
|
|
274
274
|
* // => [9, 16]
|
|
275
275
|
* ```
|
|
276
276
|
*/
|
|
277
|
-
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U, condition
|
|
277
|
+
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U, condition: (item: T, index: number) => boolean): U[];
|
|
278
|
+
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U | null | undefined): U[];
|
|
278
279
|
|
|
279
280
|
/**
|
|
280
281
|
* Select performs a find + map operation, short-circuiting on the first
|
|
@@ -621,6 +622,9 @@ declare function debounce<TArgs extends any[]>({ delay }: {
|
|
|
621
622
|
delay: number;
|
|
622
623
|
}, func: (...args: TArgs) => any): DebounceFunction<TArgs>;
|
|
623
624
|
|
|
625
|
+
declare function flip<Args extends any[], Result>(fn: (...args: Args) => Result): (...args: Flip<Args>) => Result;
|
|
626
|
+
type Flip<T extends any[]> = T extends [infer A, infer B, ...infer R] ? [B, A, ...R] : never;
|
|
627
|
+
|
|
624
628
|
interface MemoOptions<TArgs extends any[]> {
|
|
625
629
|
key?: (...args: TArgs) => string;
|
|
626
630
|
ttl?: number;
|
|
@@ -633,6 +637,45 @@ interface MemoOptions<TArgs extends any[]> {
|
|
|
633
637
|
*/
|
|
634
638
|
declare function memo<TArgs extends any[], TResult>(func: (...args: TArgs) => TResult, options?: MemoOptions<TArgs>): (...args: TArgs) => TResult;
|
|
635
639
|
|
|
640
|
+
declare const onceSymbol: unique symbol;
|
|
641
|
+
/**
|
|
642
|
+
* The type of a function wrapped with `once`.
|
|
643
|
+
*/
|
|
644
|
+
interface OnceFunction<Args extends unknown[] = unknown[], Return = unknown, This = unknown> {
|
|
645
|
+
(this: This, ...args: Args): Return;
|
|
646
|
+
[onceSymbol]?: Return | typeof onceSymbol;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Create a function that runs at most once, no matter how many times
|
|
650
|
+
* it's called. If it was already called before, returns the result
|
|
651
|
+
* from the first call. This is a lighter version of `memo()`.
|
|
652
|
+
*
|
|
653
|
+
* To allow the function to be called again, use `onceReset()`.
|
|
654
|
+
*
|
|
655
|
+
* ```ts
|
|
656
|
+
* const fn = once(() => Math.random())
|
|
657
|
+
* fn() // 0.5
|
|
658
|
+
* fn() // 0.5
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
661
|
+
declare const once: {
|
|
662
|
+
<Args extends unknown[], Return, This = unknown>(fn: (this: This, ...args: Args) => Return): (this: This, ...args: Args) => Return;
|
|
663
|
+
/**
|
|
664
|
+
* Reset the result of a function that was created with `once`,
|
|
665
|
+
* allowing it to be called again.
|
|
666
|
+
*
|
|
667
|
+
* ```ts
|
|
668
|
+
* const fn = once(() => Math.random())
|
|
669
|
+
* fn() // 0.5
|
|
670
|
+
* fn() // 0.5
|
|
671
|
+
* once.reset(fn)
|
|
672
|
+
* fn() // 0.3
|
|
673
|
+
* fn() // 0.3
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
reset(fn: OnceFunction): void;
|
|
677
|
+
};
|
|
678
|
+
|
|
636
679
|
/**
|
|
637
680
|
* This type produces the type array of `TItems` with all the type items
|
|
638
681
|
* in `TItemsToRemove` removed from the start of the array type.
|
|
@@ -716,6 +759,17 @@ declare function inRange(number: number, end: number): boolean;
|
|
|
716
759
|
*/
|
|
717
760
|
declare function inRange(number: number, start: number, end: number): boolean;
|
|
718
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Linearly interpolates between two numbers.
|
|
764
|
+
*
|
|
765
|
+
* ```
|
|
766
|
+
* lerp(0, 10, 0.5) // => 5
|
|
767
|
+
* lerp(5, 15, 0.2) // => 7
|
|
768
|
+
* lerp(-10, 10, 0.75) // => 5
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
declare function lerp(from: number, to: number, amount: number): number;
|
|
772
|
+
|
|
719
773
|
/**
|
|
720
774
|
* Rounds a number to the given precision. The default `precision` is
|
|
721
775
|
* zero. An optional rounding function (e.g. `Math.floor` or
|
|
@@ -741,9 +795,13 @@ declare function inRange(number: number, start: number, end: number): boolean;
|
|
|
741
795
|
*/
|
|
742
796
|
declare function round(value: number, precision?: number, toInteger?: (value: number) => number): number;
|
|
743
797
|
|
|
744
|
-
declare function toFloat
|
|
798
|
+
declare function toFloat(value: unknown): number;
|
|
799
|
+
declare function toFloat(value: unknown, defaultValue: number): number;
|
|
800
|
+
declare function toFloat<T>(value: unknown, defaultValue: T): number | Exclude<T, undefined>;
|
|
745
801
|
|
|
746
|
-
declare function toInt
|
|
802
|
+
declare function toInt(value: unknown): number;
|
|
803
|
+
declare function toInt(value: unknown, defaultValue: number): number;
|
|
804
|
+
declare function toInt<T>(value: unknown, defaultValue: T): number | Exclude<T, undefined>;
|
|
747
805
|
|
|
748
806
|
/**
|
|
749
807
|
* Merges two objects together recursivly into a new object applying
|
|
@@ -789,10 +847,15 @@ type KeyFilterFunction<T extends object = object> = (value: ValueOf<T>, key: Key
|
|
|
789
847
|
* filter function.
|
|
790
848
|
*/
|
|
791
849
|
type KeyFilter<T extends object = object, Key extends keyof any = keyof any> = KeyFilterFunction<T> | readonly Key[];
|
|
850
|
+
/**
|
|
851
|
+
* Extract the keys of an object that pass a filter.
|
|
852
|
+
*/
|
|
853
|
+
type FilteredKeys<T extends object, F extends KeyFilter<T> | null | undefined> = Extract<keyof T, F extends readonly any[] ? F[number] : any>;
|
|
792
854
|
/**
|
|
793
855
|
* Returns true if the key is in the “keys array” or if the “filter
|
|
794
856
|
* function” returns true.
|
|
795
857
|
*/
|
|
858
|
+
declare function filterKey<T extends object>(obj: T, key: keyof T, filter: KeyFilter<T, keyof T> | null | undefined): boolean;
|
|
796
859
|
declare function filterKey(obj: object, key: keyof any, filter: KeyFilter | null | undefined): boolean;
|
|
797
860
|
|
|
798
861
|
/**
|
|
@@ -875,7 +938,7 @@ declare function omit<T, TKeys extends keyof T>(obj: T, keys: TKeys[]): Omit<T,
|
|
|
875
938
|
/**
|
|
876
939
|
* Pick a list of properties from an object into a new object
|
|
877
940
|
*/
|
|
878
|
-
declare function pick<T extends object,
|
|
941
|
+
declare function pick<T extends object, F extends KeyFilter<T, keyof T>>(obj: T, filter: F): Pick<T, FilteredKeys<T, F>>;
|
|
879
942
|
|
|
880
943
|
/**
|
|
881
944
|
* Opposite of get, dynamically set a nested value into an object
|
|
@@ -912,7 +975,7 @@ declare function draw<T>(array: readonly T[]): T | null;
|
|
|
912
975
|
*/
|
|
913
976
|
declare function random(min: number, max: number): number;
|
|
914
977
|
|
|
915
|
-
declare function shuffle<T>(array: readonly T[]): T[];
|
|
978
|
+
declare function shuffle<T>(array: readonly T[], random?: (min: number, max: number) => number): T[];
|
|
916
979
|
|
|
917
980
|
declare function uid(length: number, specials?: string): string;
|
|
918
981
|
|
|
@@ -1010,7 +1073,7 @@ declare function title(str: string | null | undefined): string;
|
|
|
1010
1073
|
*/
|
|
1011
1074
|
declare function trim(str: string | null | undefined, charsToTrim?: string): string;
|
|
1012
1075
|
|
|
1013
|
-
declare const isArray: (value:
|
|
1076
|
+
declare const isArray: <Input>(value: Input) => value is readonly any[] extends ExtractNotAny<Input, readonly any[]> ? Extract<Input, readonly any[]> : any[] extends ExtractNotAny<Input, any[]> ? Extract<Input, any[]> : unknown[] extends Input ? unknown[] : never;
|
|
1014
1077
|
|
|
1015
1078
|
declare function isDate(value: any): value is Date;
|
|
1016
1079
|
|
|
@@ -1054,4 +1117,27 @@ declare function isString(value: any): value is string;
|
|
|
1054
1117
|
|
|
1055
1118
|
declare function isSymbol(value: any): value is symbol;
|
|
1056
1119
|
|
|
1057
|
-
|
|
1120
|
+
/**
|
|
1121
|
+
* The `Any` class does not exist at runtime. It's used in type
|
|
1122
|
+
* definitions to detect an `any` type.
|
|
1123
|
+
*
|
|
1124
|
+
* ```ts
|
|
1125
|
+
* type IsAny<T> = [T] extends [Any] ? 'is any' : 'is not any'
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
1128
|
+
declare class Any {
|
|
1129
|
+
private any;
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Extracts `T` if `T` is not `any`, otherwise `never`.
|
|
1133
|
+
*
|
|
1134
|
+
* ```ts
|
|
1135
|
+
* type A = ExtractNotAny<any, string>
|
|
1136
|
+
* // ^? never
|
|
1137
|
+
* type B = ExtractNotAny<string | number, string>
|
|
1138
|
+
* // ^? string
|
|
1139
|
+
* ```
|
|
1140
|
+
*/
|
|
1141
|
+
type ExtractNotAny<T, U> = Extract<[T] extends [Any] ? never : T, U>;
|
|
1142
|
+
|
|
1143
|
+
export { AggregateError, Any, type DebounceFunction, type ExtractNotAny, type FilteredKeys, type Flip, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MemoOptions, type OnceFunction, type RetryOptions, type Series, type ThrottledFunction, type TryitResult, type UppercaseKeys, all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isIntString, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, unzip, upperize, zip, zipToObject };
|
package/dist/radashi.d.ts
CHANGED
|
@@ -274,7 +274,8 @@ declare function replaceOrAppend<T>(array: readonly T[], newItem: T, match: (a:
|
|
|
274
274
|
* // => [9, 16]
|
|
275
275
|
* ```
|
|
276
276
|
*/
|
|
277
|
-
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U, condition
|
|
277
|
+
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U, condition: (item: T, index: number) => boolean): U[];
|
|
278
|
+
declare function select<T, U>(array: readonly T[], mapper: (item: T, index: number) => U | null | undefined): U[];
|
|
278
279
|
|
|
279
280
|
/**
|
|
280
281
|
* Select performs a find + map operation, short-circuiting on the first
|
|
@@ -621,6 +622,9 @@ declare function debounce<TArgs extends any[]>({ delay }: {
|
|
|
621
622
|
delay: number;
|
|
622
623
|
}, func: (...args: TArgs) => any): DebounceFunction<TArgs>;
|
|
623
624
|
|
|
625
|
+
declare function flip<Args extends any[], Result>(fn: (...args: Args) => Result): (...args: Flip<Args>) => Result;
|
|
626
|
+
type Flip<T extends any[]> = T extends [infer A, infer B, ...infer R] ? [B, A, ...R] : never;
|
|
627
|
+
|
|
624
628
|
interface MemoOptions<TArgs extends any[]> {
|
|
625
629
|
key?: (...args: TArgs) => string;
|
|
626
630
|
ttl?: number;
|
|
@@ -633,6 +637,45 @@ interface MemoOptions<TArgs extends any[]> {
|
|
|
633
637
|
*/
|
|
634
638
|
declare function memo<TArgs extends any[], TResult>(func: (...args: TArgs) => TResult, options?: MemoOptions<TArgs>): (...args: TArgs) => TResult;
|
|
635
639
|
|
|
640
|
+
declare const onceSymbol: unique symbol;
|
|
641
|
+
/**
|
|
642
|
+
* The type of a function wrapped with `once`.
|
|
643
|
+
*/
|
|
644
|
+
interface OnceFunction<Args extends unknown[] = unknown[], Return = unknown, This = unknown> {
|
|
645
|
+
(this: This, ...args: Args): Return;
|
|
646
|
+
[onceSymbol]?: Return | typeof onceSymbol;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Create a function that runs at most once, no matter how many times
|
|
650
|
+
* it's called. If it was already called before, returns the result
|
|
651
|
+
* from the first call. This is a lighter version of `memo()`.
|
|
652
|
+
*
|
|
653
|
+
* To allow the function to be called again, use `onceReset()`.
|
|
654
|
+
*
|
|
655
|
+
* ```ts
|
|
656
|
+
* const fn = once(() => Math.random())
|
|
657
|
+
* fn() // 0.5
|
|
658
|
+
* fn() // 0.5
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
661
|
+
declare const once: {
|
|
662
|
+
<Args extends unknown[], Return, This = unknown>(fn: (this: This, ...args: Args) => Return): (this: This, ...args: Args) => Return;
|
|
663
|
+
/**
|
|
664
|
+
* Reset the result of a function that was created with `once`,
|
|
665
|
+
* allowing it to be called again.
|
|
666
|
+
*
|
|
667
|
+
* ```ts
|
|
668
|
+
* const fn = once(() => Math.random())
|
|
669
|
+
* fn() // 0.5
|
|
670
|
+
* fn() // 0.5
|
|
671
|
+
* once.reset(fn)
|
|
672
|
+
* fn() // 0.3
|
|
673
|
+
* fn() // 0.3
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
reset(fn: OnceFunction): void;
|
|
677
|
+
};
|
|
678
|
+
|
|
636
679
|
/**
|
|
637
680
|
* This type produces the type array of `TItems` with all the type items
|
|
638
681
|
* in `TItemsToRemove` removed from the start of the array type.
|
|
@@ -716,6 +759,17 @@ declare function inRange(number: number, end: number): boolean;
|
|
|
716
759
|
*/
|
|
717
760
|
declare function inRange(number: number, start: number, end: number): boolean;
|
|
718
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Linearly interpolates between two numbers.
|
|
764
|
+
*
|
|
765
|
+
* ```
|
|
766
|
+
* lerp(0, 10, 0.5) // => 5
|
|
767
|
+
* lerp(5, 15, 0.2) // => 7
|
|
768
|
+
* lerp(-10, 10, 0.75) // => 5
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
declare function lerp(from: number, to: number, amount: number): number;
|
|
772
|
+
|
|
719
773
|
/**
|
|
720
774
|
* Rounds a number to the given precision. The default `precision` is
|
|
721
775
|
* zero. An optional rounding function (e.g. `Math.floor` or
|
|
@@ -741,9 +795,13 @@ declare function inRange(number: number, start: number, end: number): boolean;
|
|
|
741
795
|
*/
|
|
742
796
|
declare function round(value: number, precision?: number, toInteger?: (value: number) => number): number;
|
|
743
797
|
|
|
744
|
-
declare function toFloat
|
|
798
|
+
declare function toFloat(value: unknown): number;
|
|
799
|
+
declare function toFloat(value: unknown, defaultValue: number): number;
|
|
800
|
+
declare function toFloat<T>(value: unknown, defaultValue: T): number | Exclude<T, undefined>;
|
|
745
801
|
|
|
746
|
-
declare function toInt
|
|
802
|
+
declare function toInt(value: unknown): number;
|
|
803
|
+
declare function toInt(value: unknown, defaultValue: number): number;
|
|
804
|
+
declare function toInt<T>(value: unknown, defaultValue: T): number | Exclude<T, undefined>;
|
|
747
805
|
|
|
748
806
|
/**
|
|
749
807
|
* Merges two objects together recursivly into a new object applying
|
|
@@ -789,10 +847,15 @@ type KeyFilterFunction<T extends object = object> = (value: ValueOf<T>, key: Key
|
|
|
789
847
|
* filter function.
|
|
790
848
|
*/
|
|
791
849
|
type KeyFilter<T extends object = object, Key extends keyof any = keyof any> = KeyFilterFunction<T> | readonly Key[];
|
|
850
|
+
/**
|
|
851
|
+
* Extract the keys of an object that pass a filter.
|
|
852
|
+
*/
|
|
853
|
+
type FilteredKeys<T extends object, F extends KeyFilter<T> | null | undefined> = Extract<keyof T, F extends readonly any[] ? F[number] : any>;
|
|
792
854
|
/**
|
|
793
855
|
* Returns true if the key is in the “keys array” or if the “filter
|
|
794
856
|
* function” returns true.
|
|
795
857
|
*/
|
|
858
|
+
declare function filterKey<T extends object>(obj: T, key: keyof T, filter: KeyFilter<T, keyof T> | null | undefined): boolean;
|
|
796
859
|
declare function filterKey(obj: object, key: keyof any, filter: KeyFilter | null | undefined): boolean;
|
|
797
860
|
|
|
798
861
|
/**
|
|
@@ -875,7 +938,7 @@ declare function omit<T, TKeys extends keyof T>(obj: T, keys: TKeys[]): Omit<T,
|
|
|
875
938
|
/**
|
|
876
939
|
* Pick a list of properties from an object into a new object
|
|
877
940
|
*/
|
|
878
|
-
declare function pick<T extends object,
|
|
941
|
+
declare function pick<T extends object, F extends KeyFilter<T, keyof T>>(obj: T, filter: F): Pick<T, FilteredKeys<T, F>>;
|
|
879
942
|
|
|
880
943
|
/**
|
|
881
944
|
* Opposite of get, dynamically set a nested value into an object
|
|
@@ -912,7 +975,7 @@ declare function draw<T>(array: readonly T[]): T | null;
|
|
|
912
975
|
*/
|
|
913
976
|
declare function random(min: number, max: number): number;
|
|
914
977
|
|
|
915
|
-
declare function shuffle<T>(array: readonly T[]): T[];
|
|
978
|
+
declare function shuffle<T>(array: readonly T[], random?: (min: number, max: number) => number): T[];
|
|
916
979
|
|
|
917
980
|
declare function uid(length: number, specials?: string): string;
|
|
918
981
|
|
|
@@ -1010,7 +1073,7 @@ declare function title(str: string | null | undefined): string;
|
|
|
1010
1073
|
*/
|
|
1011
1074
|
declare function trim(str: string | null | undefined, charsToTrim?: string): string;
|
|
1012
1075
|
|
|
1013
|
-
declare const isArray: (value:
|
|
1076
|
+
declare const isArray: <Input>(value: Input) => value is readonly any[] extends ExtractNotAny<Input, readonly any[]> ? Extract<Input, readonly any[]> : any[] extends ExtractNotAny<Input, any[]> ? Extract<Input, any[]> : unknown[] extends Input ? unknown[] : never;
|
|
1014
1077
|
|
|
1015
1078
|
declare function isDate(value: any): value is Date;
|
|
1016
1079
|
|
|
@@ -1054,4 +1117,27 @@ declare function isString(value: any): value is string;
|
|
|
1054
1117
|
|
|
1055
1118
|
declare function isSymbol(value: any): value is symbol;
|
|
1056
1119
|
|
|
1057
|
-
|
|
1120
|
+
/**
|
|
1121
|
+
* The `Any` class does not exist at runtime. It's used in type
|
|
1122
|
+
* definitions to detect an `any` type.
|
|
1123
|
+
*
|
|
1124
|
+
* ```ts
|
|
1125
|
+
* type IsAny<T> = [T] extends [Any] ? 'is any' : 'is not any'
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
1128
|
+
declare class Any {
|
|
1129
|
+
private any;
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Extracts `T` if `T` is not `any`, otherwise `never`.
|
|
1133
|
+
*
|
|
1134
|
+
* ```ts
|
|
1135
|
+
* type A = ExtractNotAny<any, string>
|
|
1136
|
+
* // ^? never
|
|
1137
|
+
* type B = ExtractNotAny<string | number, string>
|
|
1138
|
+
* // ^? string
|
|
1139
|
+
* ```
|
|
1140
|
+
*/
|
|
1141
|
+
type ExtractNotAny<T, U> = Extract<[T] extends [Any] ? never : T, U>;
|
|
1142
|
+
|
|
1143
|
+
export { AggregateError, Any, type DebounceFunction, type ExtractNotAny, type FilteredKeys, type Flip, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MemoOptions, type OnceFunction, type RetryOptions, type Series, type ThrottledFunction, type TryitResult, type UppercaseKeys, all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isIntString, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, unzip, upperize, zip, zipToObject };
|
package/dist/radashi.js
CHANGED
|
@@ -530,7 +530,7 @@ async function retry(options, func) {
|
|
|
530
530
|
if (err._exited) {
|
|
531
531
|
throw err._exited;
|
|
532
532
|
}
|
|
533
|
-
if (++i
|
|
533
|
+
if (++i >= times) {
|
|
534
534
|
throw err;
|
|
535
535
|
}
|
|
536
536
|
if (delay) {
|
|
@@ -612,6 +612,11 @@ function debounce({ delay }, func) {
|
|
|
612
612
|
return debounced;
|
|
613
613
|
}
|
|
614
614
|
|
|
615
|
+
// src/curry/flip.ts
|
|
616
|
+
function flip(fn) {
|
|
617
|
+
return (arg2, arg1, ...args) => fn(arg1, arg2, ...args);
|
|
618
|
+
}
|
|
619
|
+
|
|
615
620
|
// src/curry/memo.ts
|
|
616
621
|
function memoize(cache, func, keyFunc, ttl) {
|
|
617
622
|
return function callWithMemo(...args) {
|
|
@@ -637,6 +642,22 @@ function memo(func, options = {}) {
|
|
|
637
642
|
return memoize({}, func, options.key ?? null, options.ttl ?? null);
|
|
638
643
|
}
|
|
639
644
|
|
|
645
|
+
// src/curry/once.ts
|
|
646
|
+
var onceSymbol = Symbol();
|
|
647
|
+
var once = (fn) => {
|
|
648
|
+
const onceFn = function(...args) {
|
|
649
|
+
if (onceFn[onceSymbol] === onceSymbol) {
|
|
650
|
+
onceFn[onceSymbol] = fn.apply(this, args);
|
|
651
|
+
}
|
|
652
|
+
return onceFn[onceSymbol];
|
|
653
|
+
};
|
|
654
|
+
onceFn[onceSymbol] = onceSymbol;
|
|
655
|
+
return onceFn;
|
|
656
|
+
};
|
|
657
|
+
once.reset = (fn) => {
|
|
658
|
+
fn[onceSymbol] = onceSymbol;
|
|
659
|
+
};
|
|
660
|
+
|
|
640
661
|
// src/curry/partial.ts
|
|
641
662
|
function partial(fn, ...args) {
|
|
642
663
|
return (...rest) => fn(...[...args, ...rest]);
|
|
@@ -691,6 +712,11 @@ function inRange(number, start, end) {
|
|
|
691
712
|
return number >= Math.min(start, end) && number < Math.max(start, end);
|
|
692
713
|
}
|
|
693
714
|
|
|
715
|
+
// src/number/lerp.ts
|
|
716
|
+
function lerp(from, to, amount) {
|
|
717
|
+
return from + (to - from) * amount;
|
|
718
|
+
}
|
|
719
|
+
|
|
694
720
|
// src/number/round.ts
|
|
695
721
|
function round(value, precision, toInteger = Math.round) {
|
|
696
722
|
if (precision) {
|
|
@@ -704,22 +730,14 @@ function round(value, precision, toInteger = Math.round) {
|
|
|
704
730
|
|
|
705
731
|
// src/number/toFloat.ts
|
|
706
732
|
function toFloat(value, defaultValue) {
|
|
707
|
-
const
|
|
708
|
-
|
|
709
|
-
return def;
|
|
710
|
-
}
|
|
711
|
-
const result = Number.parseFloat(value);
|
|
712
|
-
return Number.isNaN(result) ? def : result;
|
|
733
|
+
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseFloat(value);
|
|
734
|
+
return Number.isNaN(parsedValue) ? defaultValue !== void 0 ? defaultValue : 0 : parsedValue;
|
|
713
735
|
}
|
|
714
736
|
|
|
715
737
|
// src/number/toInt.ts
|
|
716
738
|
function toInt(value, defaultValue) {
|
|
717
|
-
const
|
|
718
|
-
|
|
719
|
-
return def;
|
|
720
|
-
}
|
|
721
|
-
const result = Number.parseInt(value);
|
|
722
|
-
return Number.isNaN(result) ? def : result;
|
|
739
|
+
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseInt(value);
|
|
740
|
+
return Number.isNaN(parsedValue) ? defaultValue !== void 0 ? defaultValue : 0 : parsedValue;
|
|
723
741
|
}
|
|
724
742
|
|
|
725
743
|
// src/object/assign.ts
|
|
@@ -923,19 +941,22 @@ function omit(obj, keys2) {
|
|
|
923
941
|
}
|
|
924
942
|
|
|
925
943
|
// src/object/pick.ts
|
|
926
|
-
function pick(obj,
|
|
944
|
+
function pick(obj, filter) {
|
|
927
945
|
if (!obj) {
|
|
928
946
|
return {};
|
|
929
947
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
{
|
|
938
|
-
|
|
948
|
+
let keys2 = filter;
|
|
949
|
+
if (isArray(filter)) {
|
|
950
|
+
filter = null;
|
|
951
|
+
} else {
|
|
952
|
+
keys2 = Reflect.ownKeys(obj);
|
|
953
|
+
}
|
|
954
|
+
return keys2.reduce((acc, key) => {
|
|
955
|
+
if (filterKey(obj, key, filter)) {
|
|
956
|
+
acc[key] = obj[key];
|
|
957
|
+
}
|
|
958
|
+
return acc;
|
|
959
|
+
}, {});
|
|
939
960
|
}
|
|
940
961
|
|
|
941
962
|
// src/object/set.ts
|
|
@@ -992,8 +1013,15 @@ function random(min2, max2) {
|
|
|
992
1013
|
}
|
|
993
1014
|
|
|
994
1015
|
// src/random/shuffle.ts
|
|
995
|
-
function shuffle(array) {
|
|
996
|
-
|
|
1016
|
+
function shuffle(array, random2 = random) {
|
|
1017
|
+
const newArray = array.slice();
|
|
1018
|
+
for (let idx = 0, randomIdx, item; idx < array.length; idx++) {
|
|
1019
|
+
randomIdx = random2(0, array.length - 1);
|
|
1020
|
+
item = newArray[randomIdx];
|
|
1021
|
+
newArray[randomIdx] = newArray[idx];
|
|
1022
|
+
newArray[idx] = item;
|
|
1023
|
+
}
|
|
1024
|
+
return newArray;
|
|
997
1025
|
}
|
|
998
1026
|
|
|
999
1027
|
// src/random/uid.ts
|
|
@@ -1316,6 +1344,7 @@ export {
|
|
|
1316
1344
|
filterKey,
|
|
1317
1345
|
first,
|
|
1318
1346
|
flat,
|
|
1347
|
+
flip,
|
|
1319
1348
|
fork,
|
|
1320
1349
|
get,
|
|
1321
1350
|
group,
|
|
@@ -1341,6 +1370,7 @@ export {
|
|
|
1341
1370
|
iterate,
|
|
1342
1371
|
keys,
|
|
1343
1372
|
last,
|
|
1373
|
+
lerp,
|
|
1344
1374
|
list,
|
|
1345
1375
|
listify,
|
|
1346
1376
|
lowerize,
|
|
@@ -1355,6 +1385,7 @@ export {
|
|
|
1355
1385
|
min,
|
|
1356
1386
|
objectify,
|
|
1357
1387
|
omit,
|
|
1388
|
+
once,
|
|
1358
1389
|
parallel,
|
|
1359
1390
|
partial,
|
|
1360
1391
|
partob,
|