Skip to content

Commit ff0f539

Browse files
committed
Meta tweaks
1 parent 6bc7032 commit ff0f539

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
22
yarn.lock
3-
dist
3+
distribution

index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ export default function memoize<
9797
FunctionToMemoize extends AnyFunction,
9898
CacheKeyType,
9999
>(
100-
fn: FunctionToMemoize,
100+
function_: FunctionToMemoize,
101101
{
102102
cacheKey,
103103
cache = new Map(),
104104
maxAge,
105105
}: Options<FunctionToMemoize, CacheKeyType> = {},
106106
): FunctionToMemoize {
107107
if (maxAge === 0) {
108-
return fn;
108+
return function_;
109109
}
110110

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

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

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

142142
timer.unref?.();
143143

144-
const timers = cacheTimerStore.get(fn) ?? new Set<number>();
144+
const timers = cacheTimerStore.get(function_) ?? new Set<number>();
145145
timers.add(timer as unknown as number);
146-
cacheTimerStore.set(fn, timers);
146+
cacheTimerStore.set(function_, timers);
147147
}
148148

149149
return result;
150150
} as FunctionToMemoize;
151151

152-
mimicFunction(memoized, fn, {
152+
mimicFunction(memoized, function_, {
153153
ignoreNonConfigurable: true,
154154
});
155155

@@ -223,8 +223,8 @@ Clear all cached data of a memoized function.
223223
224224
@param fn - The memoized function.
225225
*/
226-
export function memoizeClear(fn: AnyFunction): void {
227-
const cache = cacheStore.get(fn);
226+
export function memoizeClear(function_: AnyFunction): void {
227+
const cache = cacheStore.get(function_);
228228
if (!cache) {
229229
throw new TypeError('Can\'t clear a function that was not memoized!');
230230
}
@@ -235,7 +235,7 @@ export function memoizeClear(fn: AnyFunction): void {
235235

236236
cache.clear();
237237

238-
for (const timer of cacheTimerStore.get(fn) ?? []) {
238+
for (const timer of cacheTimerStore.get(function_) ?? []) {
239239
clearTimeout(timer);
240240
}
241241
}

package.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
},
1313
"type": "module",
1414
"exports": {
15-
"types": "./dist/index.d.ts",
16-
"default": "./dist/index.js"
15+
"types": "./distribution/index.d.ts",
16+
"default": "./distribution/index.js"
1717
},
1818
"sideEffects": false,
1919
"engines": {
2020
"node": ">=18"
2121
},
2222
"scripts": {
23-
"test": "xo && ava && npm run build && tsd --typings dist/index.d.ts",
24-
"build": "del-cli dist && tsc",
23+
"test": "xo && ava && npm run build && tsd --typings distribution/index.d.ts",
24+
"build": "del-cli distribution && tsc",
2525
"prepack": "npm run build"
2626
},
2727
"files": [
28-
"dist"
28+
"distribution"
2929
],
3030
"keywords": [
3131
"memoize",
@@ -41,18 +41,18 @@
4141
"promise"
4242
],
4343
"dependencies": {
44-
"mimic-function": "^5.0.0"
44+
"mimic-function": "^5.0.1"
4545
},
4646
"devDependencies": {
47-
"@sindresorhus/tsconfig": "^5.0.0",
47+
"@sindresorhus/tsconfig": "^6.0.0",
4848
"@types/serialize-javascript": "^5.0.4",
49-
"ava": "^5.3.1",
49+
"ava": "^6.1.3",
5050
"del-cli": "^5.1.0",
5151
"delay": "^6.0.0",
52-
"serialize-javascript": "^6.0.1",
53-
"ts-node": "^10.9.1",
54-
"tsd": "^0.29.0",
55-
"xo": "^0.56.0"
52+
"serialize-javascript": "^6.0.2",
53+
"ts-node": "^10.9.2",
54+
"tsd": "^0.31.1",
55+
"xo": "^0.59.3"
5656
},
5757
"ava": {
5858
"timeout": "1m",
@@ -61,7 +61,8 @@
6161
},
6262
"nodeArguments": [
6363
"--loader=ts-node/esm"
64-
]
64+
],
65+
"workerThreads": false
6566
},
6667
"xo": {
6768
"rules": {

test-d/index.test-d.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ import {expectType} from 'tsd';
22
import memoize, {memoizeClear} from '../index.js';
33

44
// eslint-disable-next-line unicorn/prefer-native-coercion-functions -- Required `string` type
5-
const fn = (text: string) => Boolean(text);
5+
const function_ = (text: string) => Boolean(text);
66

7-
expectType<typeof fn>(memoize(fn));
8-
expectType<typeof fn>(memoize(fn, {maxAge: 1}));
9-
expectType<typeof fn>(memoize(fn, {cacheKey: ([firstArgument]: [string]) => firstArgument}));
10-
expectType<typeof fn>(
11-
memoize(fn, {
7+
expectType<typeof function_>(memoize(function_));
8+
expectType<typeof function_>(memoize(function_, {maxAge: 1}));
9+
expectType<typeof function_>(memoize(function_, {cacheKey: ([firstArgument]: [string]) => firstArgument}));
10+
expectType<typeof function_>(
11+
memoize(function_, {
1212
// 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
1313
cacheKey: (arguments_: [string]) => arguments_,
1414
cache: new Map<[string], {data: boolean; maxAge: number}>(),
1515
}),
1616
);
17-
expectType<typeof fn>(
17+
expectType<typeof function_>(
1818
// The `firstArgument` of `fn` is of type `string`, so it's used
19-
memoize(fn, {cache: new Map<string, {data: boolean; maxAge: number}>()}),
19+
memoize(function_, {cache: new Map<string, {data: boolean; maxAge: number}>()}),
2020
);
2121

2222
/* Overloaded function tests */
23-
function overloadedFn(parameter: false): false;
24-
function overloadedFn(parameter: true): true;
25-
function overloadedFn(parameter: boolean): boolean {
23+
function overloadedFunction(parameter: false): false;
24+
function overloadedFunction(parameter: true): true;
25+
function overloadedFunction(parameter: boolean): boolean {
2626
return parameter;
2727
}
2828

29-
expectType<typeof overloadedFn>(memoize(overloadedFn));
30-
expectType<true>(memoize(overloadedFn)(true));
31-
expectType<false>(memoize(overloadedFn)(false));
29+
expectType<typeof overloadedFunction>(memoize(overloadedFunction));
30+
expectType<true>(memoize(overloadedFunction)(true));
31+
expectType<false>(memoize(overloadedFunction)(false));
3232

33-
memoizeClear(fn);
33+
memoizeClear(function_);
3434

3535
// `cacheKey` tests.
3636
// The argument should match the memoized function’s parameters

test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ test('prototype support', t => {
193193
t.is(unicorn.foo(), 0);
194194
});
195195

196-
test('.decorator()', t => {
196+
test('memoizeDecorator()', t => {
197197
let returnValue = 1;
198198
const returnValue2 = 101;
199199

@@ -218,7 +218,7 @@ test('.decorator()', t => {
218218
t.is(beta.counter(), 2, 'The method should not be memoized across instances');
219219
});
220220

221-
test('memClear() throws when called with a plain function', t => {
221+
test('memoizeClear() throws when called with a plain function', t => {
222222
t.throws(() => {
223223
memoizeClear(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
224224
}, {
@@ -227,7 +227,7 @@ test('memClear() throws when called with a plain function', t => {
227227
});
228228
});
229229

230-
test('memClear() throws when called on an unclearable cache', t => {
230+
test('memoizeClear() throws when called on an unclearable cache', t => {
231231
const fixture = () => 1;
232232
const memoized = memoize(fixture, {
233233
cache: new WeakMap(),
@@ -281,10 +281,10 @@ test('maxAge - complex arguments and cache expiration', async t => {
281281
const fixture = object => index++;
282282
const memoized = memoize(fixture, {maxAge: 100, cacheKey: JSON.stringify});
283283

284-
const arg = {key: 'value'};
285-
t.is(memoized(arg), 0);
284+
const argument = {key: 'value'};
285+
t.is(memoized(argument), 0);
286286
await delay(150);
287-
t.is(memoized(arg), 1); // Argument is the same, but should recompute due to expiration
287+
t.is(memoized(argument), 1); // Argument is the same, but should recompute due to expiration
288288
});
289289

290290
test('maxAge - concurrent calls return cached value', async t => {

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"extends": "@sindresorhus/tsconfig",
33
"compilerOptions": {
4-
"outDir": "dist",
54
"experimentalDecorators": true
65
},
76
"files": [

0 commit comments

Comments
 (0)