Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 13, 2024
1 parent 6bc7032 commit ff0f539
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
yarn.lock
dist
distribution
18 changes: 9 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export default function memoize<
FunctionToMemoize extends AnyFunction,
CacheKeyType,
>(
fn: FunctionToMemoize,
function_: FunctionToMemoize,
{
cacheKey,
cache = new Map(),
maxAge,
}: Options<FunctionToMemoize, CacheKeyType> = {},
): FunctionToMemoize {
if (maxAge === 0) {
return fn;
return function_;
}

if (typeof maxAge === 'number') {
Expand All @@ -127,7 +127,7 @@ export default function memoize<
return cacheItem.data;
}

const result = fn.apply(this, arguments_) as ReturnType<FunctionToMemoize>;
const result = function_.apply(this, arguments_) as ReturnType<FunctionToMemoize>;

cache.set(key, {
data: result,
Expand All @@ -141,15 +141,15 @@ export default function memoize<

timer.unref?.();

const timers = cacheTimerStore.get(fn) ?? new Set<number>();
const timers = cacheTimerStore.get(function_) ?? new Set<number>();
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,
});

Expand Down Expand Up @@ -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!');
}
Expand All @@ -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);
}
}
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -61,7 +61,8 @@
},
"nodeArguments": [
"--loader=ts-node/esm"
]
],
"workerThreads": false
},
"xo": {
"rules": {
Expand Down
30 changes: 15 additions & 15 deletions test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof fn>(memoize(fn));
expectType<typeof fn>(memoize(fn, {maxAge: 1}));
expectType<typeof fn>(memoize(fn, {cacheKey: ([firstArgument]: [string]) => firstArgument}));
expectType<typeof fn>(
memoize(fn, {
expectType<typeof function_>(memoize(function_));
expectType<typeof function_>(memoize(function_, {maxAge: 1}));
expectType<typeof function_>(memoize(function_, {cacheKey: ([firstArgument]: [string]) => firstArgument}));
expectType<typeof function_>(
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<typeof fn>(
expectType<typeof function_>(
// The `firstArgument` of `fn` is of type `string`, so it's used
memoize(fn, {cache: new Map<string, {data: boolean; maxAge: number}>()}),
memoize(function_, {cache: new Map<string, {data: boolean; maxAge: number}>()}),
);

/* 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<typeof overloadedFn>(memoize(overloadedFn));
expectType<true>(memoize(overloadedFn)(true));
expectType<false>(memoize(overloadedFn)(false));
expectType<typeof overloadedFunction>(memoize(overloadedFunction));
expectType<true>(memoize(overloadedFunction)(true));
expectType<false>(memoize(overloadedFunction)(false));

memoizeClear(fn);
memoizeClear(function_);

// `cacheKey` tests.
// The argument should match the memoized function’s parameters
Expand Down
12 changes: 6 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}, {
Expand All @@ -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(),
Expand Down Expand Up @@ -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 => {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "dist",
"experimentalDecorators": true
},
"files": [
Expand Down

0 comments on commit ff0f539

Please sign in to comment.