overridable-fn 2.1.2 → 3.0.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/index.d.ts CHANGED
@@ -1,19 +1,25 @@
1
- import { MapCache } from 'lodash';
2
-
3
- interface FnImpl<TArgs extends any[], TRet> {
1
+ export interface MapCache {
2
+ delete(key: any): boolean;
3
+ get(key: any): any;
4
+ has(key: any): boolean;
5
+ set(key: any, value: any): this;
6
+ clear?(): void;
7
+ }
8
+ export interface FnImpl<TArgs extends any[], TRet> {
4
9
  (...args: TArgs): TRet;
5
10
  }
6
- interface WrappedFnImpl<TArgs extends any[], TRet> {
11
+ export interface WrappedFnImpl<TArgs extends any[], TRet> {
7
12
  (...args: TArgs): TRet;
8
13
  unwrap: () => FnImpl<TArgs, TRet>;
9
14
  override: (provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>) => {
10
15
  restore: () => void;
11
16
  };
12
17
  }
13
- declare const fn: (<TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet>) & {
14
- memo: <TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet> & {
15
- cache: MapCache;
16
- };
18
+ export interface MemoizedWrappedFnImpl<TArgs extends any[], TRet> extends WrappedFnImpl<TArgs, TRet> {
19
+ cache: MapCache;
20
+ }
21
+ declare const _fn: <TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet>;
22
+ export declare const fn: typeof _fn & {
23
+ memo: <TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => MemoizedWrappedFnImpl<TArgs, TRet>;
17
24
  };
18
-
19
- export { type FnImpl, type WrappedFnImpl, fn };
25
+ export {};
package/dist/index.js CHANGED
@@ -1,473 +1,67 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __commonJS = (cb, mod) => function __require() {
8
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
19
- // If the importer is in node compatibility mode or this is not an ESM
20
- // file that has been converted to a CommonJS file using a Babel-
21
- // compatible transform (i.e. "__esModule" has not been set), then set
22
- // "default" to the CommonJS "module.exports" for node compatibility.
23
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
24
- mod
25
- ));
26
-
27
- // node_modules/.pnpm/lodash.assign@4.2.0/node_modules/lodash.assign/index.js
28
- var require_lodash = __commonJS({
29
- "node_modules/.pnpm/lodash.assign@4.2.0/node_modules/lodash.assign/index.js"(exports, module) {
30
- "use strict";
31
- var MAX_SAFE_INTEGER = 9007199254740991;
32
- var argsTag = "[object Arguments]";
33
- var funcTag = "[object Function]";
34
- var genTag = "[object GeneratorFunction]";
35
- var reIsUint = /^(?:0|[1-9]\d*)$/;
36
- function apply(func, thisArg, args) {
37
- switch (args.length) {
38
- case 0:
39
- return func.call(thisArg);
40
- case 1:
41
- return func.call(thisArg, args[0]);
42
- case 2:
43
- return func.call(thisArg, args[0], args[1]);
44
- case 3:
45
- return func.call(thisArg, args[0], args[1], args[2]);
46
- }
47
- return func.apply(thisArg, args);
48
- }
49
- function baseTimes(n, iteratee) {
50
- var index = -1, result = Array(n);
51
- while (++index < n) {
52
- result[index] = iteratee(index);
53
- }
54
- return result;
55
- }
56
- function overArg(func, transform) {
57
- return function(arg) {
58
- return func(transform(arg));
59
- };
60
- }
61
- var objectProto = Object.prototype;
62
- var hasOwnProperty = objectProto.hasOwnProperty;
63
- var objectToString = objectProto.toString;
64
- var propertyIsEnumerable = objectProto.propertyIsEnumerable;
65
- var nativeKeys = overArg(Object.keys, Object);
66
- var nativeMax = Math.max;
67
- var nonEnumShadows = !propertyIsEnumerable.call({ "valueOf": 1 }, "valueOf");
68
- function arrayLikeKeys(value, inherited) {
69
- var result = isArray(value) || isArguments(value) ? baseTimes(value.length, String) : [];
70
- var length = result.length, skipIndexes = !!length;
71
- for (var key in value) {
72
- if ((inherited || hasOwnProperty.call(value, key)) && !(skipIndexes && (key == "length" || isIndex(key, length)))) {
73
- result.push(key);
74
- }
75
- }
76
- return result;
77
- }
78
- function assignValue(object, key, value) {
79
- var objValue = object[key];
80
- if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) || value === void 0 && !(key in object)) {
81
- object[key] = value;
82
- }
83
- }
84
- function baseKeys(object) {
85
- if (!isPrototype(object)) {
86
- return nativeKeys(object);
87
- }
88
- var result = [];
89
- for (var key in Object(object)) {
90
- if (hasOwnProperty.call(object, key) && key != "constructor") {
91
- result.push(key);
92
- }
93
- }
94
- return result;
95
- }
96
- function baseRest(func, start) {
97
- start = nativeMax(start === void 0 ? func.length - 1 : start, 0);
98
- return function() {
99
- var args = arguments, index = -1, length = nativeMax(args.length - start, 0), array = Array(length);
100
- while (++index < length) {
101
- array[index] = args[start + index];
102
- }
103
- index = -1;
104
- var otherArgs = Array(start + 1);
105
- while (++index < start) {
106
- otherArgs[index] = args[index];
107
- }
108
- otherArgs[start] = array;
109
- return apply(func, this, otherArgs);
110
- };
111
- }
112
- function copyObject(source, props, object, customizer) {
113
- object || (object = {});
114
- var index = -1, length = props.length;
115
- while (++index < length) {
116
- var key = props[index];
117
- var newValue = customizer ? customizer(object[key], source[key], key, object, source) : void 0;
118
- assignValue(object, key, newValue === void 0 ? source[key] : newValue);
119
- }
120
- return object;
121
- }
122
- function createAssigner(assigner) {
123
- return baseRest(function(object, sources) {
124
- var index = -1, length = sources.length, customizer = length > 1 ? sources[length - 1] : void 0, guard = length > 2 ? sources[2] : void 0;
125
- customizer = assigner.length > 3 && typeof customizer == "function" ? (length--, customizer) : void 0;
126
- if (guard && isIterateeCall(sources[0], sources[1], guard)) {
127
- customizer = length < 3 ? void 0 : customizer;
128
- length = 1;
129
- }
130
- object = Object(object);
131
- while (++index < length) {
132
- var source = sources[index];
133
- if (source) {
134
- assigner(object, source, index, customizer);
135
- }
136
- }
137
- return object;
138
- });
139
- }
140
- function isIndex(value, length) {
141
- length = length == null ? MAX_SAFE_INTEGER : length;
142
- return !!length && (typeof value == "number" || reIsUint.test(value)) && (value > -1 && value % 1 == 0 && value < length);
143
- }
144
- function isIterateeCall(value, index, object) {
145
- if (!isObject(object)) {
146
- return false;
147
- }
148
- var type = typeof index;
149
- if (type == "number" ? isArrayLike(object) && isIndex(index, object.length) : type == "string" && index in object) {
150
- return eq(object[index], value);
151
- }
152
- return false;
153
- }
154
- function isPrototype(value) {
155
- var Ctor = value && value.constructor, proto = typeof Ctor == "function" && Ctor.prototype || objectProto;
156
- return value === proto;
157
- }
158
- function eq(value, other) {
159
- return value === other || value !== value && other !== other;
160
- }
161
- function isArguments(value) {
162
- return isArrayLikeObject(value) && hasOwnProperty.call(value, "callee") && (!propertyIsEnumerable.call(value, "callee") || objectToString.call(value) == argsTag);
163
- }
164
- var isArray = Array.isArray;
165
- function isArrayLike(value) {
166
- return value != null && isLength(value.length) && !isFunction(value);
167
- }
168
- function isArrayLikeObject(value) {
169
- return isObjectLike(value) && isArrayLike(value);
170
- }
171
- function isFunction(value) {
172
- var tag = isObject(value) ? objectToString.call(value) : "";
173
- return tag == funcTag || tag == genTag;
174
- }
175
- function isLength(value) {
176
- return typeof value == "number" && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
177
- }
178
- function isObject(value) {
179
- var type = typeof value;
180
- return !!value && (type == "object" || type == "function");
181
- }
182
- function isObjectLike(value) {
183
- return !!value && typeof value == "object";
184
- }
185
- var assign2 = createAssigner(function(object, source) {
186
- if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
187
- copyObject(source, keys(source), object);
188
- return;
189
- }
190
- for (var key in source) {
191
- if (hasOwnProperty.call(source, key)) {
192
- assignValue(object, key, source[key]);
193
- }
194
- }
195
- });
196
- function keys(object) {
197
- return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
198
- }
199
- module.exports = assign2;
200
- }
201
- });
202
-
203
- // node_modules/.pnpm/lodash.memoize@4.1.2/node_modules/lodash.memoize/index.js
204
- var require_lodash2 = __commonJS({
205
- "node_modules/.pnpm/lodash.memoize@4.1.2/node_modules/lodash.memoize/index.js"(exports, module) {
206
- "use strict";
207
- var FUNC_ERROR_TEXT = "Expected a function";
208
- var HASH_UNDEFINED = "__lodash_hash_undefined__";
209
- var funcTag = "[object Function]";
210
- var genTag = "[object GeneratorFunction]";
211
- var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
212
- var reIsHostCtor = /^\[object .+?Constructor\]$/;
213
- var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
214
- var freeSelf = typeof self == "object" && self && self.Object === Object && self;
215
- var root = freeGlobal || freeSelf || Function("return this")();
216
- function getValue(object, key) {
217
- return object == null ? void 0 : object[key];
218
- }
219
- function isHostObject(value) {
220
- var result = false;
221
- if (value != null && typeof value.toString != "function") {
222
- try {
223
- result = !!(value + "");
224
- } catch (e) {
225
- }
226
- }
227
- return result;
228
- }
229
- var arrayProto = Array.prototype;
230
- var funcProto = Function.prototype;
231
- var objectProto = Object.prototype;
232
- var coreJsData = root["__core-js_shared__"];
233
- var maskSrcKey = function() {
234
- var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || "");
235
- return uid ? "Symbol(src)_1." + uid : "";
236
- }();
237
- var funcToString = funcProto.toString;
238
- var hasOwnProperty = objectProto.hasOwnProperty;
239
- var objectToString = objectProto.toString;
240
- var reIsNative = RegExp(
241
- "^" + funcToString.call(hasOwnProperty).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$"
242
- );
243
- var splice = arrayProto.splice;
244
- var Map = getNative(root, "Map");
245
- var nativeCreate = getNative(Object, "create");
246
- function Hash(entries) {
247
- var index = -1, length = entries ? entries.length : 0;
248
- this.clear();
249
- while (++index < length) {
250
- var entry = entries[index];
251
- this.set(entry[0], entry[1]);
252
- }
253
- }
254
- function hashClear() {
255
- this.__data__ = nativeCreate ? nativeCreate(null) : {};
256
- }
257
- function hashDelete(key) {
258
- return this.has(key) && delete this.__data__[key];
259
- }
260
- function hashGet(key) {
261
- var data = this.__data__;
262
- if (nativeCreate) {
263
- var result = data[key];
264
- return result === HASH_UNDEFINED ? void 0 : result;
265
- }
266
- return hasOwnProperty.call(data, key) ? data[key] : void 0;
267
- }
268
- function hashHas(key) {
269
- var data = this.__data__;
270
- return nativeCreate ? data[key] !== void 0 : hasOwnProperty.call(data, key);
271
- }
272
- function hashSet(key, value) {
273
- var data = this.__data__;
274
- data[key] = nativeCreate && value === void 0 ? HASH_UNDEFINED : value;
275
- return this;
276
- }
277
- Hash.prototype.clear = hashClear;
278
- Hash.prototype["delete"] = hashDelete;
279
- Hash.prototype.get = hashGet;
280
- Hash.prototype.has = hashHas;
281
- Hash.prototype.set = hashSet;
282
- function ListCache(entries) {
283
- var index = -1, length = entries ? entries.length : 0;
284
- this.clear();
285
- while (++index < length) {
286
- var entry = entries[index];
287
- this.set(entry[0], entry[1]);
288
- }
289
- }
290
- function listCacheClear() {
291
- this.__data__ = [];
292
- }
293
- function listCacheDelete(key) {
294
- var data = this.__data__, index = assocIndexOf(data, key);
295
- if (index < 0) {
296
- return false;
297
- }
298
- var lastIndex = data.length - 1;
299
- if (index == lastIndex) {
300
- data.pop();
301
- } else {
302
- splice.call(data, index, 1);
303
- }
304
- return true;
305
- }
306
- function listCacheGet(key) {
307
- var data = this.__data__, index = assocIndexOf(data, key);
308
- return index < 0 ? void 0 : data[index][1];
309
- }
310
- function listCacheHas(key) {
311
- return assocIndexOf(this.__data__, key) > -1;
312
- }
313
- function listCacheSet(key, value) {
314
- var data = this.__data__, index = assocIndexOf(data, key);
315
- if (index < 0) {
316
- data.push([key, value]);
317
- } else {
318
- data[index][1] = value;
319
- }
320
- return this;
321
- }
322
- ListCache.prototype.clear = listCacheClear;
323
- ListCache.prototype["delete"] = listCacheDelete;
324
- ListCache.prototype.get = listCacheGet;
325
- ListCache.prototype.has = listCacheHas;
326
- ListCache.prototype.set = listCacheSet;
327
- function MapCache(entries) {
328
- var index = -1, length = entries ? entries.length : 0;
329
- this.clear();
330
- while (++index < length) {
331
- var entry = entries[index];
332
- this.set(entry[0], entry[1]);
333
- }
334
- }
335
- function mapCacheClear() {
336
- this.__data__ = {
337
- "hash": new Hash(),
338
- "map": new (Map || ListCache)(),
339
- "string": new Hash()
340
- };
341
- }
342
- function mapCacheDelete(key) {
343
- return getMapData(this, key)["delete"](key);
344
- }
345
- function mapCacheGet(key) {
346
- return getMapData(this, key).get(key);
347
- }
348
- function mapCacheHas(key) {
349
- return getMapData(this, key).has(key);
350
- }
351
- function mapCacheSet(key, value) {
352
- getMapData(this, key).set(key, value);
353
- return this;
354
- }
355
- MapCache.prototype.clear = mapCacheClear;
356
- MapCache.prototype["delete"] = mapCacheDelete;
357
- MapCache.prototype.get = mapCacheGet;
358
- MapCache.prototype.has = mapCacheHas;
359
- MapCache.prototype.set = mapCacheSet;
360
- function assocIndexOf(array, key) {
361
- var length = array.length;
362
- while (length--) {
363
- if (eq(array[length][0], key)) {
364
- return length;
365
- }
366
- }
367
- return -1;
368
- }
369
- function baseIsNative(value) {
370
- if (!isObject(value) || isMasked(value)) {
371
- return false;
372
- }
373
- var pattern = isFunction(value) || isHostObject(value) ? reIsNative : reIsHostCtor;
374
- return pattern.test(toSource(value));
375
- }
376
- function getMapData(map, key) {
377
- var data = map.__data__;
378
- return isKeyable(key) ? data[typeof key == "string" ? "string" : "hash"] : data.map;
379
- }
380
- function getNative(object, key) {
381
- var value = getValue(object, key);
382
- return baseIsNative(value) ? value : void 0;
383
- }
384
- function isKeyable(value) {
385
- var type = typeof value;
386
- return type == "string" || type == "number" || type == "symbol" || type == "boolean" ? value !== "__proto__" : value === null;
387
- }
388
- function isMasked(func) {
389
- return !!maskSrcKey && maskSrcKey in func;
390
- }
391
- function toSource(func) {
392
- if (func != null) {
393
- try {
394
- return funcToString.call(func);
395
- } catch (e) {
396
- }
397
- try {
398
- return func + "";
399
- } catch (e) {
400
- }
401
- }
402
- return "";
403
- }
404
- function memoize2(func, resolver) {
405
- if (typeof func != "function" || resolver && typeof resolver != "function") {
406
- throw new TypeError(FUNC_ERROR_TEXT);
407
- }
408
- var memoized = function() {
409
- var args = arguments, key = resolver ? resolver.apply(this, args) : args[0], cache = memoized.cache;
410
- if (cache.has(key)) {
411
- return cache.get(key);
412
- }
413
- var result = func.apply(this, args);
414
- memoized.cache = cache.set(key, result);
1
+ // Equivalent of lodash.memoize: caches by first argument
2
+ const memoize = (f) => {
3
+ const memoized = function (...args) {
4
+ const key = args[0];
5
+ const cache = memoized.cache;
6
+ if (cache.has(key))
7
+ return cache.get(key);
8
+ const result = f.apply(this, args);
9
+ memoized.cache = cache.set(key, result) || cache;
415
10
  return result;
416
- };
417
- memoized.cache = new (memoize2.Cache || MapCache)();
418
- return memoized;
419
- }
420
- memoize2.Cache = MapCache;
421
- function eq(value, other) {
422
- return value === other || value !== value && other !== other;
423
- }
424
- function isFunction(value) {
425
- var tag = isObject(value) ? objectToString.call(value) : "";
426
- return tag == funcTag || tag == genTag;
427
- }
428
- function isObject(value) {
429
- var type = typeof value;
430
- return !!value && (type == "object" || type == "function");
431
- }
432
- module.exports = memoize2;
433
- }
434
- });
435
-
436
- // src/index.ts
437
- var import_lodash = __toESM(require_lodash(), 1);
438
- var import_lodash2 = __toESM(require_lodash2(), 1);
439
- var _fn = (defaultImpl) => {
440
- let impl = defaultImpl;
441
- const unwrap = () => impl;
442
- const override = (provideImpl) => {
443
- const curImpl = impl;
444
- impl = provideImpl(curImpl);
445
- const restore = () => {
446
- impl = curImpl;
447
11
  };
448
- return { restore };
449
- };
450
- const invoker = (...args) => {
451
- return impl(...args);
452
- };
453
- invoker.unwrap = unwrap;
454
- invoker.override = override;
455
- return invoker;
12
+ memoized.cache = new Map();
13
+ return memoized;
456
14
  };
457
- var fn = (0, import_lodash.default)(_fn, {
458
- memo: (defaultImpl) => {
459
- const memoized = (0, import_lodash2.default)(defaultImpl);
460
- return (0, import_lodash.default)(fn(memoized), {
461
- get cache() {
462
- return memoized.cache;
463
- },
464
- set cache(cache) {
465
- memoized.cache = cache;
466
- }
467
- });
468
- }
469
- });
470
- export {
471
- fn
15
+ const assign = Object.assign;
16
+ const _fn = (defaultImpl) => {
17
+ let impl = defaultImpl;
18
+ const unwrap = () => impl;
19
+ const override = (provideImpl) => {
20
+ const curImpl = impl;
21
+ impl = provideImpl(curImpl);
22
+ const restore = () => {
23
+ impl = curImpl;
24
+ };
25
+ return { restore };
26
+ };
27
+ const invoker = (...args) => {
28
+ return impl(...args);
29
+ };
30
+ invoker.unwrap = unwrap;
31
+ invoker.override = override;
32
+ return invoker;
472
33
  };
34
+ export const fn = assign(_fn, {
35
+ memo: (defaultImpl) => {
36
+ let memoized = memoize(defaultImpl);
37
+ const wrapped = fn(memoized);
38
+ // Overriding implementations are memoized too, each with its own cache.
39
+ // Restoring brings back the previous implementation along with its cache.
40
+ const baseOverride = wrapped.override;
41
+ const override = (provideImpl) => {
42
+ const prevMemoized = memoized;
43
+ const { restore } = baseOverride((curImpl) => {
44
+ memoized = memoize(provideImpl(curImpl));
45
+ return memoized;
46
+ });
47
+ return {
48
+ restore: () => {
49
+ memoized = prevMemoized;
50
+ restore();
51
+ }
52
+ };
53
+ };
54
+ wrapped.override = override;
55
+ return Object.defineProperty(wrapped, "cache", {
56
+ get() {
57
+ return memoized.cache;
58
+ },
59
+ set(cache) {
60
+ memoized.cache = cache;
61
+ },
62
+ enumerable: true,
63
+ configurable: true
64
+ });
65
+ }
66
+ });
473
67
  //# sourceMappingURL=index.js.map