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

Memoize payload API #53

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ const result = (await RC.write(void 0, writeOpts)).unwrap();
assert.strictEqual(result, void 0);
```

memoize/memoized:

```ts
import * as RC from "@nodesecure/rc";
import assert from "node:assert";

const configurationPayload = (
await RC.read(void 0, { createMode: "ci" })
).unwrap()

RC.memoize(configurationPayload, { overwrite: true });

const memoizedPayload = RC.memoized();
assert.deepEqual(configurationPayload, memoizedPayload);
```

> 👀 .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.
## API
Expand Down Expand Up @@ -125,6 +141,27 @@ export interface writePartialPayload {

export type writeOptions = writeCompletePayload | writePartialPayload;
```
### memoize(payload: Partial<RC>, options: IMemoizeOptions = {}): void
By default, the memory API overwrites the previous stored payload. When the `OVERWRITE` option is `false`, it merges new properties with existing properties.

```ts
export interface memoizeOptions {
/** * @default true */
overwrite?: boolean;
}
```
The `overwrite` option is used to specify whether data should be overwritten or merged.

### memoized(options: IMemoizedOptions): Partial<RC> | null
This method returns null, when the default value is null, otherwise, it returns the current value of `memoizedValue`.

```ts
export interface memoizedOptions {
/** * @default null */
defaultValue: Partial<RC> | null;
}
```
If the `defaultValue` property is at null, then this value will be returned when `memoized` is called.

### homedir(): string

Expand Down
8 changes: 3 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"c8": "^7.12.0",
"chai": "^4.3.7",
"eslint": "^8.32.0",
"lodash.merge": "^4.6.2",
"mocha": "^10.2.0",
"tape": "^5.6.3",
"ts-node": "^10.9.1",
Expand All @@ -57,6 +56,7 @@
"@nodesecure/js-x-ray": "^6.0.1",
"@nodesecure/vuln": "^1.7.0",
"@slimio/config": "^1.2.0",
"lodash.merge": "^4.6.2",
"ts-results": "^3.3.0",
"tslib": "^2.4.1",
"type-fest": "^3.5.3"
Expand Down
38 changes: 38 additions & 0 deletions src/functions/memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Import Third-party Dependencies
import merge from "lodash.merge";

// Import Internal Dependencies
import { RC } from "../rc.js";

let memoizedValue: Partial<RC> | null = null;

export interface memoizeOptions {
/** * @default true */
overwrite?: boolean;
}

export interface memoizedOptions {
/** * @default null */
defaultValue?: Partial<RC>;
}

export function memoize(payload: Partial<RC>, options: memoizeOptions = {}): void {
const { overwrite = true } = options;
if (memoizedValue === null || overwrite) {
memoizedValue = payload;
}
else {
memoizedValue = merge({}, memoizedValue, payload);
}
}

export function memoized(options: memoizedOptions = {}): Partial<RC> | null {
const { defaultValue = null } = options;

return memoizedValue ?? defaultValue;
}

export function clearMemoized(): void {
memoizedValue = null;
}

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./functions/read.js";
export * from "./functions/write.js";
export * from "./functions/memoize.js";
export * as CONSTANTS from "./constants.js";

export { RC, JSONSchema, homedir } from "./rc.js";
66 changes: 66 additions & 0 deletions test/memoize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Import Third-party Dependencies
import { expect } from "chai";

// Import Internal Dependencies
import { generateDefaultRC, RC } from "../src/rc.js";
import { memoize, memoized, clearMemoized } from "../src/index.js";

let memoizedPayload: Partial<RC> | null = null;

describe("memoize()", () => {
beforeEach(() => {
memoizedPayload = null;
});

it("should store the payload in memory", () => {
const payload = generateDefaultRC();
memoize(payload, { overwrite: false });

memoizedPayload = memoized();

expect(memoizedPayload).to.deep.equal(payload);
});

it("should overwrite the previous payload if the overwrite option is true", () => {
const payload = { version: "2.0.0", i18n: "french", strategy: "yarn" } as any;
memoize(payload, { overwrite: true });
memoizedPayload = memoized();

expect(memoizedPayload).to.deep.equal(payload);
});

it("should merge with the previous memoized payload if overwrite option is set to false", () => {
const rc = generateDefaultRC();
memoize(rc, { overwrite: true });

const payload = { version: "2.0.0", i18n: "french", strategy: "yarn" } as any;
memoize(payload, { overwrite: false });
memoizedPayload = memoized();

expect(memoizedPayload).to.deep.equal({ ...rc, ...payload });
});
});


describe("memoized", () => {
beforeEach(() => {
clearMemoized();
memoizedPayload = null;
});


it("must return the default value (null)", () => {
const result = memoized();

expect(result).to.deep.equal(memoizedPayload);
});

it("should return previously remembered configuration", () => {
const rc = generateDefaultRC();

memoize(rc, { overwrite: true });
memoizedPayload = memoized();

expect(memoizedPayload).to.deep.equal(rc);
});
});