From ff0f5391a1dfdb3996cceec15a5815360d4952a8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 13 Aug 2024 13:54:58 +0200 Subject: [PATCH] Meta tweaks --- .gitignore | 2 +- index.ts | 18 +++++++++--------- package.json | 27 ++++++++++++++------------- test-d/index.test-d.ts | 30 +++++++++++++++--------------- test.ts | 12 ++++++------ tsconfig.json | 1 - 6 files changed, 45 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index c406da7..b23503d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules yarn.lock -dist +distribution diff --git a/index.ts b/index.ts index dcebec5..fcca142 100644 --- a/index.ts +++ b/index.ts @@ -97,7 +97,7 @@ export default function memoize< FunctionToMemoize extends AnyFunction, CacheKeyType, >( - fn: FunctionToMemoize, + function_: FunctionToMemoize, { cacheKey, cache = new Map(), @@ -105,7 +105,7 @@ export default function memoize< }: Options = {}, ): FunctionToMemoize { if (maxAge === 0) { - return fn; + return function_; } if (typeof maxAge === 'number') { @@ -127,7 +127,7 @@ export default function memoize< return cacheItem.data; } - const result = fn.apply(this, arguments_) as ReturnType; + const result = function_.apply(this, arguments_) as ReturnType; cache.set(key, { data: result, @@ -141,15 +141,15 @@ export default function memoize< timer.unref?.(); - const timers = cacheTimerStore.get(fn) ?? new Set(); + const timers = cacheTimerStore.get(function_) ?? new Set(); timers.add(timer as unknown as number); - cacheTimerStore.set(fn, timers); + cacheTimerStore.set(function_, timers); } return result; } as FunctionToMemoize; - mimicFunction(memoized, fn, { + mimicFunction(memoized, function_, { ignoreNonConfigurable: true, }); @@ -223,8 +223,8 @@ Clear all cached data of a memoized function. @param fn - The memoized function. */ -export function memoizeClear(fn: AnyFunction): void { - const cache = cacheStore.get(fn); +export function memoizeClear(function_: AnyFunction): void { + const cache = cacheStore.get(function_); if (!cache) { throw new TypeError('Can\'t clear a function that was not memoized!'); } @@ -235,7 +235,7 @@ export function memoizeClear(fn: AnyFunction): void { cache.clear(); - for (const timer of cacheTimerStore.get(fn) ?? []) { + for (const timer of cacheTimerStore.get(function_) ?? []) { clearTimeout(timer); } } diff --git a/package.json b/package.json index 768d0c9..ecd0178 100644 --- a/package.json +++ b/package.json @@ -12,20 +12,20 @@ }, "type": "module", "exports": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" + "types": "./distribution/index.d.ts", + "default": "./distribution/index.js" }, "sideEffects": false, "engines": { "node": ">=18" }, "scripts": { - "test": "xo && ava && npm run build && tsd --typings dist/index.d.ts", - "build": "del-cli dist && tsc", + "test": "xo && ava && npm run build && tsd --typings distribution/index.d.ts", + "build": "del-cli distribution && tsc", "prepack": "npm run build" }, "files": [ - "dist" + "distribution" ], "keywords": [ "memoize", @@ -41,18 +41,18 @@ "promise" ], "dependencies": { - "mimic-function": "^5.0.0" + "mimic-function": "^5.0.1" }, "devDependencies": { - "@sindresorhus/tsconfig": "^5.0.0", + "@sindresorhus/tsconfig": "^6.0.0", "@types/serialize-javascript": "^5.0.4", - "ava": "^5.3.1", + "ava": "^6.1.3", "del-cli": "^5.1.0", "delay": "^6.0.0", - "serialize-javascript": "^6.0.1", - "ts-node": "^10.9.1", - "tsd": "^0.29.0", - "xo": "^0.56.0" + "serialize-javascript": "^6.0.2", + "ts-node": "^10.9.2", + "tsd": "^0.31.1", + "xo": "^0.59.3" }, "ava": { "timeout": "1m", @@ -61,7 +61,8 @@ }, "nodeArguments": [ "--loader=ts-node/esm" - ] + ], + "workerThreads": false }, "xo": { "rules": { diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index d043730..41fb411 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -2,35 +2,35 @@ import {expectType} from 'tsd'; import memoize, {memoizeClear} from '../index.js'; // eslint-disable-next-line unicorn/prefer-native-coercion-functions -- Required `string` type -const fn = (text: string) => Boolean(text); +const function_ = (text: string) => Boolean(text); -expectType(memoize(fn)); -expectType(memoize(fn, {maxAge: 1})); -expectType(memoize(fn, {cacheKey: ([firstArgument]: [string]) => firstArgument})); -expectType( - memoize(fn, { +expectType(memoize(function_)); +expectType(memoize(function_, {maxAge: 1})); +expectType(memoize(function_, {cacheKey: ([firstArgument]: [string]) => firstArgument})); +expectType( + memoize(function_, { // The cacheKey returns an array. This isn't deduplicated by a regular Map, but it's valid. The correct solution would be to use ManyKeysMap to deduplicate it correctly cacheKey: (arguments_: [string]) => arguments_, cache: new Map<[string], {data: boolean; maxAge: number}>(), }), ); -expectType( +expectType( // The `firstArgument` of `fn` is of type `string`, so it's used - memoize(fn, {cache: new Map()}), + memoize(function_, {cache: new Map()}), ); /* Overloaded function tests */ -function overloadedFn(parameter: false): false; -function overloadedFn(parameter: true): true; -function overloadedFn(parameter: boolean): boolean { +function overloadedFunction(parameter: false): false; +function overloadedFunction(parameter: true): true; +function overloadedFunction(parameter: boolean): boolean { return parameter; } -expectType(memoize(overloadedFn)); -expectType(memoize(overloadedFn)(true)); -expectType(memoize(overloadedFn)(false)); +expectType(memoize(overloadedFunction)); +expectType(memoize(overloadedFunction)(true)); +expectType(memoize(overloadedFunction)(false)); -memoizeClear(fn); +memoizeClear(function_); // `cacheKey` tests. // The argument should match the memoized function’s parameters diff --git a/test.ts b/test.ts index e2e086c..4d7df09 100644 --- a/test.ts +++ b/test.ts @@ -193,7 +193,7 @@ test('prototype support', t => { t.is(unicorn.foo(), 0); }); -test('.decorator()', t => { +test('memoizeDecorator()', t => { let returnValue = 1; const returnValue2 = 101; @@ -218,7 +218,7 @@ test('.decorator()', t => { t.is(beta.counter(), 2, 'The method should not be memoized across instances'); }); -test('memClear() throws when called with a plain function', t => { +test('memoizeClear() throws when called with a plain function', t => { t.throws(() => { memoizeClear(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function }, { @@ -227,7 +227,7 @@ test('memClear() throws when called with a plain function', t => { }); }); -test('memClear() throws when called on an unclearable cache', t => { +test('memoizeClear() throws when called on an unclearable cache', t => { const fixture = () => 1; const memoized = memoize(fixture, { cache: new WeakMap(), @@ -281,10 +281,10 @@ test('maxAge - complex arguments and cache expiration', async t => { const fixture = object => index++; const memoized = memoize(fixture, {maxAge: 100, cacheKey: JSON.stringify}); - const arg = {key: 'value'}; - t.is(memoized(arg), 0); + const argument = {key: 'value'}; + t.is(memoized(argument), 0); await delay(150); - t.is(memoized(arg), 1); // Argument is the same, but should recompute due to expiration + t.is(memoized(argument), 1); // Argument is the same, but should recompute due to expiration }); test('maxAge - concurrent calls return cached value', async t => { diff --git a/tsconfig.json b/tsconfig.json index b8dfe5b..ef8e4cd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "outDir": "dist", "experimentalDecorators": true }, "files": [