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

Commit

Permalink
refactor(read.ts): Add a memoize option
Browse files Browse the repository at this point in the history
  • Loading branch information
fabnguess committed Mar 18, 2023
1 parent aef6c34 commit e1b331d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
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"));
});
});

0 comments on commit e1b331d

Please sign in to comment.