@checkdigit/memoize 1.0.0-PR.2-665a
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/LICENSE.txt +22 -0
- package/README.md +23 -0
- package/SECURITY.md +13 -0
- package/dist-cjs/index.cjs +37 -0
- package/dist-cjs/memoize.cjs +41 -0
- package/dist-mjs/index.mjs +6 -0
- package/dist-mjs/memoize.mjs +20 -0
- package/dist-types/index.d.ts +1 -0
- package/dist-types/memoize.d.ts +14 -0
- package/package.json +1 -0
- package/src/index.ts +9 -0
- package/src/memoize.ts +38 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
The MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2021-2023 Check Digit, LLC
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Memoize
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-2023 [Check Digit, LLC](https://checkdigit.com)
|
|
4
|
+
|
|
5
|
+
Memoize is a light-weight utility library used by Check Digit services for memoization purposes.
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
$ npm install @checkdigit/memoize
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Use
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
import { memoize } from '@checkdigit/memoize';
|
|
17
|
+
|
|
18
|
+
const value = memoize((_param: _paramType) => _asyncFunction(_paramValue));
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
MIT
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
These versions of @checkdigit/memoize are currently being supported with security updates.
|
|
6
|
+
|
|
7
|
+
| Version | Supported |
|
|
8
|
+
| ------- | ------------------ |
|
|
9
|
+
| > 1.x | :white_check_mark: |
|
|
10
|
+
|
|
11
|
+
## Reporting a Vulnerability
|
|
12
|
+
|
|
13
|
+
Please create an issue at https://github.com/checkdigit/memoize/issues
|
|
@@ -0,0 +1,37 @@
|
|
|
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 src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
default: () => import_memoize.default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_memoize = __toESM(require("./memoize.cjs"));
|
|
37
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIjs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFBQTtBQVFBLHFCQUF3QjsiLAogICJuYW1lcyI6IFtdCn0K
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/memoize.ts
|
|
21
|
+
var memoize_exports = {};
|
|
22
|
+
__export(memoize_exports, {
|
|
23
|
+
default: () => memoize_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(memoize_exports);
|
|
26
|
+
var memoize_default = (function_) => {
|
|
27
|
+
const cache = /* @__PURE__ */ new Map();
|
|
28
|
+
return (...arguments_) => {
|
|
29
|
+
const key = JSON.stringify(arguments_);
|
|
30
|
+
let value = cache.get(key);
|
|
31
|
+
if (value === void 0) {
|
|
32
|
+
value = function_(...arguments_);
|
|
33
|
+
cache.set(key, value);
|
|
34
|
+
value.catch(() => {
|
|
35
|
+
cache.delete(key);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL21lbW9pemUudHMiXSwKICAibWFwcGluZ3MiOiAiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFvQkEsSUFBTyxrQkFBUSxDQUNiLGNBQ29EO0FBQ3BELFFBQU0sUUFBUSxvQkFBSSxJQUE2QjtBQUMvQyxTQUFPLElBQUksZUFBZTtBQUN4QixVQUFNLE1BQU0sS0FBSyxVQUFVLFVBQVU7QUFDckMsUUFBSSxRQUFRLE1BQU0sSUFBSSxHQUFHO0FBQ3pCLFFBQUksVUFBVSxRQUFXO0FBQ3ZCLGNBQVEsVUFBVSxHQUFHLFVBQVU7QUFDL0IsWUFBTSxJQUFJLEtBQUssS0FBSztBQUNwQixZQUFNLE1BQU0sTUFBTTtBQUVoQixjQUFNLE9BQU8sR0FBRztBQUFBLE1BQ2xCLENBQUM7QUFBQSxJQUNIO0FBQ0EsV0FBTztBQUFBLEVBQ1Q7QUFDRjsiLAogICJuYW1lcyI6IFtdCn0K
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { default as default2 } from "./memoize.mjs";
|
|
3
|
+
export {
|
|
4
|
+
default2 as default
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVFBLFNBQVMsV0FBQUEsZ0JBQWU7IiwKICAibmFtZXMiOiBbImRlZmF1bHQiXQp9Cg==
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/memoize.ts
|
|
2
|
+
var memoize_default = (function_) => {
|
|
3
|
+
const cache = /* @__PURE__ */ new Map();
|
|
4
|
+
return (...arguments_) => {
|
|
5
|
+
const key = JSON.stringify(arguments_);
|
|
6
|
+
let value = cache.get(key);
|
|
7
|
+
if (value === void 0) {
|
|
8
|
+
value = function_(...arguments_);
|
|
9
|
+
cache.set(key, value);
|
|
10
|
+
value.catch(() => {
|
|
11
|
+
cache.delete(key);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
memoize_default as default
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL21lbW9pemUudHMiXSwKICAibWFwcGluZ3MiOiAiO0FBb0JBLElBQU8sa0JBQVEsQ0FDYixjQUNvRDtBQUNwRCxRQUFNLFFBQVEsb0JBQUksSUFBNkI7QUFDL0MsU0FBTyxJQUFJLGVBQWU7QUFDeEIsVUFBTSxNQUFNLEtBQUssVUFBVSxVQUFVO0FBQ3JDLFFBQUksUUFBUSxNQUFNLElBQUksR0FBRztBQUN6QixRQUFJLFVBQVUsUUFBVztBQUN2QixjQUFRLFVBQVUsR0FBRyxVQUFVO0FBQy9CLFlBQU0sSUFBSSxLQUFLLEtBQUs7QUFDcEIsWUFBTSxNQUFNLE1BQU07QUFFaEIsY0FBTSxPQUFPLEdBQUc7QUFBQSxNQUNsQixDQUFDO0FBQUEsSUFDSDtBQUNBLFdBQU87QUFBQSxFQUNUO0FBQ0Y7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './memoize';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { URL } from 'node:url';
|
|
3
|
+
export type Argument = string | number | boolean | null | Date | URL | Argument[] | {
|
|
4
|
+
[key: string]: Argument;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Memoize an async function. The promise is cached, not the value. This guarantees the underlying function is
|
|
8
|
+
* called at most once for a given set of arguments, unless the promise is rejected.
|
|
9
|
+
* If the promise is rejected, it will be removed from the memoize cache. This allows the function to be retried.
|
|
10
|
+
*
|
|
11
|
+
* @param function_ function to memoize
|
|
12
|
+
*/
|
|
13
|
+
declare const _default: <Return, Arguments extends Argument[]>(function_: (...arguments_: Arguments) => Promise<Return>) => (...arguments_: Arguments) => Promise<Return>;
|
|
14
|
+
export default _default;
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@checkdigit/memoize","version":"1.0.0-PR.2-665a","description":"Memoize is a light-weight utility library used by Check Digit services for memoization purposes","exports":{".":{"types":"./dist-types/index.d.ts","require":"./dist-cjs/index.cjs","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-cjs","dist-mjs","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-cjs/**/*.test.cjs","!dist-cjs/**/*.spec.cjs","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"prettier":"@checkdigit/prettier-config","engines":{"node":">=16"},"repository":{"type":"git","url":"git+https://github.com/checkdigit/memoize.git"},"author":"Check Digit, LLC","license":"MIT","bugs":{"url":"https://github.com/checkdigit/memoize/issues"},"homepage":"https://github.com/checkdigit/memoize#readme","devDependencies":{"@checkdigit/eslint-config":"^7.8.0","@checkdigit/jest-config":"^2.3.0","@checkdigit/prettier-config":"^3.4.0","@checkdigit/typescript-config":"^3.5.0","@types/uuid":"^9.0.2","rimraf":"^5.0.1","uuid":"^9.0.0"},"eslintConfig":{"extends":["@checkdigit/eslint-config"]},"jest":{"preset":"@checkdigit/jest-config"},"scripts":{"prepublishOnly":"npm run build:dist-types && npm run build:dist-cjs && npm run build:dist-esm","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","build:dist-cjs":"rimraf dist-cjs && npx builder --type=commonjs --sourceMap --outDir=dist-cjs","build:dist-esm":"rimraf dist-esm && npx builder --type=module --sourceMap --outDir=dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","lint:fix":"eslint -f unix --ext .ts,.mts src --fix","lint":"eslint -f unix --ext .ts,.mts src","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style","ci:compile":"tsc --noEmit","ci:test":"jest --coverage=false","ci:coverage":"jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier"}}
|
package/src/index.ts
ADDED
package/src/memoize.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// memoize.ts
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2021-2023 Check Digit, LLC
|
|
5
|
+
*
|
|
6
|
+
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { URL } from 'node:url';
|
|
10
|
+
|
|
11
|
+
// arguments must be stringify-able
|
|
12
|
+
export type Argument = string | number | boolean | null | Date | URL | Argument[] | { [key: string]: Argument };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Memoize an async function. The promise is cached, not the value. This guarantees the underlying function is
|
|
16
|
+
* called at most once for a given set of arguments, unless the promise is rejected.
|
|
17
|
+
* If the promise is rejected, it will be removed from the memoize cache. This allows the function to be retried.
|
|
18
|
+
*
|
|
19
|
+
* @param function_ function to memoize
|
|
20
|
+
*/
|
|
21
|
+
export default <Return, Arguments extends Argument[]>(
|
|
22
|
+
function_: (...arguments_: Arguments) => Promise<Return>
|
|
23
|
+
): ((...arguments_: Arguments) => Promise<Return>) => {
|
|
24
|
+
const cache = new Map<string, Promise<Return>>();
|
|
25
|
+
return (...arguments_) => {
|
|
26
|
+
const key = JSON.stringify(arguments_);
|
|
27
|
+
let value = cache.get(key);
|
|
28
|
+
if (value === undefined) {
|
|
29
|
+
value = function_(...arguments_);
|
|
30
|
+
cache.set(key, value);
|
|
31
|
+
value.catch(() => {
|
|
32
|
+
// on a reject, evict key from cache
|
|
33
|
+
cache.delete(key);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
};
|