Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and require Node.js 10 #67

Merged
merged 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare namespace mem {
interface CacheStorage<KeyType, ValueType> {
has(key: KeyType): boolean;
get(key: KeyType): ValueType | undefined;
set(key: KeyType, value: ValueType): void;
delete(key: KeyType): void;
has: (key: KeyType) => boolean;
get: (key: KeyType) => ValueType | undefined;
set: (key: KeyType, value: ValueType) => void;
delete: (key: KeyType) => void;
clear?: () => void;
}

Expand Down Expand Up @@ -100,9 +100,9 @@ declare const mem: {

@param fn - Memoized function.
*/
clear<ArgumentsType extends unknown[], ReturnType>(
clear: <ArgumentsType extends unknown[], ReturnType>(
fn: (...arguments: ArgumentsType) => ReturnType
): void;
) => void;
};

export = mem;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const mem = (fn, {
// The below call will throw in some host environments
// See https://github.com/sindresorhus/mimic-fn/issues/10
mimicFn(memoized, fn);
} catch (_) {}
} catch {}

cacheStore.set(memoized, cache);

Expand Down
29 changes: 0 additions & 29 deletions index.test-d.ts

This file was deleted.

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -38,10 +38,16 @@
"mimic-fn": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"@types/serialize-javascript": "^4.0.0",
"ava": "^3.13.0",
"delay": "^4.1.0",
"serialize-javascript": "^2.1.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
"serialize-javascript": "^5.0.1",
"tsd": "^0.13.1",
"xo": "^0.33.1"
},
"xo": {
"rules": {
"@typescript-eslint/member-ordering": "off"
}
}
}
32 changes: 32 additions & 0 deletions test-d/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {expectType} from 'tsd';
import mem = require('..');

const fn = (text: string) => Boolean(text);

expectType<typeof fn>(mem(fn));
expectType<typeof fn>(mem(fn, {maxAge: 1}));
expectType<typeof fn>(mem(fn, {cacheKey: ([firstArgument]) => firstArgument}));
expectType<typeof fn>(
mem(fn, {
// 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>(
// The `firstArgument` of `fn` is of type `string`, so it's used
mem(fn, {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 {
return parameter;
}

expectType<typeof overloadedFn>(mem(overloadedFn));
expectType<true>(mem(overloadedFn)(true));
expectType<false>(mem(overloadedFn)(false));

mem.clear(fn);
16 changes: 10 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import delay from 'delay';
import serializeJavascript from 'serialize-javascript';
import mem from '.';
const test = require('ava');
const delay = require('delay');
const serializeJavascript = require('serialize-javascript');
const mem = require('.');

test('memoize', t => {
let i = 0;
Expand Down Expand Up @@ -132,7 +132,9 @@ test('maxAge items are deleted even if function throws', async t => {
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.throws(() => memoized(1), 'failure');
t.throws(() => memoized(1), {
message: 'failure'
});
t.is(cache.size, 0);
});

Expand Down Expand Up @@ -195,5 +197,7 @@ test('prototype support', t => {
test('mem.clear() throws when called with a plain function', t => {
t.throws(() => {
mem.clear(() => {});
}, 'Can\'t clear a function that was not memoized!');
}, {
message: 'Can\'t clear a function that was not memoized!'
});
});