es-toolkit 1.37.2-dev.1261 → 1.37.2-dev.1263

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.
@@ -97,7 +97,7 @@ export { delay } from './function/delay.mjs';
97
97
  export { flip } from './function/flip.mjs';
98
98
  export { flow } from './function/flow.mjs';
99
99
  export { flowRight } from './function/flowRight.mjs';
100
- export { memoize } from '../function/memoize.mjs';
100
+ export { memoize } from './function/memoize.mjs';
101
101
  export { negate } from './function/negate.mjs';
102
102
  export { nthArg } from './function/nthArg.mjs';
103
103
  export { once } from '../function/once.mjs';
@@ -97,7 +97,7 @@ export { delay } from './function/delay.js';
97
97
  export { flip } from './function/flip.js';
98
98
  export { flow } from './function/flow.js';
99
99
  export { flowRight } from './function/flowRight.js';
100
- export { memoize } from '../function/memoize.js';
100
+ export { memoize } from './function/memoize.js';
101
101
  export { negate } from './function/negate.js';
102
102
  export { nthArg } from './function/nthArg.js';
103
103
  export { once } from '../function/once.js';
@@ -97,7 +97,7 @@ export { delay } from './function/delay.mjs';
97
97
  export { flip } from './function/flip.mjs';
98
98
  export { flow } from './function/flow.mjs';
99
99
  export { flowRight } from './function/flowRight.mjs';
100
- export { memoize } from '../function/memoize.mjs';
100
+ export { memoize } from './function/memoize.mjs';
101
101
  export { negate } from './function/negate.mjs';
102
102
  export { nthArg } from './function/nthArg.mjs';
