@thi.ng/memoize 3.2.0 → 3.3.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-03-27T09:53:45Z
3
+ - **Last updated**: 2024-04-08T14:59:29Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,19 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [3.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.3.0) (2024-04-08)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add delay() wrapper ([d8f4733](https://github.com/thi-ng/umbrella/commit/d8f4733))
17
+ - migrated from [@thi.ng/compose](https://github.com/thi-ng/umbrella/tree/main/packages/compose) since conceptually better at home here
18
+ - add docs
19
+
20
+ #### ♻️ Refactoring
21
+
22
+ - rename `defonce()` => `defOnce()` ([08e876f](https://github.com/thi-ng/umbrella/commit/08e876f))
23
+ - deprecate old spelling
24
+
12
25
  ## [3.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.2.0) (2024-03-27)
13
26
 
14
27
  #### 🚀 Features
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 190 standalone projects, maintained as part
10
+ > This is one of 191 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -15,6 +15,7 @@
15
15
  > GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
16
16
 
17
17
  - [About](#about)
18
+ - [Available memoization functions](#available-memoization-functions)
18
19
  - [Status](#status)
19
20
  - [Related packages](#related-packages)
20
21
  - [Installation](#installation)
@@ -32,7 +33,7 @@
32
33
  Function memoization with configurable caching.
33
34
 
34
35
  This package provides different function memoization implementations for
35
- functions with 1 or more arguments and custom result caching using ES6
36
+ functions with arbitrary arguments and custom result caching using ES6
36
37
  Map API like implementations. Unlike native ES6 Maps, **the
37
38
  implementations MUST support value, not just referential, equality
38
39
  semantics** (e.g. those provided by
@@ -42,6 +43,16 @@ or
42
43
  The latter also support automatically pruning of memoization caches,
43
44
  based on different strategies. See doc strings for further details.
44
45
 
46
+ ### Available memoization functions
47
+
48
+ - [defOnce()](https://docs.thi.ng/umbrella/memoize/functions/defOnce.html)
49
+ - [delay()](https://docs.thi.ng/umbrella/memoize/functions/delay.html)
50
+ - [doOnce()](https://docs.thi.ng/umbrella/memoize/functions/doOnce.html)
51
+ - [memoize()](https://docs.thi.ng/umbrella/memoize/functions/memoize.html)
52
+ - [memoize1()](https://docs.thi.ng/umbrella/memoize/functions/memoize1.html)
53
+ - [memoizeJ()](https://docs.thi.ng/umbrella/memoize/functions/memoizeJ.html)
54
+ - [memoizeO()](https://docs.thi.ng/umbrella/memoize/functions/memoizeO.html)
55
+
45
56
  ## Status
46
57
 
47
58
  **STABLE** - used in production
@@ -58,7 +69,13 @@ based on different strategies. See doc strings for further details.
58
69
  yarn add @thi.ng/memoize
59
70
  ```
60
71
 
61
- ES module import:
72
+ ESM import:
73
+
74
+ ```ts
75
+ import * as mem from "@thi.ng/memoize";
76
+ ```
77
+
78
+ Browser ESM import:
62
79
 
63
80
  ```html
64
81
  <script type="module" src="https://cdn.skypack.dev/@thi.ng/memoize"></script>
@@ -69,10 +86,10 @@ ES module import:
69
86
  For Node.js REPL:
70
87
 
71
88
  ```js
72
- const memoize = await import("@thi.ng/memoize");
89
+ const mem = await import("@thi.ng/memoize");
73
90
  ```
74
91
 
75
- Package sizes (brotli'd, pre-treeshake): ESM: 334 bytes
92
+ Package sizes (brotli'd, pre-treeshake): ESM: 518 bytes
76
93
 
77
94
  ## Dependencies
78
95
 
package/defonce.d.ts CHANGED
@@ -17,5 +17,7 @@ import type { Fn0 } from "@thi.ng/api";
17
17
  * @param id -
18
18
  * @param factory -
19
19
  */
20
+ export declare const defOnce: <T>(id: string, factory: Fn0<T>) => T;
21
+ /** @deprecated renamed to {@link defOnce} */
20
22
  export declare const defonce: <T>(id: string, factory: Fn0<T>) => T;
21
23
  //# sourceMappingURL=defonce.d.ts.map
package/defonce.js CHANGED
@@ -1,5 +1,7 @@
1
1
  const cache = /* @__PURE__ */ Object.create(null);
2
- const defonce = (id, factory) => id in cache ? cache[id] : cache[id] = factory();
2
+ const defOnce = (id, factory) => id in cache ? cache[id] : cache[id] = factory();
3
+ const defonce = defOnce;
3
4
  export {
5
+ defOnce,
4
6
  defonce
5
7
  };
package/delay.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { Fn0, IDeref } from "@thi.ng/api";
2
+ /**
3
+ * Syntax sugar for {@link Delay} ctor. Wraps given no-arg function in a
4
+ * {@link Delay} value/memoization wrapper and returns it. The function result
5
+ * can later be obtained via `.deref()`. The function will only be called the
6
+ * first time `.deref()` is used and the result will be cached. Future deref's
7
+ * will then only return the cached value.
8
+ *
9
+ * @remarks
10
+ * Use {@link Delay#isRealized} to check if the function result is already
11
+ * available (i.e. if the function has already been called).
12
+ *
13
+ * @example
14
+ * ```ts tangle:../export/delay.ts
15
+ * import { delay } from "@thi.ng/memoize";
16
+ *
17
+ * const a = delay(() => {
18
+ * console.log("calculating answer...");
19
+ * return 42;
20
+ * });
21
+ *
22
+ * // the function will only be called now (and once)
23
+ * console.log("first:", a.deref());
24
+ * // calculating answer...
25
+ * // first: 42
26
+ *
27
+ * // now only returns cached result
28
+ * console.log("second:", a.deref());
29
+ * // second: 42
30
+ * ```
31
+ *
32
+ * @param body
33
+ */
34
+ export declare const delay: <T>(body: Fn0<T>) => Delay<T>;
35
+ export declare class Delay<T> implements IDeref<T> {
36
+ protected value: T;
37
+ protected body: Fn0<T>;
38
+ protected realized: boolean;
39
+ constructor(body: Fn0<T>);
40
+ deref(): T;
41
+ isRealized(): boolean;
42
+ }
43
+ //# sourceMappingURL=delay.d.ts.map
package/delay.js ADDED
@@ -0,0 +1,24 @@
1
+ const delay = (body) => new Delay(body);
2
+ class Delay {
3
+ value;
4
+ body;
5
+ realized;
6
+ constructor(body) {
7
+ this.body = body;
8
+ this.realized = false;
9
+ }
10
+ deref() {
11
+ if (!this.realized) {
12
+ this.value = this.body();
13
+ this.realized = true;
14
+ }
15
+ return this.value;
16
+ }
17
+ isRealized() {
18
+ return this.realized;
19
+ }
20
+ }
21
+ export {
22
+ Delay,
23
+ delay
24
+ };
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./defonce.js";
3
+ export * from "./delay.js";
3
4
  export * from "./do-once.js";
4
5
  export * from "./memoize.js";
5
6
  export * from "./memoize1.js";
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./defonce.js";
3
+ export * from "./delay.js";
3
4
  export * from "./do-once.js";
4
5
  export * from "./memoize.js";
5
6
  export * from "./memoize1.js";
package/memoizeo.d.ts CHANGED
@@ -8,7 +8,7 @@ import type { Fn, Fn2, Fn3, Fn4, NumOrString } from "@thi.ng/api";
8
8
  * Also see {@link memoize1}, {@link memoizeJ}, {@link memoize}.
9
9
  *
10
10
  * @example
11
- * ```ts tangle:../../export/memoizeo.ts
11
+ * ```ts tangle:../export/memoizeo.ts
12
12
  * import { memoizeO } from "@thi.ng/memoize";
13
13
  *
14
14
  * const test = memoizeO((x: number) => (console.log("exec", x), x * 10));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/memoize",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "Function memoization with configurable caching",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,7 +36,7 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.9.31"
39
+ "@thi.ng/api": "^8.10.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@microsoft/api-extractor": "^7.43.0",
@@ -71,6 +71,9 @@
71
71
  "./defonce": {
72
72
  "default": "./defonce.js"
73
73
  },
74
+ "./delay": {
75
+ "default": "./delay.js"
76
+ },
74
77
  "./do-once": {
75
78
  "default": "./do-once.js"
76
79
  },
@@ -93,5 +96,5 @@
93
96
  ],
94
97
  "year": 2018
95
98
  },
96
- "gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n"
99
+ "gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
97
100
  }