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

Add memoize option to read API #74

Merged
merged 1 commit into from
Mar 20, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ interface createReadOptions {
* @default `minimal`
*/
createMode?: RCGenerationMode | RCGenerationMode[];
/**
* RC automatic caching option. This option allows to cache a configuration passed in parameter.
*
* @default false
*/
memoize?: boolean;
}

export type readOptions = RequireAtLeastOne<
Expand Down
14 changes: 13 additions & 1 deletion src/functions/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RequireAtLeastOne } from "type-fest";
// Import Internal Dependencies
import { RC, JSONSchema, generateDefaultRC, RCGenerationMode } from "../rc.js";
import * as CONSTANTS from "../constants.js";
import { memoize } from "./memoize.js";

// CONSTANTS
const { Ok, Err } = TR;
Expand All @@ -27,6 +28,13 @@ interface createReadOptions {
* @default `minimal`
*/
createMode?: RCGenerationMode | RCGenerationMode[];

/**
* RC automatic caching option. This option allows to cache a configuration passed in parameter.
*
* @default false
*/
memoize?: boolean;
}

export type readOptions = RequireAtLeastOne<createReadOptions, "createIfDoesNotExist" | "createMode">;
Expand All @@ -36,7 +44,7 @@ export async function read(
options: readOptions = Object.create(null)
): Promise<Result<RC, NodeJS.ErrnoException>> {
try {
const { createIfDoesNotExist = Boolean(options.createMode), createMode } = options;
const { createIfDoesNotExist = Boolean(options.createMode), createMode, memoize: memoizeRc = false } = options;

const cfgPath = path.join(location, CONSTANTS.CONFIGURATION_NAME);
const cfg = new Config<RC>(cfgPath, {
Expand All @@ -50,6 +58,10 @@ export async function read(
}
const result = cfg.payload;

if (memoizeRc) {
memoize(result);
}

await cfg.close();

return new Ok(result);
Expand Down
37 changes: 36 additions & 1 deletion test/read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fileURLToPath } from "node:url";
import { expect } from "chai";

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

// CONSTANTS
Expand Down Expand Up @@ -66,3 +66,38 @@ describe("read .nodesecurerc", () => {
});
});
});

describe("read | memoize option", () => {
const location = path.join(os.tmpdir(), "rcread");

before(async() => {
await fs.mkdir(location);
});

beforeEach(async() => {
clearMemoized();
await fs.rm(path.join(location, CONSTANTS.CONFIGURATION_NAME), { force: true });
});

after(async() => {
await fs.rm(location, { force: true, recursive: true });
});

it("should return the default value 'null' when the memoize option is not declared ", async() => {
await read(location, { createIfDoesNotExist: true });
expect(memoized()).to.eq(null);
});

it("should return the configuration passed in parameter", async() => {
await read(location, { createIfDoesNotExist: true, memoize: true });
expect(memoized()).to.deep.equal(generateDefaultRC());
});

it("must overwrite the previously stored payload", async() => {
await read(location, { createIfDoesNotExist: true, memoize: true });

memoize(generateDefaultRC("report"));

expect(memoized()).to.deep.equal(generateDefaultRC("report"));
});
});