es-toolkit 1.15.1-dev.449 → 1.15.1-dev.451

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.
@@ -54,7 +54,7 @@ export { noop } from '../function/noop.mjs';
54
54
  export { once } from '../function/once.mjs';
55
55
  export { throttle } from '../function/throttle.mjs';
56
56
  export { negate } from '../function/negate.mjs';
57
- export { memoize } from '../function/memoize.mjs';
57
+ export { MemoizeCache, memoize } from '../function/memoize.mjs';
58
58
  export { unary } from '../function/unary.mjs';
59
59
  export { partial } from '../function/partial.mjs';
60
60
  export { partialRight } from '../function/partialRight.mjs';
@@ -54,7 +54,7 @@ export { noop } from '../function/noop.js';
54
54
  export { once } from '../function/once.js';
55
55
  export { throttle } from '../function/throttle.js';
56
56
  export { negate } from '../function/negate.js';
57
- export { memoize } from '../function/memoize.js';
57
+ export { MemoizeCache, memoize } from '../function/memoize.js';
58
58
  export { unary } from '../function/unary.js';
59
59
  export { partial } from '../function/partial.js';
60
60
  export { partialRight } from '../function/partialRight.js';
@@ -5,7 +5,7 @@ export { noop } from './noop.mjs';
5
5
  export { once } from './once.mjs';
6
6
  export { throttle } from './throttle.mjs';
7
7
  export { negate } from './negate.mjs';
8
- export { memoize } from './memoize.mjs';
8
+ export { MemoizeCache, memoize } from './memoize.mjs';
9
9
  export { ary } from './ary.mjs';
10
10
  export { unary } from './unary.mjs';
11
11
  export { partial } from './partial.mjs';
@@ -5,7 +5,7 @@ export { noop } from './noop.js';
5
5
  export { once } from './once.js';
6
6
  export { throttle } from './throttle.js';
7
7
  export { negate } from './negate.js';
8
- export { memoize } from './memoize.js';
8
+ export { MemoizeCache, memoize } from './memoize.js';
9
9
  export { ary } from './ary.js';
10
10
  export { unary } from './unary.js';
11
11
  export { partial } from './partial.js';
@@ -88,16 +88,13 @@ function negate(func) {
88
88
  }
89
89
 
