pepka 1.4.3 → 1.5.3

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/bundle.cjs CHANGED
@@ -486,11 +486,21 @@ const replace = curry3((a, b, where
486
486
  const filter = curry2((cond, data) => isArray(data)
487
487
  ? data.filter(cond)
488
488
  : qfilter(cond, { ...data }));
489
- const memoize = (fn) => {
490
- let cache;
491
- let cached = false;
492
- return () => cached ? cache : (cached = true, cache = fn());
493
- };
489
+ /** Saves result of a function with given key and avoids calling it again.
490
+ * @param {(...args: Args) string} keyGen that takes the same args and returns a key for the cache.
491
+ * @param {(...args: Args) any} fn to be cached.
492
+ */
493
+ const memoize = curry2((keyGen, fn) => {
494
+ const cache = {};
495
+ return (...args) => {
496
+ const key = keyGen(...args);
497
+ if (key in cache)
498
+ return cache[key];
499
+ const res = fn(...args);
500
+ cache[key] = res;
501
+ return res;
502
+ };
503
+ });
494
504
  const mergeShallow = curry2((o1, o2) => Object.assign({}, o1, o2));
495
505
  const mergeDeep = curry2((a, b) => qmergeDeep(clone(a), clone(b)));
496
506
  const mergeDeepX = curry2((a, b) => qmergeDeepX(clone(a), clone(b)));
package/dist/bundle.d.ts CHANGED
@@ -425,7 +425,16 @@ export declare const filter: {
425
425
  (a: (v: any, k: string | number) => boolean): (b: any[] | AnyObject) => any;
426
426
  (a: (v: any, k: string | number) => boolean, b: any[] | AnyObject): any;
427
427
  };
428
- export declare const memoize: (fn: Function) => () => any;
428
+ /** Saves result of a function with given key and avoids calling it again.
429
+ * @param {(...args: Args) string} keyGen that takes the same args and returns a key for the cache.
430
+ * @param {(...args: Args) any} fn to be cached.
431
+ */
432
+ export declare const memoize: {
433
+ (a: symbol, b: AnyFunc<any, any[]>): (a: (...args: any[]) => string) => (...args: any[]) => any;
434
+ (a: (...args: any[]) => string, b: symbol): (b: AnyFunc<any, any[]>) => (...args: any[]) => any;
435
+ (a: (...args: any[]) => string): (b: AnyFunc<any, any[]>) => (...args: any[]) => any;
436
+ (a: (...args: any[]) => string, b: AnyFunc<any, any[]>): (...args: any[]) => any;
437
+ };
429
438
  export declare const mergeShallow: {
430
439
  (a: symbol, b: AnyObject): (a: AnyObject) => AnyObject;
431
440
  (a: AnyObject, b: symbol): (b: AnyObject) => AnyObject;
package/dist/bundle.mjs CHANGED
@@ -484,11 +484,21 @@ const replace = curry3((a, b, where
484
484
  const filter = curry2((cond, data) => isArray(data)
485
485
  ? data.filter(cond)
486
486
  : qfilter(cond, { ...data }));
487
- const memoize = (fn) => {
488
- let cache;
489
- let cached = false;
490
- return () => cached ? cache : (cached = true, cache = fn());
491
- };
487
+ /** Saves result of a function with given key and avoids calling it again.
488
+ * @param {(...args: Args) string} keyGen that takes the same args and returns a key for the cache.
489
+ * @param {(...args: Args) any} fn to be cached.
490
+ */
491
+ const memoize = curry2((keyGen, fn) => {
492
+ const cache = {};
493
+ return (...args) => {
494
+ const key = keyGen(...args);
495
+ if (key in cache)
496
+ return cache[key];
497
+ const res = fn(...args);
498
+ cache[key] = res;
499
+ return res;
500
+ };
501
+ });
492
502
  const mergeShallow = curry2((o1, o2) => Object.assign({}, o1, o2));
493
503
  const mergeDeep = curry2((a, b) => qmergeDeep(clone(a), clone(b)));
494
504
  const mergeDeepX = curry2((a, b) => qmergeDeepX(clone(a), clone(b)));
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "prod": "npm run gentypes && npm run prod:es && npm run prod:cjs",
43
43
  "all": "npm run dev && npm run prod"
44
44
  },
45
- "version": "1.4.3",
45
+ "version": "1.5.3",
46
46
  "ava": {
47
47
  "files": [
48
48
  "./test/specs/*.ts"
package/src/safe.ts CHANGED
@@ -332,11 +332,23 @@ export const filter = curry2(
332
332
  ? data.filter(cond)
333
333
  : qfilter(cond, {...data})
334
334
  )
335
- export const memoize = (fn: Function) => {
336
- let cache: any
337
- let cached = false
338
- return () => cached ? cache : (cached = true, cache = fn())
339
- }
335
+ /** Saves result of a function with given key and avoids calling it again.
336
+ * @param {(...args: Args) string} keyGen that takes the same args and returns a key for the cache.
337
+ * @param {(...args: Args) any} fn to be cached.
338
+ */
339
+ export const memoize = curry2(<Args extends any[]>(
340
+ keyGen: (...args: Args) => string,
341
+ fn: AnyFunc<any, Args>
342
+ ) => {
343
+ const cache: {[key in string]: ReturnType<typeof fn>} = {}
344
+ return (...args: Args): ReturnType<typeof fn> => {
345
+ const key = keyGen(...args)
346
+ if(key in cache) return cache[key]
347
+ const res = fn(...args)
348
+ cache[key] = res
349
+ return res
350
+ }
351
+ })
340
352
  export const mergeShallow = curry2(
341
353
  (o1: AnyObject, o2: AnyObject): AnyObject =>
342
354
  Object.assign({}, o1, o2)