103
103
  export { once } from '../function/once.mjs';
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Interface defining a cache implementation for the memoize function.
3
+ * Provides methods for storing, retrieving, and managing cached values.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * // Implementing a simple MemoizeCache
8
+ * class SimpleCache implements MemoizeCache {
9
+ * private entries = new Map<any, any>();
10
+ *
11
+ * delete(key: any): boolean {
12
+ * return this.entries.delete(key);
13
+ * }
14
+ *
15
+ * get(key: any): any {
16
+ * return this.entries.get(key);
17
+ * }
18
+ *
19
+ * has(key: any): boolean {
20
+ * return this.entries.has(key);
21
+ * }
22
+ *
23
+ * set(key: any, value: any): this {
24
+ * this.entries.set(key, value);
25
+ * return this;
26
+ * }
27
+ *
28
+ * clear(): void {
29
+ * this.entries.clear();
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ interface MemoizeCache {
35
+ /**
36
+ * Optional internal data structure for the cache implementation.
37
+ * This is primarily used for testing and internal operations.
38
+ */
39
+ __data__?: any;
40
+ /**
41
+ * Removes the value associated with the specified key from the cache.
42
+ *
43
+ * @param key - The key of the value to remove
44
+ * @returns `true` if an element was removed, `false` if the key wasn't found
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * cache.set('user', { id: 123, name: 'John' });
49
+ * cache.delete('user'); // Returns true
50
+ * cache.delete('unknown'); // Returns false
51
+ * ```
52
+ */
53
+ delete(key: any): boolean;
54
+ /**
55
+ * Retrieves the value associated with the specified key from the cache.
56
+ *
57
+ * @param key - The key of the value to retrieve
58
+ * @returns The cached value or undefined if not found
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * cache.set('user', { id: 123, name: 'John' });
63
+ * cache.get('user'); // Returns { id: 123, name: 'John' }
64
+ * cache.get('unknown'); // Returns undefined
65
+ * ```
66
+ */
67
+ get(key: any): any;
68
+ /**
69
+ * Checks if the cache contains a value for the specified key.
70
+ *
71
+ * @param key - The key to check for existence
72
+ * @returns `true` if the key exists in the cache, otherwise `false`
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * cache.set('user', { id: 123, name: 'John' });
77
+ * cache.has('user'); // Returns true
78
+ * cache.has('unknown'); // Returns false
79
+ * ```
80
+ */
81
+ has(key: any): boolean;
82
+ /**
83
+ * Stores a value in the cache with the specified key.
84
+ * If the key already exists, its value is updated.
85
+ *
86
+ * @param key - The key to associate with the value
87
+ * @param value - The value to store in the cache
88
+ * @returns The cache instance for method chaining
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * cache.set('user', { id: 123, name: 'John' })
93
+ * .set('settings', { theme: 'dark' });
94
+ * ```
95
+ */
96
+ set(key: any, value: any): this;
97
+ /**
98
+ * Removes all key-value pairs from the cache.
99
+ * This method is optional as some cache implementations may be immutable.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * cache.set('user', { id: 123, name: 'John' });
104
+ * cache.set('settings', { theme: 'dark' });
105
+ * cache.clear(); // Cache is now empty
106
+ * ```
107
+ */
108
+ clear?(): void;
109
+ }
110
+ /**
111
+ * Interface for the constructor of cache implementations.
112
+ * This allows for creating new cache instances with optional initial entries.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * // Use a custom cache constructor with memoize
117
+ * memoize.Cache = CustomCache;
118
+ *
119
+ * // Create a cache with initial entries
120
+ * const cache = new CustomCache([
121
+ * ['key1', 'value1'],
122
+ * ['key2', 'value2']
123
+ * ]);
124
+ * ```
125
+ */
126
+ interface MemoizeCacheConstructor {
127
+ /**
128
+ * Creates a new cache instance.
129
+ *
130
+ * @param entries - Optional array of key-value pairs to initialize the cache with
131
+ * @returns A new cache instance
132
+ */
133
+ new (entries?: Array<[any, any]>): MemoizeCache;
134
+ }
135
+ /**
136
+ * Represents a function that has been memoized.
137
+ * A memoized function maintains the same signature as the original function
138
+ * but adds a cache property to store previously computed results.
139
+ *
140
+ * @template T - The type of the original function being memoized
141
+ */
142
+ interface MemoizedFunction<T extends (...args: any) => any> {
143
+ /**
144
+ * Calls the function with the provided arguments, using cached results when available
145
+ *
146
+ * @param {...Parameters<T>} args - The arguments to pass to the original function
147
+ * @returns {ReturnType<T>} The result of the function call, either from cache or freshly computed
148
+ */
149
+ (...args: Parameters<T>): ReturnType<T>;
150
+ /**
151
+ * The cache storing previously computed results
152
+ */
153
+ cache: MemoizeCache;
154
+ }
155
+ /**
156
+ * Creates a function that memoizes the result of func. If resolver is provided it determines the cache key for
157
+ * storing the result based on the arguments provided to the memoized function. By default, the first argument
158
+ * provided to the memoized function is coerced to a string and used as the cache key. The func is invoked with
159
+ * the this binding of the memoized function.
160
+ *
161
+ * @param func The function to have its output memoized.
162
+ * @param resolver The function to resolve the cache key.
163
+ * @return Returns the new memoizing function.
164
+ */
165
+ declare function memoize<T extends (...args: any) => any>(func: T, resolver?: (...args: Parameters<T>) => any): MemoizedFunction<T>;
166
+ declare namespace memoize {
167
+ var Cache: MemoizeCacheConstructor;
168
+ }
169
+
170
+ export { memoize };
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Interface defining a cache implementation for the memoize function.
3
+ * Provides methods for storing, retrieving, and managing cached values.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * // Implementing a simple MemoizeCache
8
+ * class SimpleCache implements MemoizeCache {
9
+ * private entries = new Map<any, any>();
10
+ *
11
+ * delete(key: any): boolean {
12
+ * return this.entries.delete(key);
13
+ * }
14
+ *
15
+ * get(key: any): any {
16
+ * return this.entries.get(key);
17
+ * }
18
+ *
19
+ * has(key: any): boolean {
20
+ * return this.entries.has(key);
21
+ * }
22
+ *
23
+ * set(key: any, value: any): this {
24
+ * this.entries.set(key, value);
25
+ * return this;
26
+ * }
27
+ *
28
+ * clear(): void {
29
+ * this.entries.clear();
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ interface MemoizeCache {
35
+ /**
36
+ * Optional internal data structure for the cache implementation.
37
+ * This is primarily used for testing and internal operations.
38
+ */
39
+ __data__?: any;
40
+ /**
41
+ * Removes the value associated with the specified key from the cache.
42
+ *
43
+ * @param key - The key of the value to remove
44
+ * @returns `true` if an element was removed, `false` if the key wasn't found
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * cache.set('user', { id: 123, name: 'John' });
49
+ * cache.delete('user'); // Returns true
50
+ * cache.delete('unknown'); // Returns false
51
+ * ```
52
+ */
53
+ delete(key: any): boolean;
54
+ /**
55
+ * Retrieves the value associated with the specified key from the cache.
56
+ *
57
+ * @param key - The key of the value to retrieve
58
+ * @returns The cached value or undefined if not found
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * cache.set('user', { id: 123, name: 'John' });
63
+ * cache.get('user'); // Returns { id: 123, name: 'John' }
64
+ * cache.get('unknown'); // Returns undefined
65
+ * ```
66
+ */
67
+ get(key: any): any;
68
+ /**
69
+ * Checks if the cache contains a value for the specified key.
70
+ *
71
+ * @param key - The key to check for existence
72
+ * @returns `true` if the key exists in the cache, otherwise `false`
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * cache.set('user', { id: 123, name: 'John' });
77
+ * cache.has('user'); // Returns true
78
+ * cache.has('unknown'); // Returns false
79
+ * ```
80
+ */
81
+ has(key: any): boolean;
82
+ /**
83
+ * Stores a value in the cache with the specified key.
84
+ * If the key already exists, its value is updated.
85
+ *
86
+ * @param key - The key to associate with the value
87
+ * @param value - The value to store in the cache
88
+ * @returns The cache instance for method chaining
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * cache.set('user', { id: 123, name: 'John' })
93
+ * .set('settings', { theme: 'dark' });
94
+ * ```
95
+ */
96
+ set(key: any, value: any): this;
97
+ /**
98
+ * Removes all key-value pairs from the cache.
99
+ * This method is optional as some cache implementations may be immutable.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * cache.set('user', { id: 123, name: 'John' });
104
+ * cache.set('settings', { theme: 'dark' });
105
+ * cache.clear(); // Cache is now empty
106
+ * ```
107
+ */
108
+ clear?(): void;
109
+ }
110
+ /**
111
+ * Interface for the constructor of cache implementations.
112
+ * This allows for creating new cache instances with optional initial entries.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * // Use a custom cache constructor with memoize
117
+ * memoize.Cache = CustomCache;
118
+ *
119
+ * // Create a cache with initial entries
120
+ * const cache = new CustomCache([
121
+ * ['key1', 'value1'],
122
+ * ['key2', 'value2']
123
+ * ]);
124
+ * ```
125
+ */
126
+ interface MemoizeCacheConstructor {
127
+ /**
128
+ * Creates a new cache instance.
129
+ *
130
+ * @param entries - Optional array of key-value pairs to initialize the cache with
131
+ * @returns A new cache instance
132
+ */
133
+ new (entries?: Array<[any, any]>): MemoizeCache;
134
+ }
135
+ /**
136
+ * Represents a function that has been memoized.
137
+ * A memoized function maintains the same signature as the original function
138
+ * but adds a cache property to store previously computed results.
139
+ *
140
+ * @template T - The type of the original function being memoized
141
+ */
142
+ interface MemoizedFunction<T extends (...args: any) => any> {
143
+ /**
144
+ * Calls the function with the provided arguments, using cached results when available
145
+ *
146
+ * @param {...Parameters<T>} args - The arguments to pass to the original function
147
+ * @returns {ReturnType<T>} The result of the function call, either from cache or freshly computed
148
+ */
149
+ (...args: Parameters<T>): ReturnType<T>;
150
+ /**
151
+ * The cache storing previously computed results
152
+ */
153
+ cache: MemoizeCache;
154
+ }
155
+ /**
156
+ * Creates a function that memoizes the result of func. If resolver is provided it determines the cache key for
157
+ * storing the result based on the arguments provided to the memoized function. By default, the first argument
158
+ * provided to the memoized function is coerced to a string and used as the cache key. The func is invoked with
159
+ * the this binding of the memoized function.
160
+ *
161
+ * @param func The function to have its output memoized.
162
+ * @param resolver The function to resolve the cache key.
163
+ * @return Returns the new memoizing function.
164
+ */
165
+ declare function memoize<T extends (...args: any) => any>(func: T, resolver?: (...args: Parameters<T>) => any): MemoizedFunction<T>;
166
+ declare namespace memoize {
167
+ var Cache: MemoizeCacheConstructor;
168
+ }
169
+
170
+ export { memoize };
@@ -0,0 +1,171 @@
1
+ import { eq } from '../util/eq.mjs';
2
+
3
+ class Hash {
4
+ static hasOwnProperty = Object.prototype.hasOwnProperty;
5
+ __data__ = {};
6
+ size = 0;
7
+ constructor(entries = []) {
8
+ if (entries && entries.length) {
9
+ const length = entries.length;
10
+ for (let i = 0; i < length; i++) {
11
+ const [key, value] = entries[i];
12
+ this.set(key, value);
13
+ }
14
+ }
15
+ }
16
+ clear() {
17
+ this.__data__ = {};
18
+ this.size = 0;
19
+ }
20
+ delete(key) {
21
+ const hasKey = this.has(key);
22
+ if (hasKey) {
23
+ delete this.__data__[key];
24
+ this.size--;
25
+ }
26
+ return hasKey;
27
+ }
28
+ get(key) {
29
+ const data = this.__data__;
30
+ return Hash.hasOwnProperty.call(data, key) ? data[key] : undefined;
31
+ }
32
+ has(key) {
33
+ return Hash.hasOwnProperty.call(this.__data__, key);
34
+ }
35
+ set(key, value) {
36
+ const hadKey = this.has(key);
37
+ this.__data__[key] = value === undefined ? '__lodash_hash_undefined__' : value;
38
+ if (!hadKey) {
39
+ this.size++;
40
+ }
41
+ return this;
42
+ }
43
+ }
44
+ class ListCache {
45
+ __data__ = [];
46
+ size = 0;
47
+ constructor(entries = []) {
48
+ if (entries && entries.length) {
49
+ const length = entries.length;
50
+ for (let i = 0; i < length; i++) {
51
+ const [key, value] = entries[i];
52
+ this.set(key, value);
53
+ }
54
+ }
55
+ }
56
+ clear() {
57
+ this.__data__ = [];
58
+ this.size = 0;
59
+ }
60
+ delete(key) {
61
+ const index = this.findIndex(key);
62
+ if (index < 0) {
63
+ return false;
64
+ }
65
+ this.__data__.splice(index, 1);
66
+ this.size--;
67
+ return true;
68
+ }
69
+ get(key) {
70
+ const index = this.findIndex(key);
71
+ return index < 0 ? undefined : this.__data__[index][1];
72
+ }
73
+ has(key) {
74
+ return this.findIndex(key) >= 0;
75
+ }
76
+ set(key, value) {
77
+ const index = this.findIndex(key);
78
+ if (index < 0) {
79
+ this.__data__.push([key, value]);
80
+ this.size++;
81
+ }
82
+ else {
83
+ this.__data__[index][1] = value;
84
+ }
85
+ return this;
86
+ }
87
+ findIndex(key) {
88
+ const data = this.__data__;
89
+ let length = data.length;
90
+ while (length--) {
91
+ if (eq(data[length][0], key)) {
92
+ return length;
93
+ }
94
+ }
95
+ return -1;
96
+ }
97
+ }
98
+ class MapCache {
99
+ size = 0;
100
+ __data__ = {
101
+ hash: new Hash(),
102
+ map: typeof Map !== 'undefined' ? new Map() : new ListCache(),
103
+ string: new Hash(),
104
+ };
105
+ constructor(entries = []) {
106
+ if (entries && entries.length) {
107
+ const length = entries.length;
108
+ for (let i = 0; i < length; i++) {
109
+ const [key, value] = entries[i];
110
+ this.set(key, value);
111
+ }
112
+ }
113
+ }
114
+ clear() {
115
+ this.size = 0;
116
+ this.__data__ = {
117
+ hash: new Hash(),
118
+ map: typeof Map !== 'undefined' ? new Map() : new ListCache(),
119
+ string: new Hash(),
120
+ };
121
+ }
122
+ delete(key) {
123
+ const result = this.getMapData(key)['delete'](key);
124
+ this.size -= result ? 1 : 0;
125
+ return result;
126
+ }
127
+ get(key) {
128
+ return this.getMapData(key).get(key);
129
+ }
130
+ has(key) {
131
+ return this.getMapData(key).has(key);
132
+ }
133
+ set(key, value) {
134
+ const data = this.getMapData(key);
135
+ const size = data.size;
136
+ data.set(key, value);
137
+ this.size += data.size === size ? 0 : 1;
138
+ return this;
139
+ }
140
+ isKeyable(value) {
141
+ const type = typeof value;
142
+ return type === 'string' || type === 'number' || type === 'symbol' || type === 'boolean'
143
+ ? value !== '__proto__'
144
+ : value === null;
145
+ }
146
+ getMapData(key) {
147
+ const data = this.__data__;
148
+ return this.isKeyable(key) ? data[typeof key === 'string' ? 'string' : 'hash'] : data.map;
149
+ }
150
+ }
151
+ function memoize(func, resolver) {
152
+ if (typeof func !== 'function' || (resolver != null && typeof resolver !== 'function')) {
153
+ throw new TypeError('Expected a function');
154
+ }
155
+ const memoized = function (...args) {
156
+ const key = resolver ? resolver.apply(this, args) : args[0];
157
+ const cache = memoized.cache;
158
+ if (cache.has(key)) {
159
+ return cache.get(key);
160
+ }
161
+ const result = func.apply(this, args);
162
+ memoized.cache = cache.set(key, result) || cache;
163
+ return result;
164
+ };
165
+ const CacheConstructor = memoize.Cache || MapCache;
166
+ memoized.cache = new CacheConstructor();
167
+ return memoized;
168
+ }
169
+ memoize.Cache = MapCache;
170
+
171
+ export { memoize };
@@ -97,7 +97,7 @@ export { delay } from './function/delay.mjs';
97
97
  export { flip } from './function/flip.mjs';
98
98
  export { flow } from './function/flow.mjs';
99
99
  export { flowRight } from './function/flowRight.mjs';
100
- export { memoize } from '../function/memoize.mjs';
100
+ export { memoize } from './function/memoize.mjs';
101
101
  export { negate } from './function/negate.mjs';
102
102
  export { nthArg } from './function/nthArg.mjs';
103
103
  export { once } from '../function/once.mjs';
@@ -97,7 +97,7 @@ export { delay } from './function/delay.js';
97
97
  export { flip } from './function/flip.js';
98
98
  export { flow } from './function/flow.js';
99
99
  export { flowRight } from './function/flowRight.js';
100
- export { memoize } from '../function/memoize.js';
100
+ export { memoize } from './function/memoize.js';
101
101
  export { negate } from './function/negate.js';
102
102
  export { nthArg } from './function/nthArg.js';
103
103
  export { once } from '../function/once.js';