Skip to content

Commit a47a91b

Browse files
committed
feat: add husky pre-commit hook and enhance README with ESM note; update test cases for no-argument functions; more robust key generation for helpers
1 parent 311bbf0 commit a47a91b

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

Diff for: .husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pnpm check && pnpm test

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
`node-cache-plus` is a wrapper around the popular library [`node-cache`](https://github.com/node-cache/node-cache/tree/master) with additional features such as tag-based invalidation, factory functions, and other helpers for in-memory caching in Node.js applications.
44

5+
Sorry 🦖, this is an ESM-only package.
6+
57
## Installation
68

79
To install `node-cache-plus`, use your preferred package manager:

Diff for: package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
1515
"check": "biome check ./src --diagnostic-level=error --fix",
1616
"check-tsc": "tsc --noEmit",
17-
"test": "vitest run",
17+
"test": "vitest run --printConsoleTrace=true",
1818
"ci": "pnpm check && pnpm test && pnpm build && pnpm check-exports",
19-
"prepublishOnly": "pnpm run ci"
19+
"prepublishOnly": "pnpm run ci",
20+
"prepare": "husky"
2021
},
2122
"keywords": [
2223
"cache",
@@ -67,6 +68,7 @@
6768
"@biomejs/biome": "1.9.4",
6869
"@changesets/cli": "~2.27.10",
6970
"@types/node": "~22.10.2",
71+
"husky": "~9.1.7",
7072
"tsup": "~8.3.5",
7173
"typedoc": "~0.27.5",
7274
"typescript": "~5.7.2",

Diff for: pnpm-lock.yaml

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

Diff for: src/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Cache } from "./Cache";
33
import { getDefaultCache } from "./defaults";
44
import type { CacheFunctionOptions } from "./types";
55

6-
function defaultKeyGenerator(fnName: string, args: any[]): string {
6+
export function defaultKeyGenerator(fnName: string, args: any[] = []): string {
77
const hash = crypto
88
.createHash("sha1")
99
.update(JSON.stringify(args))
@@ -36,7 +36,7 @@ export async function cachedCall<T, A extends any[]>(
3636
} = options;
3737

3838
const cache = cacheInstance || getDefaultCache();
39-
const fnName = fn.name || "anonymous_function";
39+
const fnName = `cachedCall_${fn.name || "anonymous_function"}`;
4040
const cacheKey = key ?? keyGenerator(fnName, fnArgs);
4141

4242
const cachedData = cache.get<T>(cacheKey);
@@ -72,7 +72,7 @@ export function withCache<T extends (...args: any[]) => Promise<any>>(
7272
cacheInstance,
7373
} = options;
7474

75-
const fnName = fn.name || "anonymous_function";
75+
const fnName = `withCache_${fn.name || "anonymous_function"}`;
7676

7777
const cachedFn = async (...args: Parameters<T>): Promise<ReturnType<T>> => {
7878
const cache: Cache = cacheInstance || getDefaultCache();

Diff for: src/tests/helpers.test.ts

+72-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { cachedCall, withCache } from "../helpers";
2+
import { cachedCall, withCache, defaultKeyGenerator } from "../helpers";
33

44
// A mock function that simulates an expensive async operation
55
async function expensiveFunction(param: string): Promise<string> {
@@ -145,3 +145,74 @@ describe("cachedFunction Wrapper with complex parameters", () => {
145145
).toBeGreaterThanOrEqual(500);
146146
});
147147
});
148+
149+
async function noArgFunction(): Promise<string> {
150+
return new Promise((resolve) => {
151+
setTimeout(() => {
152+
resolve("no_arg_result");
153+
}, 500);
154+
});
155+
}
156+
157+
describe("cachedCall with no arguments", () => {
158+
it("should run the function and cache the result on initial call", async () => {
159+
const result = await cachedCall(noArgFunction, {
160+
ttl: 2,
161+
tags: ["testTag"],
162+
});
163+
expect(result).toBe("no_arg_result");
164+
});
165+
166+
it("should return cached result on second call", async () => {
167+
await cachedCall(noArgFunction, { ttl: 2, tags: ["testTag"] });
168+
const startCachedCall = Date.now();
169+
const result = await cachedCall(noArgFunction, {
170+
ttl: 2,
171+
tags: ["testTag"],
172+
});
173+
const endCachedCall = Date.now();
174+
expect(result).toBe("no_arg_result");
175+
expect(endCachedCall - startCachedCall).toBeLessThan(100);
176+
});
177+
});
178+
179+
const cachedNoArgFunction = withCache(noArgFunction, {
180+
ttl: 3600,
181+
tags: ["tag1"],
182+
});
183+
184+
describe("withCache with no arguments", () => {
185+
it("should run the function and cache the result on initial call", async () => {
186+
const startFirstCall = Date.now();
187+
const cachedResult = await cachedNoArgFunction();
188+
const endFirstCall = Date.now();
189+
expect(cachedResult).toBe("no_arg_result");
190+
expect(endFirstCall - startFirstCall).toBeGreaterThanOrEqual(500);
191+
});
192+
193+
it("should return cached result on second call", async () => {
194+
await cachedNoArgFunction();
195+
const startCachedCall2 = Date.now();
196+
const cachedResult = await cachedNoArgFunction();
197+
const endCachedCall2 = Date.now();
198+
expect(cachedResult).toBe("no_arg_result");
199+
expect(endCachedCall2 - startCachedCall2).toBeLessThan(100);
200+
});
201+
});
202+
203+
describe("defaultKeyGenerator", () => {
204+
it("should generate a key with no arguments", () => {
205+
const key = defaultKeyGenerator("testFunction", []);
206+
expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/);
207+
});
208+
209+
it("should generate a key with null arguments", () => {
210+
const key = defaultKeyGenerator("testFunction", null as any);
211+
expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/);
212+
});
213+
214+
it("should generate a key with undefined arguments", () => {
215+
const key = defaultKeyGenerator("testFunction", undefined);
216+
expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/);
217+
});
218+
});

0 commit comments

Comments
 (0)