Skip to content

Commit 78654fc

Browse files
committed
rename matchesArg to isArgEqual and matchesKey to isKeyEqual to be more declarative
1 parent 57bbf3a commit 78654fc

File tree

9 files changed

+81
-52
lines changed

9 files changed

+81
-52
lines changed

DEV_ONLY/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ console.groupEnd();
8181
console.group('maxArgs');
8282

8383
const memoizedMax = memoize(method, {
84-
matchesKey: (originalKey, newKey) => originalKey[0] === newKey[0],
84+
isKeyEqual: (originalKey, newKey) => originalKey[0] === newKey[0],
8585
});
8686

8787
memoizedMax(foo, bar);
@@ -105,7 +105,7 @@ const deepEqualMethod = ({
105105
};
106106

107107
const deepEqualMemoized = memoize(deepEqualMethod, {
108-
matchesArg: deepEqual,
108+
isArgEqual: deepEqual,
109109
});
110110

111111
deepEqualMemoized({ one: 1, two: 2 });
@@ -208,7 +208,7 @@ const noFns = (one: string, two: string, three: Function) => {
208208
};
209209

210210
const memoizedNoFns = memoize(noFns, {
211-
matchesArg(key1, key2) {
211+
isArgEqual(key1, key2) {
212212
return key1 === key2;
213213
},
214214
transformKey(args) {
@@ -233,7 +233,7 @@ const matchingKeyMethod = function (object: {
233233
};
234234

235235
const matchingKeyMemoized = memoize(matchingKeyMethod, {
236-
matchesKey: deepEqual,
236+
isKeyEqual: deepEqual,
237237
maxSize: 10,
238238
});
239239

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A tiny, crazy [fast](#benchmarks) memoization library for the 95% use-case
1212
- [Composition](#composition)
1313
- [Options](#options)
1414
- [async](#async)
15-
- [matchesArg](#matchesarg)
16-
- [matchesKey](#matcheskey)
15+
- [isArgEqual](#matchesarg)
16+
- [isKeyEqual](#matcheskey)
1717
- [maxSize](#maxsize)
1818
- [transformKey](#transformkey)
1919
- [Cache](#cache)
@@ -72,7 +72,7 @@ Starting in `4.0.0`, you can compose memoized functions if you want to have mult
7272
```ts
7373
const simple = memoized(fn); // { maxSize: 1 }
7474
const upToFive = memoized(simple, { maxSize: 5 }); // { maxSize: 5 }
75-
const withCustomEquals = memoized(upToFive, { matchesArg: deepEqual }); // { maxSize: 5, matchesArg: deepEqual }
75+
const withCustomEquals = memoized(upToFive, { isArgEqual: deepEqual }); // { maxSize: 5, isArgEqual: deepEqual }
7676
```
7777

7878
**NOTE**: The original function is the function used in the composition, the composition only applies to the options. In the example above, `upToFive` does not call `simple`, it calls `fn`.
@@ -110,7 +110,7 @@ setTimeout(() => {
110110

111111
**NOTE**: If you don't want rejections to auto-remove the entry from cache, set `async` to `false` (or simply do not set it), but be aware this will also remove the cache listeners that fire on successful resolution.
112112

113-
### matchesArg
113+
### isArgEqual
114114

115115
`function(arg1: any, arg2: any): boolean`, _defaults to `isSameValueZero`_
116116

@@ -136,7 +136,7 @@ const deepObject = (object: {
136136
bar: object.bar,
137137
});
138138

139-
const memoizedDeepObject = memoize(deepObject, { matchesArg: deepEqual });
139+
const memoizedDeepObject = memoize(deepObject, { isArgEqual: deepEqual });
140140

141141
console.log(
142142
memoizedDeepObject({
@@ -169,7 +169,7 @@ console.log(
169169

170170
**NOTE**: The default method tests for [SameValueZero](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) equality, which is summarized as strictly equal while also considering `NaN` equal to `NaN`.
171171

172-
### matchesKey
172+
### isKeyEqual
173173

174174
`function(existingKey: any[], passedKey: any[]): boolean`
175175

@@ -193,7 +193,7 @@ const deepObject = (object: ContrivedObject) => ({
193193

194194
const memoizedShape = memoize(deepObject, {
195195
// receives the full key in cache and the full key of the most recent call
196-
matchesKey(key1, key2) {
196+
isKeyEqual(key1, key2) {
197197
const object1 = key1[0];
198198
const object2 = key2[0];
199199

@@ -261,13 +261,13 @@ console.log(memoized('one', () => {})); // ['one', () => {}]
261261
console.log(memoized('one', () => {})); // pulled from cache, ['one', () => {}]
262262
```
263263

264-
If your transformed keys require something other than `SameValueZero` equality, you can combine `transformKey` with [`matchesArg`](#isequal) for completely custom key creation and comparison.
264+
If your transformed keys require something other than `SameValueZero` equality, you can combine `transformKey` with [`isArgEqual`](#isequal) for completely custom key creation and comparison.
265265

266266
```ts
267267
const ignoreFunctionArg = (one: string, two: () => void) => [one, two];
268268

269269
const memoized = memoize(ignoreFunctionArg, {
270-
matchesKey: (key1, key2) => key1[0] === key2[0],
270+
isKeyEqual: (key1, key2) => key1[0] === key2[0],
271271
// Cache based on the serialized first parameter
272272
transformKey: (args) => [JSON.stringify(args[0])],
273273
});

__tests__/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('memoize', () => {
200200
]);
201201
});
202202

203-
it('will return the memoized function that will use the custom matchesArg method', () => {
203+
it('will return the memoized function that will use the custom isArgEqual method', () => {
204204
let callCount = 0;
205205

206206
const fn = (one: any, two: any) => {
@@ -212,9 +212,9 @@ describe('memoize', () => {
212212
};
213213
};
214214

215-
const memoized = memoize(fn, { matchesArg: deepEqual });
215+
const memoized = memoize(fn, { isArgEqual: deepEqual });
216216

217-
expect(memoized.options.matchesArg).toBe(deepEqual);
217+
expect(memoized.options.isArgEqual).toBe(deepEqual);
218218

219219
expect(
220220
memoized(
@@ -292,7 +292,7 @@ describe('memoize', () => {
292292
]);
293293
});
294294

295-
it('will return the memoized function that will use the transformKey method with a custom matchesArg', () => {
295+
it('will return the memoized function that will use the transformKey method with a custom isArgEqual', () => {
296296
let callCount = 0;
297297

298298
const fn = (one: any, two: any) => {
@@ -303,7 +303,7 @@ describe('memoize', () => {
303303
two,
304304
};
305305
};
306-
const matchesArg = function (key1: any, key2: any) {
306+
const isArgEqual = function (key1: any, key2: any) {
307307
return key1.args === key2.args;
308308
};
309309
const transformKey = function (args: any[]) {
@@ -315,11 +315,11 @@ describe('memoize', () => {
315315
};
316316

317317
const memoized = memoize(fn, {
318-
matchesArg,
318+
isArgEqual,
319319
transformKey,
320320
});
321321

322-
expect(memoized.options.matchesArg).toBe(matchesArg);
322+
expect(memoized.options.isArgEqual).toBe(isArgEqual);
323323
expect(memoized.options.transformKey).toBe(transformKey);
324324

325325
const fnArg1 = () => {};
@@ -627,14 +627,14 @@ describe('memoize', () => {
627627
const fn = () => {};
628628

629629
const maxSize = 5;
630-
const matchesArg = () => true;
630+
const isArgEqual = () => true;
631631

632632
const memoized = memoize(fn, { maxSize });
633-
const reMemoized = memoize(memoized, { matchesArg });
633+
const reMemoized = memoize(memoized, { isArgEqual });
634634

635635
expect(reMemoized).not.toBe(memoized);
636636
expect(reMemoized.options.maxSize).toBe(maxSize);
637-
expect(reMemoized.options.matchesArg).toBe(matchesArg);
637+
expect(reMemoized.options.isArgEqual).toBe(isArgEqual);
638638
});
639639

640640
it('will throw an error if not a function', () => {
@@ -656,7 +656,7 @@ describe('memoize', () => {
656656
expect(result2).toBe(result1);
657657
});
658658

659-
it('matches for option `matchesArg`', () => {
659+
it('matches for option `isArgEqual`', () => {
660660
type ContrivedObject = {
661661
deep: string;
662662
};
@@ -670,7 +670,7 @@ describe('memoize', () => {
670670
bar: object.bar,
671671
});
672672

673-
const memoizedDeepObject = memoize(deepObject, { matchesArg: deepEqual });
673+
const memoizedDeepObject = memoize(deepObject, { isArgEqual: deepEqual });
674674

675675
const result1 = memoizedDeepObject({
676676
foo: {
@@ -702,7 +702,7 @@ describe('memoize', () => {
702702
expect(result2).toBe(result1);
703703
});
704704

705-
it('matches for option `matchesKey`', () => {
705+
it('matches for option `isKeyEqual`', () => {
706706
type ContrivedObject = { foo: string; bar: number; baz: string };
707707

708708
const deepObject = (object: ContrivedObject) => ({
@@ -712,7 +712,7 @@ describe('memoize', () => {
712712

713713
const memoizedShape = memoize(deepObject, {
714714
// receives the full key in cache and the full key of the most recent call
715-
matchesKey(key1, key2) {
715+
isKeyEqual(key1, key2) {
716716
const object1 = key1[0];
717717
const object2 = key2[0];
718718

@@ -949,7 +949,7 @@ describe('memoize', () => {
949949
}));
950950

951951
const memoized = memoize(ignoreFunctionArg, {
952-
matchesKey: (key1, key2) => key1[0] === key2[0],
952+
isKeyEqual: (key1, key2) => key1[0] === key2[0],
953953
// Cache based on the serialized first parameter
954954
transformKey: (args) => [
955955
JSON.stringify(args, (_key: string, value: any) =>

benchmarks/index.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const lruMemoize = require('lru-memoize').default;
2929
const mem = require('mem');
3030
const memoizee = require('memoizee');
3131
const memoizerific = require('memoizerific');
32-
const memoize = require('../dist/micro-memoize.cjs.js').default;
32+
const memoize = require('../dist/cjs/index.cjs').default;
3333
const ramda = require('ramda').memoizeWith(resolveArguments);
3434
const underscore = require('underscore').memoize;
3535

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Memoize } from './dist/umd/types/internalTypes.d';
22

3-
export * from './dist/umd/types/internalTypes.d';
3+
export type * from './dist/umd/types/internalTypes.d';
44
export type { Cache } from './dist/umd/types/Cache.d';
55

66
/**

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@
9898
"url": "git+https://github.com/planttheidea/micro-memoize.git"
9999
},
100100
"scripts": {
101-
"benchmark": "npm run clean && npm run build && NODE_ENV=production node ./benchmarks/index.cjs",
101+
"benchmark": "npm run build:cjs && NODE_ENV=production node ./benchmarks/index.cjs",
102102
"build": "npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:min",
103103
"build:cjs": "rimraf dist/cjs && NODE_ENV=production rollup -c build/rollup/config.cjs.js && tsc -p ./build/tsconfig/cjs.json",
104104
"build:esm": "rimraf dist/esm && NODE_ENV=production rollup -c build/rollup/config.esm.js && tsc -p ./build/tsconfig/esm.json",
105105
"build:min": "rimraf dist/min && NODE_ENV=production rollup -c build/rollup/config.min.js && tsc -p ./build/tsconfig/min.json",
106106
"build:umd": "rimraf dist/umd && NODE_ENV=production rollup -c build/rollup/config.umd.js && tsc -p ./build/tsconfig/umd.json",
107-
"clean": "rimraf dist && rimraf mjs",
108107
"dev": "NODE_ENV=development webpack-dev-server --config=build/webpack.config.js",
109108
"lint": "NODE_ENV=test eslint src/*.ts",
110109
"lint:fix": "npm run lint -- --fix",

src/Cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export class Cache<Fn extends (...args: any[]) => any> {
3030
constructor(options: Options<Fn>) {
3131
const transformKey = getDefault('function', options.transformKey);
3232

33-
this.a = getDefault('function', options.matchesArg, isSameValueZero);
33+
this.a = getDefault('function', options.isArgEqual, isSameValueZero);
3434
this.l = getDefault('number', options.maxSize, 1);
35-
this.m = getDefault('function', options.matchesKey, this.e);
35+
this.m = getDefault('function', options.isKeyEqual, this.e);
3636
this.p = getDefault('boolean', options.async, false);
3737

38-
if (transformKey || options.matchesKey === this.m) {
38+
if (transformKey || options.isKeyEqual === this.m) {
3939
this.k = transformKey
4040
? (args: IArguments | Key) => transformKey(cloneKey<Fn>(args))
4141
: cloneKey;

src/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import type { Key, Memoize, Memoized, Options } from './internalTypes';
22
import { Cache } from './Cache';
33
import { cloneKey, isMemoized } from './utils';
44

5+
export type * from './internalTypes';
6+
7+
export { Cache };
8+
59
const memoize: Memoize = function memoize<
610
Fn extends (...args: any[]) => any,
711
Opts extends Options<Fn>,
@@ -19,16 +23,15 @@ const memoize: Memoize = function memoize<
1923
return memoize(fn.fn, Object.assign({}, fn.options, passedOptions));
2024
}
2125

22-
const cache = new Cache(passedOptions);
23-
2426
const memoized: Memoized<Fn, Opts> = function memoized(this: any) {
25-
const { h: head, k: transformKey } = cache;
27+
const cache = memoized.cache;
28+
const transformKey = cache.k;
2629
const key = transformKey
2730
? transformKey(arguments)
2831
: (arguments as unknown as Key);
2932
let node = cache.g(key);
3033

31-
if (node === head) {
34+
if (node === cache.h) {
3235
cache.oh && cache.oh.n(node);
3336
} else if (node) {
3437
cache.u(node);
@@ -46,14 +49,12 @@ const memoize: Memoize = function memoize<
4649
return node.v;
4750
};
4851

49-
memoized.cache = cache;
52+
memoized.cache = new Cache(passedOptions);
5053
memoized.fn = fn;
5154
memoized.isMemoized = true;
5255
memoized.options = passedOptions;
5356

5457
return memoized;
5558
};
5659

57-
memoize.Cache = Cache;
58-
5960
export default memoize;

0 commit comments

Comments
 (0)