Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit 085b3c9

Browse files
authored
feat: add memoize and memoized API (#53)
1 parent 64ee15e commit 085b3c9

File tree

6 files changed

+146
-6
lines changed

6 files changed

+146
-6
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ const result = (await RC.write(void 0, writeOpts)).unwrap();
6161
assert.strictEqual(result, void 0);
6262
```
6363

64+
memoize/memoized:
65+
66+
```ts
67+
import * as RC from "@nodesecure/rc";
68+
import assert from "node:assert";
69+
70+
const configurationPayload = (
71+
await RC.read(void 0, { createMode: "ci" })
72+
).unwrap()
73+
74+
RC.memoize(configurationPayload, { overwrite: true });
75+
76+
const memoizedPayload = RC.memoized();
77+
assert.deepEqual(configurationPayload, memoizedPayload);
78+
```
79+
6480
> 👀 .read and .write return Rust like [Result](https://doc.rust-lang.org/std/result/) object. Under the hood we use [ts-results](https://github.com/vultix/ts-results) to achieve this.
6581
6682
## API
@@ -125,6 +141,27 @@ export interface writePartialPayload {
125141

126142
export type writeOptions = writeCompletePayload | writePartialPayload;
127143
```
144+
### memoize(payload: Partial<RC>, options: IMemoizeOptions = {}): void
145+
By default, the memory API overwrites the previous stored payload. When the `OVERWRITE` option is `false`, it merges new properties with existing properties.
146+
147+
```ts
148+
export interface memoizeOptions {
149+
/** * @default true */
150+
overwrite?: boolean;
151+
}
152+
```
153+
The `overwrite` option is used to specify whether data should be overwritten or merged.
154+
155+
### memoized(options: IMemoizedOptions): Partial<RC> | null
156+
This method returns null, when the default value is null, otherwise, it returns the current value of `memoizedValue`.
157+
158+
```ts
159+
export interface memoizedOptions {
160+
/** * @default null */
161+
defaultValue: Partial<RC> | null;
162+
}
163+
```
164+
If the `defaultValue` property is at null, then this value will be returned when `memoized` is called.
128165

129166
### homedir(): string
130167

package-lock.json

+3-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"c8": "^7.12.0",
4646
"chai": "^4.3.7",
4747
"eslint": "^8.32.0",
48-
"lodash.merge": "^4.6.2",
4948
"mocha": "^10.2.0",
5049
"tape": "^5.6.3",
5150
"ts-node": "^10.9.1",
@@ -57,6 +56,7 @@
5756
"@nodesecure/js-x-ray": "^6.0.1",
5857
"@nodesecure/vuln": "^1.7.0",
5958
"@slimio/config": "^1.2.0",
59+
"lodash.merge": "^4.6.2",
6060
"ts-results": "^3.3.0",
6161
"tslib": "^2.4.1",
6262
"type-fest": "^3.5.3"

src/functions/memoize.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Import Third-party Dependencies
2+
import merge from "lodash.merge";
3+
4+
// Import Internal Dependencies
5+
import { RC } from "../rc.js";
6+
7+
let memoizedValue: Partial<RC> | null = null;
8+
9+
export interface memoizeOptions {
10+
/** * @default true */
11+
overwrite?: boolean;
12+
}
13+
14+
export interface memoizedOptions {
15+
/** * @default null */
16+
defaultValue?: Partial<RC>;
17+
}
18+
19+
export function memoize(payload: Partial<RC>, options: memoizeOptions = {}): void {
20+
const { overwrite = true } = options;
21+
if (memoizedValue === null || overwrite) {
22+
memoizedValue = payload;
23+
}
24+
else {
25+
memoizedValue = merge({}, memoizedValue, payload);
26+
}
27+
}
28+
29+
export function memoized(options: memoizedOptions = {}): Partial<RC> | null {
30+
const { defaultValue = null } = options;
31+
32+
return memoizedValue ?? defaultValue;
33+
}
34+
35+
export function clearMemoized(): void {
36+
memoizedValue = null;
37+
}
38+

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./functions/read.js";
22
export * from "./functions/write.js";
3+
export * from "./functions/memoize.js";
34
export * as CONSTANTS from "./constants.js";
45

56
export { RC, JSONSchema, homedir } from "./rc.js";

test/memoize.spec.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Import Third-party Dependencies
2+
import { expect } from "chai";
3+
4+
// Import Internal Dependencies
5+
import { generateDefaultRC, RC } from "../src/rc.js";
6+
import { memoize, memoized, clearMemoized } from "../src/index.js";
7+
8+
let memoizedPayload: Partial<RC> | null = null;
9+
10+
describe("memoize()", () => {
11+
beforeEach(() => {
12+
memoizedPayload = null;
13+
});
14+
15+
it("should store the payload in memory", () => {
16+
const payload = generateDefaultRC();
17+
memoize(payload, { overwrite: false });
18+
19+
memoizedPayload = memoized();
20+
21+
expect(memoizedPayload).to.deep.equal(payload);
22+
});
23+
24+
it("should overwrite the previous payload if the overwrite option is true", () => {
25+
const payload = { version: "2.0.0", i18n: "french", strategy: "yarn" } as any;
26+
memoize(payload, { overwrite: true });
27+
memoizedPayload = memoized();
28+
29+
expect(memoizedPayload).to.deep.equal(payload);
30+
});
31+
32+
it("should merge with the previous memoized payload if overwrite option is set to false", () => {
33+
const rc = generateDefaultRC();
34+
memoize(rc, { overwrite: true });
35+
36+
const payload = { version: "2.0.0", i18n: "french", strategy: "yarn" } as any;
37+
memoize(payload, { overwrite: false });
38+
memoizedPayload = memoized();
39+
40+
expect(memoizedPayload).to.deep.equal({ ...rc, ...payload });
41+
});
42+
});
43+
44+
45+
describe("memoized", () => {
46+
beforeEach(() => {
47+
clearMemoized();
48+
memoizedPayload = null;
49+
});
50+
51+
52+
it("must return the default value (null)", () => {
53+
const result = memoized();
54+
55+
expect(result).to.deep.equal(memoizedPayload);
56+
});
57+
58+
it("should return previously remembered configuration", () => {
59+
const rc = generateDefaultRC();
60+
61+
memoize(rc, { overwrite: true });
62+
memoizedPayload = memoized();
63+
64+
expect(memoizedPayload).to.deep.equal(rc);
65+
});
66+
});

0 commit comments

Comments
 (0)