@chriscdn/memoize 1.0.12 → 2.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/package.json CHANGED
@@ -1,33 +1,21 @@
1
1
  {
2
2
  "name": "@chriscdn/memoize",
3
- "version": "1.0.12",
3
+ "version": "2.0.0",
4
4
  "description": "Memoize a synchronous or asynchronous function.",
5
5
  "repository": "https://github.com/chriscdn/memoize",
6
6
  "author": "Christopher Meyer <chris@schwiiz.org>",
7
7
  "license": "MIT",
8
8
  "type": "module",
9
- "main": "./lib/index.cjs",
10
- "module": "./lib/index.js",
9
+ "main": "./lib/index.js",
11
10
  "types": "./lib/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "import": {
15
- "types": "./lib/index.d.ts",
16
- "default": "./lib/index.js"
17
- },
18
- "require": {
19
- "types": "./lib/index.d.cts",
20
- "default": "./lib/index.cjs"
21
- }
22
- }
23
- },
11
+ "exports": "./lib/index.js",
24
12
  "scripts": {
25
13
  "build": "tsup",
26
14
  "watch": "yarn build --watch",
27
15
  "test": "vitest"
28
16
  },
29
17
  "dependencies": {
30
- "@chriscdn/promise-semaphore": "^3.1.3",
18
+ "@chriscdn/promise-semaphore": "^4.0.0",
31
19
  "quick-lru": "^7.3.0"
32
20
  },
33
21
  "devDependencies": {
package/lib/index.cjs DELETED
@@ -1,99 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- Memoize: () => Memoize,
34
- MemoizeAsync: () => MemoizeAsync
35
- });
36
- module.exports = __toCommonJS(index_exports);
37
- var import_promise_semaphore = require("@chriscdn/promise-semaphore");
38
- var import_quick_lru = __toESM(require("quick-lru"), 1);
39
- var kDefaultMaxSize = 1e3;
40
- var Memoize = (cb, options = {}) => {
41
- const maxAge = options.maxAge;
42
- const maxSize = options.maxSize ?? kDefaultMaxSize;
43
- const shouldCache = options.shouldCache ?? (() => true);
44
- const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
45
- const cache = new import_quick_lru.default({
46
- maxAge,
47
- maxSize
48
- });
49
- const memoizedFunction = (...args) => {
50
- const key = resolver(...args);
51
- if (cache.has(key)) {
52
- return cache.get(key);
53
- } else {
54
- const returnValue = cb(...args);
55
- if (shouldCache(returnValue, key)) {
56
- cache.set(key, returnValue);
57
- }
58
- return returnValue;
59
- }
60
- };
61
- memoizedFunction.cache = cache;
62
- return memoizedFunction;
63
- };
64
- var MemoizeAsync = (cb, options = {}) => {
65
- const maxAge = options.maxAge;
66
- const maxSize = options.maxSize ?? kDefaultMaxSize;
67
- const shouldCache = options.shouldCache ?? (() => true);
68
- const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
69
- const cache = new import_quick_lru.default({
70
- maxAge,
71
- maxSize
72
- });
73
- const semaphore = new import_promise_semaphore.Semaphore();
74
- const memoizedFunction = async (...args) => {
75
- const key = resolver(...args);
76
- try {
77
- await semaphore.acquire(key);
78
- if (cache.has(key)) {
79
- return cache.get(key);
80
- } else {
81
- const returnValue = await cb(...args);
82
- if (shouldCache(returnValue, key)) {
83
- cache.set(key, returnValue);
84
- }
85
- return returnValue;
86
- }
87
- } finally {
88
- semaphore.release(key);
89
- }
90
- };
91
- memoizedFunction.cache = cache;
92
- return memoizedFunction;
93
- };
94
- // Annotate the CommonJS export names for ESM import in node:
95
- 0 && (module.exports = {
96
- Memoize,
97
- MemoizeAsync
98
- });
99
- //# sourceMappingURL=index.cjs.map
package/lib/index.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,+BAA0B;AAC1B,uBAAqB;AAErB,IAAM,kBAAkB;AAYxB,IAAM,UAAU,CACd,IACA,UAA0C,CAAC,MACxC;AACH,QAAM,SAA6B,QAAQ;AAC3C,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,cAAc,QAAQ,gBAAgB,MAAM;AAElD,QAAM,WAAW,QAAQ,aACtB,IAAI,SAAe,KAAK,UAAU,IAAI;AAEzC,QAAM,QAAQ,IAAI,iBAAAA,QAAyB;AAAA,IACzC;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,mBAAmB,IAAI,SAAuB;AAClD,UAAM,MAAM,SAAS,GAAG,IAAI;AAE5B,QAAI,MAAM,IAAI,GAAG,GAAG;AAClB,aAAO,MAAM,IAAI,GAAG;AAAA,IACtB,OAAO;AACL,YAAM,cAAc,GAAG,GAAG,IAAI;AAC9B,UAAI,YAAY,aAAa,GAAG,GAAG;AACjC,cAAM,IAAI,KAAK,WAAW;AAAA,MAC5B;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAEA,mBAAiB,QAAQ;AAEzB,SAAO;AACT;AAKA,IAAM,eAAe,CACnB,IACA,UAA0C,CAAC,MACxC;AACH,QAAM,SAA6B,QAAQ;AAC3C,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,cAAc,QAAQ,gBAAgB,MAAM;AAElD,QAAM,WAAW,QAAQ,aACtB,IAAI,SAAe,KAAK,UAAU,IAAI;AAEzC,QAAM,QAAQ,IAAI,iBAAAA,QAAyB;AAAA,IACzC;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,YAAY,IAAI,mCAAU;AAEhC,QAAM,mBAAmB,UAAU,SAAgC;AACjE,UAAM,MAAM,SAAS,GAAG,IAAI;AAE5B,QAAI;AACF,YAAM,UAAU,QAAQ,GAAG;AAE3B,UAAI,MAAM,IAAI,GAAG,GAAG;AAClB,eAAO,MAAM,IAAI,GAAG;AAAA,MACtB,OAAO;AACL,cAAM,cAAc,MAAM,GAAG,GAAG,IAAI;AACpC,YAAI,YAAY,aAAa,GAAG,GAAG;AACjC,gBAAM,IAAI,KAAK,WAAW;AAAA,QAC5B;AACA,eAAO;AAAA,MACT;AAAA,IACF,UAAE;AACA,gBAAU,QAAQ,GAAG;AAAA,IACvB;AAAA,EACF;AAEA,mBAAiB,QAAQ;AAEzB,SAAO;AACT;","names":["QuickLRU"]}
package/lib/index.d.cts DELETED
@@ -1,24 +0,0 @@
1
- import QuickLRU from 'quick-lru';
2
-
3
- type Options<T extends any[], Return> = {
4
- maxSize: number;
5
- maxAge?: number;
6
- shouldCache: (returnValue: Return, key: string) => boolean;
7
- resolver: (...args: T) => string;
8
- };
9
- /**
10
- * Memoize a synchronous function.
11
- */
12
- declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => Return, options?: Partial<Options<Args, Return>>) => {
13
- (...args: Args): Return;
14
- cache: QuickLRU<string, Return>;
15
- };
16
- /**
17
- * Memoize an asynchronous function.
18
- */
19
- declare const MemoizeAsync: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<Options<Args, Return>>) => {
20
- (...args: Args): Promise<Return>;
21
- cache: QuickLRU<string, Return>;
22
- };
23
-
24
- export { Memoize, MemoizeAsync };