@budsbox/lib-es 2.2.0 → 2.3.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/dist/function.d.ts +44 -0
- package/dist/function.js +22 -0
- package/package.json +25 -5
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { UnknownArray } from 'type-fest';
|
|
2
|
+
import type { AnyFunction } from '@budsbox/lib-types';
|
|
3
|
+
/**
|
|
4
|
+
* A function that does nothing.
|
|
5
|
+
*/
|
|
6
|
+
export declare const noop: () => void;
|
|
7
|
+
/**
|
|
8
|
+
* A function that returns its argument.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The value to return.
|
|
11
|
+
* @returns The input value.
|
|
12
|
+
*/
|
|
13
|
+
export declare const pass: <T>(value: T) => T;
|
|
14
|
+
type CacheableFn<TFn extends AnyFunction> = Parameters<TFn> extends [unknown, ...UnknownArray] ? TFn : never;
|
|
15
|
+
type DefaultCacheKey<TFn extends AnyFunction> = Parameters<TFn> extends [infer TArg0, ...UnknownArray] ? TArg0 : never;
|
|
16
|
+
type CachedFn<TFn extends AnyFunction, TKey = DefaultCacheKey<TFn>> = TFn extends (...args: infer TArgs) => infer TReturn ? (cacheMap: Map<TKey, TReturn>, ...args: TArgs) => TReturn : never;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a cached version of a given function.
|
|
19
|
+
* The returned function stores the results of previous calls in a provided cache map
|
|
20
|
+
* and returns the cached result when called with the same arguments,
|
|
21
|
+
* reducing redundant calculations or operations.
|
|
22
|
+
*
|
|
23
|
+
* @remarks A provided function has to accept at least one argument, or it fails typechecking.
|
|
24
|
+
* @param fn - The function to be cached. It must conform to the `CacheableFn` type.
|
|
25
|
+
* @returns A new function that accepts the cache map as the first argument and the original function's arguments as the rest.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createCachedFn<TFn extends AnyFunction>(fn: CacheableFn<TFn>): CachedFn<TFn>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a cached version of a given function.
|
|
30
|
+
* The returned function stores the results of previous calls in a provided cache map
|
|
31
|
+
* and returns the cached result when called with the same arguments,
|
|
32
|
+
* reducing redundant calculations or operations. The caching behavior is
|
|
33
|
+
* determined by a key generation function (`keyFn`) which computes a unique
|
|
34
|
+
* key for each set of arguments. If the same key is generated for later
|
|
35
|
+
* calls, the cached result is returned instead of invoking the original function.
|
|
36
|
+
*
|
|
37
|
+
* @remarks A provided function has to accept at least one argument, or it fails typechecking.
|
|
38
|
+
* @param fn - The function to be cached. It must conform to the `CacheableFn` type.
|
|
39
|
+
* @param keyFn - A function that generates a unique key based on the arguments passed to `fn`.
|
|
40
|
+
* @returns A new function that accepts the cache map as the first argument and the original function's arguments as the rest.
|
|
41
|
+
*/
|
|
42
|
+
export declare function createCachedFn<TFn extends AnyFunction, TKey>(fn: CacheableFn<TFn>, keyFn: (...args: Parameters<TFn>) => TKey): CachedFn<TFn, TKey>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=function.d.ts.map
|
package/dist/function.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that does nothing.
|
|
3
|
+
*/
|
|
4
|
+
export const noop = () => { };
|
|
5
|
+
/**
|
|
6
|
+
* A function that returns its argument.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to return.
|
|
9
|
+
* @returns The input value.
|
|
10
|
+
*/
|
|
11
|
+
export const pass = (value) => value;
|
|
12
|
+
export function createCachedFn(fn, keyFn = pass) {
|
|
13
|
+
return (map, ...args) => {
|
|
14
|
+
const key = keyFn(...args);
|
|
15
|
+
if (map.has(key))
|
|
16
|
+
return map.get(key);
|
|
17
|
+
const result = fn(...args);
|
|
18
|
+
map.set(key, result);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=function.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@budsbox/lib-es",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"homepage": "https://gitlab.com/budsbox/fe/seed",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://gitlab.com/budsbox/fe/seed/-/issues"
|
|
@@ -60,6 +60,16 @@
|
|
|
60
60
|
"default": "./dist/logical.js",
|
|
61
61
|
"types": "./dist/logical.d.ts"
|
|
62
62
|
}
|
|
63
|
+
},
|
|
64
|
+
"#function": {
|
|
65
|
+
"import": {
|
|
66
|
+
"default": "./dist/function.js",
|
|
67
|
+
"types": "./dist/function.d.ts"
|
|
68
|
+
},
|
|
69
|
+
"require": {
|
|
70
|
+
"default": "./dist/function.js",
|
|
71
|
+
"types": "./dist/function.d.ts"
|
|
72
|
+
}
|
|
63
73
|
}
|
|
64
74
|
},
|
|
65
75
|
"exports": {
|
|
@@ -113,6 +123,16 @@
|
|
|
113
123
|
"types": "./dist/logical.d.ts"
|
|
114
124
|
}
|
|
115
125
|
},
|
|
126
|
+
"./function": {
|
|
127
|
+
"import": {
|
|
128
|
+
"default": "./dist/function.js",
|
|
129
|
+
"types": "./dist/function.d.ts"
|
|
130
|
+
},
|
|
131
|
+
"require": {
|
|
132
|
+
"default": "./dist/function.js",
|
|
133
|
+
"types": "./dist/function.d.ts"
|
|
134
|
+
}
|
|
135
|
+
},
|
|
116
136
|
"./package.json": "./package.json"
|
|
117
137
|
},
|
|
118
138
|
"files": [
|
|
@@ -124,16 +144,16 @@
|
|
|
124
144
|
"prepack": "yarn p:ts:prepack"
|
|
125
145
|
},
|
|
126
146
|
"dependencies": {
|
|
127
|
-
"@budsbox/lib-types": "^1.
|
|
147
|
+
"@budsbox/lib-types": "^1.2.0",
|
|
128
148
|
"@types/node": "^22.15.2",
|
|
129
149
|
"tslib": "^2.8.1",
|
|
130
150
|
"type-fest": "^4.32.0"
|
|
131
151
|
},
|
|
132
152
|
"devDependencies": {
|
|
133
153
|
"@budsbox/eslint": "^1.2.0",
|
|
134
|
-
"@budsbox/eslint_presets-lib": "^1.0.
|
|
135
|
-
"@budsbox/eslint_presets-tools": "^1.0.
|
|
136
|
-
"@budsbox/tsconfigs": "^4.3.
|
|
154
|
+
"@budsbox/eslint_presets-lib": "^1.0.4",
|
|
155
|
+
"@budsbox/eslint_presets-tools": "^1.0.4",
|
|
156
|
+
"@budsbox/tsconfigs": "^4.3.1",
|
|
137
157
|
"@eslint/js": "^9.26.0",
|
|
138
158
|
"@types/eslint": "^9.6.1",
|
|
139
159
|
"@typescript-eslint/parser": "^8.32.1",
|