es-toolkit 1.15.1 → 1.16.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/CHANGELOG.md +6 -0
- package/dist/_chunk/{initial-CBsbzo.js → initial-y0QrPY.js} +28 -0
- package/dist/_chunk/{isFunction-D0hq6d.js → isFunction-aCEz9d.js} +5 -7
- package/dist/_chunk/{isObjectLike-BeLCsr.js → toMerged-BGwYW5.js} +25 -0
- package/dist/_internal/compareValues.mjs +11 -0
- package/dist/array/index.d.mts +1 -0
- package/dist/array/index.d.ts +1 -0
- package/dist/array/index.js +3 -11
- package/dist/array/index.mjs +1 -0
- package/dist/array/orderBy.mjs +2 -9
- package/dist/array/sortBy.d.mts +35 -0
- package/dist/array/sortBy.d.ts +35 -0
- package/dist/array/sortBy.mjs +19 -0
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/_internal/getSymbols.mjs +1 -2
- package/dist/compat/_internal/toKey.mjs +13 -0
- package/dist/compat/array/find.d.mts +122 -0
- package/dist/compat/array/find.d.ts +122 -0
- package/dist/compat/array/find.mjs +42 -0
- package/dist/compat/array/findIndex.d.mts +62 -0
- package/dist/compat/array/findIndex.d.ts +62 -0
- package/dist/compat/array/findIndex.mjs +26 -0
- package/dist/compat/array/indexOf.d.mts +21 -0
- package/dist/compat/array/indexOf.d.ts +21 -0
- package/dist/compat/array/indexOf.mjs +20 -0
- package/dist/compat/function/ary.d.mts +2 -1
- package/dist/compat/function/ary.d.ts +2 -1
- package/dist/compat/function/bind.d.mts +3 -1
- package/dist/compat/function/bind.d.ts +3 -1
- package/dist/compat/function/rest.d.mts +33 -0
- package/dist/compat/function/rest.d.ts +33 -0
- package/dist/compat/function/rest.mjs +11 -0
- package/dist/compat/index.d.mts +13 -0
- package/dist/compat/index.d.ts +13 -0
- package/dist/compat/index.js +413 -259
- package/dist/compat/index.mjs +13 -0
- package/dist/compat/object/get.mjs +2 -4
- package/dist/compat/object/has.d.mts +32 -0
- package/dist/compat/object/has.d.ts +32 -0
- package/dist/compat/object/has.mjs +34 -0
- package/dist/compat/object/merge.d.mts +1 -0
- package/dist/compat/object/merge.d.ts +1 -0
- package/dist/compat/object/mergeWith.d.mts +6 -0
- package/dist/compat/object/mergeWith.d.ts +6 -0
- package/dist/compat/object/mergeWith.mjs +0 -3
- package/dist/compat/object/set.mjs +1 -5
- package/dist/compat/predicate/isArrayLike.mjs +1 -2
- package/dist/compat/predicate/isString.d.mts +20 -0
- package/dist/compat/predicate/isString.d.ts +20 -0
- package/dist/compat/predicate/isString.mjs +13 -0
- package/dist/compat/predicate/matchesProperty.d.mts +28 -0
- package/dist/compat/predicate/matchesProperty.d.ts +28 -0
- package/dist/compat/predicate/matchesProperty.mjs +22 -0
- package/dist/compat/string/padEnd.d.mts +20 -0
- package/dist/compat/string/padEnd.d.ts +20 -0
- package/dist/compat/string/padEnd.mjs +5 -0
- package/dist/function/ary.d.mts +1 -1
- package/dist/function/ary.d.ts +1 -1
- package/dist/function/index.d.mts +4 -0
- package/dist/function/index.d.ts +4 -0
- package/dist/function/index.js +76 -0
- package/dist/function/index.mjs +4 -0
- package/dist/function/memoize.d.mts +87 -0
- package/dist/function/memoize.d.ts +87 -0
- package/dist/function/memoize.mjs +16 -0
- package/dist/function/partial.d.mts +33 -0
- package/dist/function/partial.d.ts +33 -0
- package/dist/function/partial.mjs +23 -0
- package/dist/function/partialRight.d.mts +33 -0
- package/dist/function/partialRight.d.ts +33 -0
- package/dist/function/partialRight.mjs +25 -0
- package/dist/function/rest.d.mts +33 -0
- package/dist/function/rest.d.ts +33 -0
- package/dist/function/rest.mjs +12 -0
- package/dist/function/unary.d.mts +1 -1
- package/dist/function/unary.d.ts +1 -1
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +21 -14
- package/dist/index.mjs +7 -0
- package/dist/object/index.d.mts +1 -0
- package/dist/object/index.d.ts +1 -0
- package/dist/object/index.js +14 -32
- package/dist/object/index.mjs +1 -0
- package/dist/object/toMerged.d.mts +45 -0
- package/dist/object/toMerged.d.ts +45 -0
- package/dist/object/toMerged.mjs +8 -0
- package/dist/predicate/index.d.mts +1 -0
- package/dist/predicate/index.d.ts +1 -0
- package/dist/predicate/index.js +6 -1
- package/dist/predicate/index.mjs +1 -0
- package/dist/predicate/isEqual.mjs +4 -5
- package/dist/predicate/isString.d.mts +20 -0
- package/dist/predicate/isString.d.ts +20 -0
- package/dist/predicate/isString.mjs +5 -0
- package/dist/string/camelCase.mjs +2 -2
- package/dist/string/index.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a memoized version of the provided function. The memoized function caches
|
|
3
|
+
* results based on the argument it receives, so if the same argument is passed again,
|
|
4
|
+
* it returns the cached result instead of recomputing it.
|
|
5
|
+
*
|
|
6
|
+
* This function works with functions that take zero or just one argument. If your function
|
|
7
|
+
* originally takes multiple arguments, you should refactor it to take a single object or array
|
|
8
|
+
* that combines those arguments.
|
|
9
|
+
*
|
|
10
|
+
* If the argument is not primitive (e.g., arrays or objects), provide a
|
|
11
|
+
* `getCacheKey` function to generate a unique cache key for proper caching.
|
|
12
|
+
*
|
|
13
|
+
* @param {F} fn - The function to be memoized. It should accept a single argument and return a value.
|
|
14
|
+
* @param {MemoizeOptions<Parameters<F>[0], ReturnType<F>>} [options={}] - Optional configuration for the memoization.
|
|
15
|
+
* @param {MemoizeCache<any, V>} [options.cache] - The cache object used to store results. Defaults to a new `Map`.
|
|
16
|
+
* @param {(args: A) => unknown} [options.getCacheKey] - An optional function to generate a unique cache key for each argument.
|
|
17
|
+
*
|
|
18
|
+
* @returns {F & { cache: MemoizeCache<any, ReturnType<F>> }} - The memoized function with an additional `cache` property that exposes the internal cache.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Example using the default cache
|
|
22
|
+
* const add = (x: number) => x + 10;
|
|
23
|
+
* const memoizedAdd = memoize(add);
|
|
24
|
+
*
|
|
25
|
+
* console.log(memoizedAdd(5)); // 15
|
|
26
|
+
* console.log(memoizedAdd(5)); // 15 (cached result)
|
|
27
|
+
* console.log(memoizedAdd.cache.size); // 1
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Example using a custom resolver
|
|
31
|
+
* const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
|
|
32
|
+
* const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
|
|
33
|
+
* console.log(memoizedSum([1, 2])); // 3
|
|
34
|
+
* console.log(memoizedSum([1, 2])); // 3 (cached result)
|
|
35
|
+
* console.log(memoizedSum.cache.size); // 1
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Example using a custom cache implementation
|
|
39
|
+
* class CustomCache<K, T> implements MemoizeCache<K, T> {
|
|
40
|
+
* private cache = new Map<K, T>();
|
|
41
|
+
*
|
|
42
|
+
* set(key: K, value: T): void {
|
|
43
|
+
* this.cache.set(key, value);
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* get(key: K): T | undefined {
|
|
47
|
+
* return this.cache.get(key);
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* has(key: K): boolean {
|
|
51
|
+
* return this.cache.has(key);
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* delete(key: K): boolean {
|
|
55
|
+
* return this.cache.delete(key);
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* clear(): void {
|
|
59
|
+
* this.cache.clear();
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* get size(): number {
|
|
63
|
+
* return this.cache.size;
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* const customCache = new CustomCache<string, number>();
|
|
67
|
+
* const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
|
|
68
|
+
* console.log(memoizedSumWithCustomCache([1, 2])); // 3
|
|
69
|
+
* console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
|
|
70
|
+
* console.log(memoizedAddWithCustomCache.cache.size); // 1
|
|
71
|
+
*/
|
|
72
|
+
declare function memoize<F extends (...args: any) => any>(fn: F, options?: {
|
|
73
|
+
cache?: MemoizeCache<any, ReturnType<F>>;
|
|
74
|
+
getCacheKey?: (args: Parameters<F>[0]) => unknown;
|
|
75
|
+
}): F & {
|
|
76
|
+
cache: MemoizeCache<any, ReturnType<F>>;
|
|
77
|
+
};
|
|
78
|
+
interface MemoizeCache<K, V> {
|
|
79
|
+
set(key: K, value: V): void;
|
|
80
|
+
get(key: K): V | undefined;
|
|
81
|
+
has(key: K): boolean;
|
|
82
|
+
delete(key: K): boolean | void;
|
|
83
|
+
clear(): void;
|
|
84
|
+
size: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { type MemoizeCache, memoize };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a memoized version of the provided function. The memoized function caches
|
|
3
|
+
* results based on the argument it receives, so if the same argument is passed again,
|
|
4
|
+
* it returns the cached result instead of recomputing it.
|
|
5
|
+
*
|
|
6
|
+
* This function works with functions that take zero or just one argument. If your function
|
|
7
|
+
* originally takes multiple arguments, you should refactor it to take a single object or array
|
|
8
|
+
* that combines those arguments.
|
|
9
|
+
*
|
|
10
|
+
* If the argument is not primitive (e.g., arrays or objects), provide a
|
|
11
|
+
* `getCacheKey` function to generate a unique cache key for proper caching.
|
|
12
|
+
*
|
|
13
|
+
* @param {F} fn - The function to be memoized. It should accept a single argument and return a value.
|
|
14
|
+
* @param {MemoizeOptions<Parameters<F>[0], ReturnType<F>>} [options={}] - Optional configuration for the memoization.
|
|
15
|
+
* @param {MemoizeCache<any, V>} [options.cache] - The cache object used to store results. Defaults to a new `Map`.
|
|
16
|
+
* @param {(args: A) => unknown} [options.getCacheKey] - An optional function to generate a unique cache key for each argument.
|
|
17
|
+
*
|
|
18
|
+
* @returns {F & { cache: MemoizeCache<any, ReturnType<F>> }} - The memoized function with an additional `cache` property that exposes the internal cache.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Example using the default cache
|
|
22
|
+
* const add = (x: number) => x + 10;
|
|
23
|
+
* const memoizedAdd = memoize(add);
|
|
24
|
+
*
|
|
25
|
+
* console.log(memoizedAdd(5)); // 15
|
|
26
|
+
* console.log(memoizedAdd(5)); // 15 (cached result)
|
|
27
|
+
* console.log(memoizedAdd.cache.size); // 1
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Example using a custom resolver
|
|
31
|
+
* const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
|
|
32
|
+
* const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
|
|
33
|
+
* console.log(memoizedSum([1, 2])); // 3
|
|
34
|
+
* console.log(memoizedSum([1, 2])); // 3 (cached result)
|
|
35
|
+
* console.log(memoizedSum.cache.size); // 1
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Example using a custom cache implementation
|
|
39
|
+
* class CustomCache<K, T> implements MemoizeCache<K, T> {
|
|
40
|
+
* private cache = new Map<K, T>();
|
|
41
|
+
*
|
|
42
|
+
* set(key: K, value: T): void {
|
|
43
|
+
* this.cache.set(key, value);
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* get(key: K): T | undefined {
|
|
47
|
+
* return this.cache.get(key);
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* has(key: K): boolean {
|
|
51
|
+
* return this.cache.has(key);
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* delete(key: K): boolean {
|
|
55
|
+
* return this.cache.delete(key);
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* clear(): void {
|
|
59
|
+
* this.cache.clear();
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* get size(): number {
|
|
63
|
+
* return this.cache.size;
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* const customCache = new CustomCache<string, number>();
|
|
67
|
+
* const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
|
|
68
|
+
* console.log(memoizedSumWithCustomCache([1, 2])); // 3
|
|
69
|
+
* console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
|
|
70
|
+
* console.log(memoizedAddWithCustomCache.cache.size); // 1
|
|
71
|
+
*/
|
|
72
|
+
declare function memoize<F extends (...args: any) => any>(fn: F, options?: {
|
|
73
|
+
cache?: MemoizeCache<any, ReturnType<F>>;
|
|
74
|
+
getCacheKey?: (args: Parameters<F>[0]) => unknown;
|
|
75
|
+
}): F & {
|
|
76
|
+
cache: MemoizeCache<any, ReturnType<F>>;
|
|
77
|
+
};
|
|
78
|
+
interface MemoizeCache<K, V> {
|
|
79
|
+
set(key: K, value: V): void;
|
|
80
|
+
get(key: K): V | undefined;
|
|
81
|
+
has(key: K): boolean;
|
|
82
|
+
delete(key: K): boolean | void;
|
|
83
|
+
clear(): void;
|
|
84
|
+
size: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { type MemoizeCache, memoize };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function memoize(fn, options = {}) {
|
|
2
|
+
const { cache = new Map(), getCacheKey } = options;
|
|
3
|
+
const memoizedFn = function (arg) {
|
|
4
|
+
const key = getCacheKey ? getCacheKey(arg) : arg;
|
|
5
|
+
if (cache.has(key)) {
|
|
6
|
+
return cache.get(key);
|
|
7
|
+
}
|
|
8
|
+
const result = fn.call(this, arg);
|
|
9
|
+
cache.set(key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
memoizedFn.cache = cache;
|
|
13
|
+
return memoizedFn;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { memoize };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
|
|
3
|
+
*
|
|
4
|
+
* The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
5
|
+
*
|
|
6
|
+
* Note: This method doesn't set the `length` property of partially applied functions.
|
|
7
|
+
*
|
|
8
|
+
* @template F The type of the function to partially apply.
|
|
9
|
+
* @param {F} func The function to partially apply arguments to.
|
|
10
|
+
* @param {any[]} partialArgs The arguments to be partially applied.
|
|
11
|
+
* @returns {F} Returns the new partially applied function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* function greet(greeting, name) {
|
|
15
|
+
* return greeting + ' ' + name;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const sayHelloTo = partial(greet, 'hello');
|
|
19
|
+
* sayHelloTo('fred');
|
|
20
|
+
* // => 'hello fred'
|
|
21
|
+
*
|
|
22
|
+
* // Partially applied with placeholders.
|
|
23
|
+
* const greetFred = partial(greet, partial.placeholder, 'fred');
|
|
24
|
+
* greetFred('hi');
|
|
25
|
+
* // => 'hi fred'
|
|
26
|
+
*/
|
|
27
|
+
declare function partial<F extends (...args: any[]) => any>(func: F, ...partialArgs: any[]): F;
|
|
28
|
+
declare namespace partial {
|
|
29
|
+
var placeholder: typeof partialPlaceholder;
|
|
30
|
+
}
|
|
31
|
+
declare const partialPlaceholder: unique symbol;
|
|
32
|
+
|
|
33
|
+
export { partial };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
|
|
3
|
+
*
|
|
4
|
+
* The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
5
|
+
*
|
|
6
|
+
* Note: This method doesn't set the `length` property of partially applied functions.
|
|
7
|
+
*
|
|
8
|
+
* @template F The type of the function to partially apply.
|
|
9
|
+
* @param {F} func The function to partially apply arguments to.
|
|
10
|
+
* @param {any[]} partialArgs The arguments to be partially applied.
|
|
11
|
+
* @returns {F} Returns the new partially applied function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* function greet(greeting, name) {
|
|
15
|
+
* return greeting + ' ' + name;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const sayHelloTo = partial(greet, 'hello');
|
|
19
|
+
* sayHelloTo('fred');
|
|
20
|
+
* // => 'hello fred'
|
|
21
|
+
*
|
|
22
|
+
* // Partially applied with placeholders.
|
|
23
|
+
* const greetFred = partial(greet, partial.placeholder, 'fred');
|
|
24
|
+
* greetFred('hi');
|
|
25
|
+
* // => 'hi fred'
|
|
26
|
+
*/
|
|
27
|
+
declare function partial<F extends (...args: any[]) => any>(func: F, ...partialArgs: any[]): F;
|
|
28
|
+
declare namespace partial {
|
|
29
|
+
var placeholder: typeof partialPlaceholder;
|
|
30
|
+
}
|
|
31
|
+
declare const partialPlaceholder: unique symbol;
|
|
32
|
+
|
|
33
|
+
export { partial };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function partial(func, ...partialArgs) {
|
|
2
|
+
return function (...providedArgs) {
|
|
3
|
+
const args = [];
|
|
4
|
+
let startIndex = 0;
|
|
5
|
+
for (let i = 0; i < partialArgs.length; i++) {
|
|
6
|
+
const arg = partialArgs[i];
|
|
7
|
+
if (arg === partial.placeholder) {
|
|
8
|
+
args.push(providedArgs[startIndex++]);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
args.push(arg);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
for (let i = startIndex; i < providedArgs.length; i++) {
|
|
15
|
+
args.push(providedArgs[i]);
|
|
16
|
+
}
|
|
17
|
+
return func.apply(this, args);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const partialPlaceholder = Symbol('partial.placeholder');
|
|
21
|
+
partial.placeholder = partialPlaceholder;
|
|
22
|
+
|
|
23
|
+
export { partial };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This method is like `partial` except that partially applied arguments are appended to the arguments it receives.
|
|
3
|
+
*
|
|
4
|
+
* The partialRight.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
5
|
+
*
|
|
6
|
+
* Note: This method doesn't set the `length` property of partially applied functions.
|
|
7
|
+
*
|
|
8
|
+
* @template F The type of the function to partially apply.
|
|
9
|
+
* @param {F} func The function to partially apply arguments to.
|
|
10
|
+
* @param {any[]} partialArgs The arguments to be partially applied.
|
|
11
|
+
* @returns {F} Returns the new partially applied function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* function greet(greeting, name) {
|
|
15
|
+
* return greeting + ' ' + name;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const greetFred = partialRight(greet, 'fred');
|
|
19
|
+
* greetFred('hi');
|
|
20
|
+
* // => 'hi fred'
|
|
21
|
+
*
|
|
22
|
+
* // Partially applied with placeholders.
|
|
23
|
+
* const sayHelloTo = partialRight(greet, 'hello', partialRight.placeholder);
|
|
24
|
+
* sayHelloTo('fred');
|
|
25
|
+
* // => 'hello fred'
|
|
26
|
+
*/
|
|
27
|
+
declare function partialRight<F extends (...args: any[]) => any>(func: F, ...partialArgs: any[]): F;
|
|
28
|
+
declare namespace partialRight {
|
|
29
|
+
var placeholder: typeof partialRightPlaceholder;
|
|
30
|
+
}
|
|
31
|
+
declare const partialRightPlaceholder: unique symbol;
|
|
32
|
+
|
|
33
|
+
export { partialRight };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This method is like `partial` except that partially applied arguments are appended to the arguments it receives.
|
|
3
|
+
*
|
|
4
|
+
* The partialRight.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
5
|
+
*
|
|
6
|
+
* Note: This method doesn't set the `length` property of partially applied functions.
|
|
7
|
+
*
|
|
8
|
+
* @template F The type of the function to partially apply.
|
|
9
|
+
* @param {F} func The function to partially apply arguments to.
|
|
10
|
+
* @param {any[]} partialArgs The arguments to be partially applied.
|
|
11
|
+
* @returns {F} Returns the new partially applied function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* function greet(greeting, name) {
|
|
15
|
+
* return greeting + ' ' + name;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const greetFred = partialRight(greet, 'fred');
|
|
19
|
+
* greetFred('hi');
|
|
20
|
+
* // => 'hi fred'
|
|
21
|
+
*
|
|
22
|
+
* // Partially applied with placeholders.
|
|
23
|
+
* const sayHelloTo = partialRight(greet, 'hello', partialRight.placeholder);
|
|
24
|
+
* sayHelloTo('fred');
|
|
25
|
+
* // => 'hello fred'
|
|
26
|
+
*/
|
|
27
|
+
declare function partialRight<F extends (...args: any[]) => any>(func: F, ...partialArgs: any[]): F;
|
|
28
|
+
declare namespace partialRight {
|
|
29
|
+
var placeholder: typeof partialRightPlaceholder;
|
|
30
|
+
}
|
|
31
|
+
declare const partialRightPlaceholder: unique symbol;
|
|
32
|
+
|
|
33
|
+
export { partialRight };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function partialRight(func, ...partialArgs) {
|
|
2
|
+
return function (...providedArgs) {
|
|
3
|
+
const placeholderLength = partialArgs.filter(arg => arg === partialRightPlaceholder).length;
|
|
4
|
+
const rangeLength = Math.max(providedArgs.length - placeholderLength, 0);
|
|
5
|
+
const args = [];
|
|
6
|
+
let providedIndex = 0;
|
|
7
|
+
for (let i = 0; i < rangeLength; i++) {
|
|
8
|
+
args.push(providedArgs[providedIndex++]);
|
|
9
|
+
}
|
|
10
|
+
for (let i = 0; i < partialArgs.length; i++) {
|
|
11
|
+
const arg = partialArgs[i];
|
|
12
|
+
if (arg === partialRight.placeholder) {
|
|
13
|
+
args.push(providedArgs[providedIndex++]);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
args.push(arg);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return func.apply(this, args);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const partialRightPlaceholder = Symbol('partialRight.placeholder');
|
|
23
|
+
partialRight.placeholder = partialRightPlaceholder;
|
|
24
|
+
|
|
25
|
+
export { partialRight };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that transforms the arguments of the provided function `func`.
|
|
3
|
+
* The transformed arguments are passed to `func` such that the arguments starting from a specified index
|
|
4
|
+
* are grouped into an array, while the previous arguments are passed as individual elements.
|
|
5
|
+
*
|
|
6
|
+
* @template F - The type of the function being transformed.
|
|
7
|
+
* @param {F} func - The function whose arguments are to be transformed.
|
|
8
|
+
* @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
|
|
9
|
+
* Defaults to `func.length - 1`, grouping all arguments after the last parameter.
|
|
10
|
+
* @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
|
|
11
|
+
*
|
|
12
|
+
* The transformed arguments are:
|
|
13
|
+
* - The first `start` arguments as individual elements.
|
|
14
|
+
* - The remaining arguments from index `start` onward grouped into an array.
|
|
15
|
+
* @example
|
|
16
|
+
* function fn(a, b, c) {
|
|
17
|
+
* return [a, b, c];
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Using default start index (func.length - 1, which is 2 in this case)
|
|
21
|
+
* const transformedFn = rest(fn);
|
|
22
|
+
* console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
|
|
23
|
+
*
|
|
24
|
+
* // Using start index 1
|
|
25
|
+
* const transformedFnWithStart = rest(fn, 1);
|
|
26
|
+
* console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
|
|
27
|
+
*
|
|
28
|
+
* // With fewer arguments than the start index
|
|
29
|
+
* console.log(transformedFn(1)); // [1, undefined, []]
|
|
30
|
+
*/
|
|
31
|
+
declare function rest<F extends (...args: any[]) => any>(func: F, startIndex?: number): (...args: any[]) => ReturnType<F>;
|
|
32
|
+
|
|
33
|
+
export { rest };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that transforms the arguments of the provided function `func`.
|
|
3
|
+
* The transformed arguments are passed to `func` such that the arguments starting from a specified index
|
|
4
|
+
* are grouped into an array, while the previous arguments are passed as individual elements.
|
|
5
|
+
*
|
|
6
|
+
* @template F - The type of the function being transformed.
|
|
7
|
+
* @param {F} func - The function whose arguments are to be transformed.
|
|
8
|
+
* @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
|
|
9
|
+
* Defaults to `func.length - 1`, grouping all arguments after the last parameter.
|
|
10
|
+
* @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
|
|
11
|
+
*
|
|
12
|
+
* The transformed arguments are:
|
|
13
|
+
* - The first `start` arguments as individual elements.
|
|
14
|
+
* - The remaining arguments from index `start` onward grouped into an array.
|
|
15
|
+
* @example
|
|
16
|
+
* function fn(a, b, c) {
|
|
17
|
+
* return [a, b, c];
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Using default start index (func.length - 1, which is 2 in this case)
|
|
21
|
+
* const transformedFn = rest(fn);
|
|
22
|
+
* console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
|
|
23
|
+
*
|
|
24
|
+
* // Using start index 1
|
|
25
|
+
* const transformedFnWithStart = rest(fn, 1);
|
|
26
|
+
* console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
|
|
27
|
+
*
|
|
28
|
+
* // With fewer arguments than the start index
|
|
29
|
+
* console.log(transformedFn(1)); // [1, undefined, []]
|
|
30
|
+
*/
|
|
31
|
+
declare function rest<F extends (...args: any[]) => any>(func: F, startIndex?: number): (...args: any[]) => ReturnType<F>;
|
|
32
|
+
|
|
33
|
+
export { rest };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function rest(func, startIndex = func.length - 1) {
|
|
2
|
+
return function (...args) {
|
|
3
|
+
const rest = args.slice(startIndex);
|
|
4
|
+
const params = args.slice(0, startIndex);
|
|
5
|
+
while (params.length < startIndex) {
|
|
6
|
+
params.push(undefined);
|
|
7
|
+
}
|
|
8
|
+
return func.apply(this, [...params, rest]);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { rest };
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* @param {F} func - The function to cap arguments for.
|
|
6
6
|
* @returns {(...args: any[]) => ReturnType<F>} Returns the new capped function.
|
|
7
7
|
*/
|
|
8
|
-
declare function unary<F extends (...args: any[]) => any>(func: F): (
|
|
8
|
+
declare function unary<F extends (...args: any[]) => any>(func: F): (...args: any[]) => ReturnType<F>;
|
|
9
9
|
|
|
10
10
|
export { unary };
|
package/dist/function/unary.d.ts
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* @param {F} func - The function to cap arguments for.
|
|
6
6
|
* @returns {(...args: any[]) => ReturnType<F>} Returns the new capped function.
|
|
7
7
|
*/
|
|
8
|
-
declare function unary<F extends (...args: any[]) => any>(func: F): (
|
|
8
|
+
declare function unary<F extends (...args: any[]) => any>(func: F): (...args: any[]) => ReturnType<F>;
|
|
9
9
|
|
|
10
10
|
export { unary };
|
package/dist/index.d.mts
CHANGED
|
@@ -22,6 +22,7 @@ export { maxBy } from './array/maxBy.mjs';
|
|
|
22
22
|
export { minBy } from './array/minBy.mjs';
|
|
23
23
|
export { orderBy } from './array/orderBy.mjs';
|
|
24
24
|
export { partition } from './array/partition.mjs';
|
|
25
|
+
export { sortBy } from './array/sortBy.mjs';
|
|
25
26
|
export { sample } from './array/sample.mjs';
|
|
26
27
|
export { sampleSize } from './array/sampleSize.mjs';
|
|
27
28
|
export { shuffle } from './array/shuffle.mjs';
|
|
@@ -59,8 +60,12 @@ export { noop } from './function/noop.mjs';
|
|
|
59
60
|
export { once } from './function/once.mjs';
|
|
60
61
|
export { throttle } from './function/throttle.mjs';
|
|
61
62
|
export { negate } from './function/negate.mjs';
|
|
63
|
+
export { MemoizeCache, memoize } from './function/memoize.mjs';
|
|
62
64
|
export { ary } from './function/ary.mjs';
|
|
63
65
|
export { unary } from './function/unary.mjs';
|
|
66
|
+
export { partial } from './function/partial.mjs';
|
|
67
|
+
export { partialRight } from './function/partialRight.mjs';
|
|
68
|
+
export { rest } from './function/rest.mjs';
|
|
64
69
|
export { clamp } from './math/clamp.mjs';
|
|
65
70
|
export { inRange } from './math/inRange.mjs';
|
|
66
71
|
export { mean } from './math/mean.mjs';
|
|
@@ -82,6 +87,7 @@ export { mapKeys } from './object/mapKeys.mjs';
|
|
|
82
87
|
export { mapValues } from './object/mapValues.mjs';
|
|
83
88
|
export { cloneDeep } from './object/cloneDeep.mjs';
|
|
84
89
|
export { merge } from './object/merge.mjs';
|
|
90
|
+
export { toMerged } from './object/toMerged.mjs';
|
|
85
91
|
export { mergeWith } from './object/mergeWith.mjs';
|
|
86
92
|
export { isEqual } from './predicate/isEqual.mjs';
|
|
87
93
|
export { isNil } from './predicate/isNil.mjs';
|
|
@@ -95,6 +101,7 @@ export { isTypedArray } from './predicate/isTypedArray.mjs';
|
|
|
95
101
|
export { isPrimitive } from './predicate/isPrimitive.mjs';
|
|
96
102
|
export { isBoolean } from './predicate/isBoolean.mjs';
|
|
97
103
|
export { isSymbol } from './predicate/isSymbol.mjs';
|
|
104
|
+
export { isString } from './predicate/isString.mjs';
|
|
98
105
|
export { delay } from './promise/delay.mjs';
|
|
99
106
|
export { withTimeout } from './promise/withTimeout.mjs';
|
|
100
107
|
export { camelCase } from './string/camelCase.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export { maxBy } from './array/maxBy.js';
|
|
|
22
22
|
export { minBy } from './array/minBy.js';
|
|
23
23
|
export { orderBy } from './array/orderBy.js';
|
|
24
24
|
export { partition } from './array/partition.js';
|
|
25
|
+
export { sortBy } from './array/sortBy.js';
|
|
25
26
|
export { sample } from './array/sample.js';
|
|
26
27
|
export { sampleSize } from './array/sampleSize.js';
|
|
27
28
|
export { shuffle } from './array/shuffle.js';
|
|
@@ -59,8 +60,12 @@ export { noop } from './function/noop.js';
|
|
|
59
60
|
export { once } from './function/once.js';
|
|
60
61
|
export { throttle } from './function/throttle.js';
|
|
61
62
|
export { negate } from './function/negate.js';
|
|
63
|
+
export { MemoizeCache, memoize } from './function/memoize.js';
|
|
62
64
|
export { ary } from './function/ary.js';
|
|
63
65
|
export { unary } from './function/unary.js';
|
|
66
|
+
export { partial } from './function/partial.js';
|
|
67
|
+
export { partialRight } from './function/partialRight.js';
|
|
68
|
+
export { rest } from './function/rest.js';
|
|
64
69
|
export { clamp } from './math/clamp.js';
|
|
65
70
|
export { inRange } from './math/inRange.js';
|
|
66
71
|
export { mean } from './math/mean.js';
|
|
@@ -82,6 +87,7 @@ export { mapKeys } from './object/mapKeys.js';
|
|
|
82
87
|
export { mapValues } from './object/mapValues.js';
|
|
83
88
|
export { cloneDeep } from './object/cloneDeep.js';
|
|
84
89
|
export { merge } from './object/merge.js';
|
|
90
|
+
export { toMerged } from './object/toMerged.js';
|
|
85
91
|
export { mergeWith } from './object/mergeWith.js';
|
|
86
92
|
export { isEqual } from './predicate/isEqual.js';
|
|
87
93
|
export { isNil } from './predicate/isNil.js';
|
|
@@ -95,6 +101,7 @@ export { isTypedArray } from './predicate/isTypedArray.js';
|
|
|
95
101
|
export { isPrimitive } from './predicate/isPrimitive.js';
|
|
96
102
|
export { isBoolean } from './predicate/isBoolean.js';
|
|
97
103
|
export { isSymbol } from './predicate/isSymbol.js';
|
|
104
|
+
export { isString } from './predicate/isString.js';
|
|
98
105
|
export { delay } from './promise/delay.js';
|
|
99
106
|
export { withTimeout } from './promise/withTimeout.js';
|
|
100
107
|
export { camelCase } from './string/camelCase.js';
|
package/dist/index.js
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const initial = require('./_chunk/initial-
|
|
5
|
+
const initial = require('./_chunk/initial-y0QrPY.js');
|
|
6
6
|
const array_index = require('./array/index.js');
|
|
7
7
|
const promise_index = require('./_chunk/index-CwRt_M.js');
|
|
8
8
|
const function_index = require('./function/index.js');
|
|
9
9
|
const math_index = require('./math/index.js');
|
|
10
10
|
const randomInt = require('./_chunk/randomInt-CF7bZK.js');
|
|
11
|
-
const
|
|
11
|
+
const toMerged = require('./_chunk/toMerged-BGwYW5.js');
|
|
12
12
|
const object_index = require('./object/index.js');
|
|
13
|
-
const isFunction = require('./_chunk/isFunction-
|
|
13
|
+
const isFunction = require('./_chunk/isFunction-aCEz9d.js');
|
|
14
14
|
const isTypedArray = require('./_chunk/isTypedArray-BBEkFl.js');
|
|
15
15
|
const predicate_index = require('./predicate/index.js');
|
|
16
16
|
const string_index = require('./string/index.js');
|
|
@@ -46,6 +46,7 @@ exports.partition = initial.partition;
|
|
|
46
46
|
exports.sample = initial.sample;
|
|
47
47
|
exports.sampleSize = initial.sampleSize;
|
|
48
48
|
exports.shuffle = initial.shuffle;
|
|
49
|
+
exports.sortBy = initial.sortBy;
|
|
49
50
|
exports.tail = initial.tail;
|
|
50
51
|
exports.take = initial.take;
|
|
51
52
|
exports.takeRight = initial.takeRight;
|
|
@@ -77,9 +78,13 @@ exports.after = function_index.after;
|
|
|
77
78
|
exports.ary = function_index.ary;
|
|
78
79
|
exports.before = function_index.before;
|
|
79
80
|
exports.debounce = function_index.debounce;
|
|
81
|
+
exports.memoize = function_index.memoize;
|
|
80
82
|
exports.negate = function_index.negate;
|
|
81
83
|
exports.noop = function_index.noop;
|
|
82
84
|
exports.once = function_index.once;
|
|
85
|
+
exports.partial = function_index.partial;
|
|
86
|
+
exports.partialRight = function_index.partialRight;
|
|
87
|
+
exports.rest = function_index.rest;
|
|
83
88
|
exports.throttle = function_index.throttle;
|
|
84
89
|
exports.unary = function_index.unary;
|
|
85
90
|
exports.clamp = math_index.clamp;
|
|
@@ -92,17 +97,18 @@ exports.sum = math_index.sum;
|
|
|
92
97
|
exports.sumBy = math_index.sumBy;
|
|
93
98
|
exports.random = randomInt.random;
|
|
94
99
|
exports.randomInt = randomInt.randomInt;
|
|
95
|
-
exports.clone =
|
|
96
|
-
exports.cloneDeep =
|
|
97
|
-
exports.flattenObject =
|
|
98
|
-
exports.invert =
|
|
99
|
-
exports.mapKeys =
|
|
100
|
-
exports.mapValues =
|
|
101
|
-
exports.
|
|
102
|
-
exports.
|
|
103
|
-
exports.
|
|
104
|
-
exports.
|
|
105
|
-
exports.
|
|
100
|
+
exports.clone = toMerged.clone;
|
|
101
|
+
exports.cloneDeep = toMerged.cloneDeep;
|
|
102
|
+
exports.flattenObject = toMerged.flattenObject;
|
|
103
|
+
exports.invert = toMerged.invert;
|
|
104
|
+
exports.mapKeys = toMerged.mapKeys;
|
|
105
|
+
exports.mapValues = toMerged.mapValues;
|
|
106
|
+
exports.merge = toMerged.merge;
|
|
107
|
+
exports.omit = toMerged.omit;
|
|
108
|
+
exports.omitBy = toMerged.omitBy;
|
|
109
|
+
exports.pick = toMerged.pick;
|
|
110
|
+
exports.pickBy = toMerged.pickBy;
|
|
111
|
+
exports.toMerged = toMerged.toMerged;
|
|
106
112
|
exports.mergeWith = object_index.mergeWith;
|
|
107
113
|
exports.isEqual = isFunction.isEqual;
|
|
108
114
|
exports.isFunction = isFunction.isFunction;
|
|
@@ -115,6 +121,7 @@ exports.isPlainObject = isTypedArray.isPlainObject;
|
|
|
115
121
|
exports.isPrimitive = isTypedArray.isPrimitive;
|
|
116
122
|
exports.isTypedArray = isTypedArray.isTypedArray;
|
|
117
123
|
exports.isBoolean = predicate_index.isBoolean;
|
|
124
|
+
exports.isString = predicate_index.isString;
|
|
118
125
|
exports.isSymbol = predicate_index.isSymbol;
|
|
119
126
|
exports.camelCase = string_index.camelCase;
|
|
120
127
|
exports.capitalize = string_index.capitalize;
|