90
90
  function memoize(fn, options = {}) {
91
- const { cache = new Map(), resolver } = options;
92
- if (typeof fn !== 'function' || (resolver && typeof resolver !== 'function')) {
93
- throw new TypeError('Expected a function and an optional resolver function');
94
- }
95
- const memoizedFn = function (...args) {
96
- const key = resolver ? resolver.apply(this, args) : args[0];
91
+ const { cache = new Map(), getCacheKey } = options;
92
+ const memoizedFn = function (arg) {
93
+ const key = getCacheKey ? getCacheKey(arg) : arg;
97
94
  if (cache.has(key)) {
98
95
  return cache.get(key);
99
96
  }
100
- const result = fn.apply(this, args);
97
+ const result = fn.call(this, arg);
101
98
  cache.set(key, result);
102
99
  return result;
103
100
  };
@@ -1,75 +1,81 @@
1
1
  /**
2
- * Memoizes a given function by caching its result based on the arguments provided.
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.
3
5
  *
4
- * @template F - The type of the function to memoize.
5
- * @template K - The type of the cache key.
6
- * @param func
7
- * @param {F} fn - The function to memoize.
8
- * @param {MemoizeOptions<K, ReturnType<F>>} [options] - An options object with a resolver function and/or a custom cache object.
9
- * @returns {F & { cache: Cache<K, ReturnType<F>> }} - The memoized function with a cache property.
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.
10
9
  *
11
- * @throws {TypeError} If the provided function or resolver is not valid.
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.
12
19
  *
13
20
  * @example
14
- * // Basic usage with default cache
15
- * const add = (a, b) => a + b;
21
+ * // Example using the default cache
22
+ * const add = (x: number) => x + 10;
16
23
  * const memoizedAdd = memoize(add);
17
- * console.log(memoizedAdd(1, 2)); // 3
24
+ *
25
+ * console.log(memoizedAdd(5)); // 15
26
+ * console.log(memoizedAdd(5)); // 15 (cached result)
18
27
  * console.log(memoizedAdd.cache.size); // 1
19
28
  *
20
29
  * @example
21
- * // Using a custom resolver
22
- * const resolver = (...args) => args.join('-');
23
- * const memoizedAddWithResolver = memoize(add, { resolver });
24
- * console.log(memoizedAddWithResolver(1, 2)); // 3
25
- * console.log(memoizedAddWithResolver.cache.size); // 1
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
26
36
  *
27
37
  * @example
28
- * // Using a custom cache
29
- * class CustomCache {
30
- * constructor() {
31
- * this.store = {};
32
- * }
33
- * set(key, value) {
34
- * this.store[key] = value;
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);
35
44
  * }
36
- * get(key) {
37
- * return this.store[key];
45
+ *
46
+ * get(key: K): T | undefined {
47
+ * return this.cache.get(key);
38
48
  * }
39
- * has(key) {
40
- * return key in this.store;
49
+ *
50
+ * has(key: K): boolean {
51
+ * return this.cache.has(key);
41
52
  * }
42
- * delete(key) {
43
- * delete this.store[key];
53
+ *
54
+ * delete(key: K): boolean {
55
+ * return this.cache.delete(key);
44
56
  * }
45
- * clear() {
46
- * this.store = {};
57
+ *
58
+ * clear(): void {
59
+ * this.cache.clear();
47
60
  * }
48
- * get size() {
49
- * return Object.keys(this.store).length;
61
+ *
62
+ * get size(): number {
63
+ * return this.cache.size;
50
64
  * }
51
65
  * }
52
- * const customCache = new CustomCache();
53
- * const memoizedAddWithCustomCache = memoize(add, { cache: customCache });
54
- * console.log(memoizedAddWithCustomCache(1, 2)); // 3
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)
55
70
  * console.log(memoizedAddWithCustomCache.cache.size); // 1
56
- *
57
- * @example
58
- * // Using both custom resolver and custom cache
59
- * const resolver = (...args) => args.join('-');
60
- * const customCache = new CustomCache();
61
- * const memoizedAddWithBoth = memoize(add, { resolver, cache: customCache });
62
- * console.log(memoizedAddWithBoth(1, 2)); // 3
63
- * console.log(memoizedAddWithBoth.cache.size); // 1
64
71
  */
65
- declare function memoize<F extends (...args: any[]) => any, K = Parameters<F>[0]>(fn: F, options?: MemoizeOptions<K, ReturnType<F>>): F & {
66
- cache: Cache<K, ReturnType<F>>;
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>>;
67
77
  };
68
- interface MemoizeOptions<K, V> {
69
- cache?: Cache<K, V>;
70
- resolver?: (...args: any[]) => K;
71
- }
72
- interface Cache<K, V> {
78
+ interface MemoizeCache<K, V> {
73
79
  set(key: K, value: V): void;
74
80
  get(key: K): V | undefined;
75
81
  has(key: K): boolean;
@@ -78,4 +84,4 @@ interface Cache<K, V> {
78
84
  size: number;
79
85
  }
80
86
 
81
- export { type Cache, type MemoizeOptions, memoize };
87
+ export { type MemoizeCache, memoize };
@@ -1,75 +1,81 @@
1
1
  /**
2
- * Memoizes a given function by caching its result based on the arguments provided.
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.
3
5
  *
4
- * @template F - The type of the function to memoize.
5
- * @template K - The type of the cache key.
6
- * @param func
7
- * @param {F} fn - The function to memoize.
8
- * @param {MemoizeOptions<K, ReturnType<F>>} [options] - An options object with a resolver function and/or a custom cache object.
9
- * @returns {F & { cache: Cache<K, ReturnType<F>> }} - The memoized function with a cache property.
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.
10
9
  *
11
- * @throws {TypeError} If the provided function or resolver is not valid.
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.
12
19
  *
13
20
  * @example
14
- * // Basic usage with default cache
15
- * const add = (a, b) => a + b;
21
+ * // Example using the default cache
22
+ * const add = (x: number) => x + 10;
16
23
  * const memoizedAdd = memoize(add);
17
- * console.log(memoizedAdd(1, 2)); // 3
24
+ *
25
+ * console.log(memoizedAdd(5)); // 15
26
+ * console.log(memoizedAdd(5)); // 15 (cached result)
18
27
  * console.log(memoizedAdd.cache.size); // 1
19
28
  *
20
29
  * @example
21
- * // Using a custom resolver
22
- * const resolver = (...args) => args.join('-');
23
- * const memoizedAddWithResolver = memoize(add, { resolver });
24
- * console.log(memoizedAddWithResolver(1, 2)); // 3
25
- * console.log(memoizedAddWithResolver.cache.size); // 1
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
26
36
  *
27
37
  * @example
28
- * // Using a custom cache
29
- * class CustomCache {
30
- * constructor() {
31
- * this.store = {};
32
- * }
33
- * set(key, value) {
34
- * this.store[key] = value;
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);
35
44
  * }
36
- * get(key) {
37
- * return this.store[key];
45
+ *
46
+ * get(key: K): T | undefined {
47
+ * return this.cache.get(key);
38
48
  * }
39
- * has(key) {
40
- * return key in this.store;
49
+ *
50
+ * has(key: K): boolean {
51
+ * return this.cache.has(key);
41
52
  * }
42
- * delete(key) {
43
- * delete this.store[key];
53
+ *
54
+ * delete(key: K): boolean {
55
+ * return this.cache.delete(key);
44
56
  * }
45
- * clear() {
46
- * this.store = {};
57
+ *
58
+ * clear(): void {
59
+ * this.cache.clear();
47
60
  * }
48
- * get size() {
49
- * return Object.keys(this.store).length;
61
+ *
62
+ * get size(): number {
63
+ * return this.cache.size;
50
64
  * }
51
65
  * }
52
- * const customCache = new CustomCache();
53
- * const memoizedAddWithCustomCache = memoize(add, { cache: customCache });
54
- * console.log(memoizedAddWithCustomCache(1, 2)); // 3
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)
55
70
  * console.log(memoizedAddWithCustomCache.cache.size); // 1
56
- *
57
- * @example
58
- * // Using both custom resolver and custom cache
59
- * const resolver = (...args) => args.join('-');
60
- * const customCache = new CustomCache();
61
- * const memoizedAddWithBoth = memoize(add, { resolver, cache: customCache });
62
- * console.log(memoizedAddWithBoth(1, 2)); // 3
63
- * console.log(memoizedAddWithBoth.cache.size); // 1
64
71
  */
65
- declare function memoize<F extends (...args: any[]) => any, K = Parameters<F>[0]>(fn: F, options?: MemoizeOptions<K, ReturnType<F>>): F & {
66
- cache: Cache<K, ReturnType<F>>;
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>>;
67
77
  };
68
- interface MemoizeOptions<K, V> {
69
- cache?: Cache<K, V>;
70
- resolver?: (...args: any[]) => K;
71
- }
72
- interface Cache<K, V> {
78
+ interface MemoizeCache<K, V> {
73
79
  set(key: K, value: V): void;
74
80
  get(key: K): V | undefined;
75
81
  has(key: K): boolean;
@@ -78,4 +84,4 @@ interface Cache<K, V> {
78
84
  size: number;
79
85
  }
80
86
 
81
- export { type Cache, type MemoizeOptions, memoize };
87
+ export { type MemoizeCache, memoize };
@@ -1,14 +1,11 @@
1
1
  function memoize(fn, options = {}) {
2
- const { cache = new Map(), resolver } = options;
3
- if (typeof fn !== 'function' || (resolver && typeof resolver !== 'function')) {
4
- throw new TypeError('Expected a function and an optional resolver function');
5
- }
6
- const memoizedFn = function (...args) {
7
- const key = resolver ? resolver.apply(this, args) : args[0];
2
+ const { cache = new Map(), getCacheKey } = options;
3
+ const memoizedFn = function (arg) {
4
+ const key = getCacheKey ? getCacheKey(arg) : arg;
8
5
  if (cache.has(key)) {
9
6
  return cache.get(key);
10
7
  }
11
- const result = fn.apply(this, args);
8
+ const result = fn.call(this, arg);
12
9
  cache.set(key, result);
13
10
  return result;
14
11
  };
package/dist/index.d.mts CHANGED
@@ -60,7 +60,7 @@ export { noop } from './function/noop.mjs';
60
60
  export { once } from './function/once.mjs';
61
61
  export { throttle } from './function/throttle.mjs';
62
62
  export { negate } from './function/negate.mjs';
63
- export { memoize } from './function/memoize.mjs';
63
+ export { MemoizeCache, memoize } from './function/memoize.mjs';
64
64
  export { ary } from './function/ary.mjs';
65
65
  export { unary } from './function/unary.mjs';
66
66
  export { partial } from './function/partial.mjs';
package/dist/index.d.ts CHANGED
@@ -60,7 +60,7 @@ export { noop } from './function/noop.js';
60
60
  export { once } from './function/once.js';
61
61
  export { throttle } from './function/throttle.js';
62
62
  export { negate } from './function/negate.js';
63
- export { memoize } from './function/memoize.js';
63
+ export { MemoizeCache, memoize } from './function/memoize.js';
64
64
  export { ary } from './function/ary.js';
65
65
  export { unary } from './function/unary.js';
66
66
  export { partial } from './function/partial.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.15.1-dev.449+3cb5c804",
4
+ "version": "1.15.1-dev.451+9d68f567",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {