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

Add .decorator() method #61

Merged
merged 29 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6438d9b
Add `.mixin()`
Richienb Aug 20, 2020
5be90de
Update index.d.ts
Richienb Aug 20, 2020
d3d5df0
Update index.d.ts
Richienb Aug 20, 2020
7fe63b4
Update index.js
Richienb Aug 21, 2020
16ecd9d
Fix formatting
Richienb Aug 21, 2020
45b358b
Merge branches 'add-decorator' and 'add-decorator' of https://github.…
Richienb Aug 21, 2020
782abbd
Remove colon
Richienb Aug 22, 2020
a5eee78
Rename to `.decorator`
Richienb Aug 22, 2020
ed5547a
Extend tests
fregante Aug 22, 2020
2307895
Properly indent typings
Richienb Aug 23, 2020
c32dc30
Remove unrelated changes
Richienb Aug 23, 2020
444ea2f
Add example with options
Richienb Aug 23, 2020
3be99b2
Update index.d.ts
Richienb Aug 23, 2020
9c0cf3b
import = require
fregante Aug 23, 2020
8ab5b79
Merge remote-tracking branch 'upstream/master' into add-decorator
Richienb Oct 7, 2020
1900859
Update index.ts
Richienb Oct 7, 2020
2ae22a8
Merge remote-tracking branch 'upstream/master' into add-decorator
Richienb Oct 10, 2020
19a6598
Move tests to main test file
Richienb Oct 10, 2020
ba89249
Lint
fregante Oct 10, 2020
914506a
Fix options typings
Richienb Dec 19, 2020
28bae9a
Merge remote-tracking branch 'upstream/master' into add-decorator
Richienb Dec 20, 2020
5a9ea93
Merge remote-tracking branch 'upstream/master' into add-decorator
Richienb Dec 20, 2020
31735fd
Update test.ts
Richienb Dec 20, 2020
97a358a
Fix tests
Richienb Mar 9, 2021
7a8b838
Update index.ts
Richienb Mar 9, 2021
cf406a3
Update index.ts
Richienb Mar 10, 2021
a34fcf1
Update readme.md
Richienb Mar 10, 2021
1119f2e
Update readme.md
Richienb Mar 10, 2021
f782bce
Minor tweaks
Richienb Mar 10, 2021
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
31 changes: 31 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ declare const mem: {
options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType>
): FunctionToMemoize;

/**
Returns a decorator which memoizes the function provided.
Richienb marked this conversation as resolved.
Show resolved Hide resolved

@example
```
const mem = require('mem');
fregante marked this conversation as resolved.
Show resolved Hide resolved

class Example {
constructor() {
this.i = 0;
}

@mem.mixin();
Richienb marked this conversation as resolved.
Show resolved Hide resolved
counter() {
return ++this.i;
}
}
```
*/
mixin<ArgumentsType extends unknown[],
Richienb marked this conversation as resolved.
Show resolved Hide resolved
ReturnType,
CacheKeyType,
FunctionToMemoize = (...arguments: ArgumentsType) => ReturnType
>(
options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType>
): (
target: FunctionToMemoize,
propertyKey: string,
descriptor: PropertyDescriptor
) => FunctionToMemoize;

/**
Clear all cached data of a memoized function.

Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ const mem = (fn, {

module.exports = mem;

module.exports.mixin = options => (target, propertyKey, descriptor) => {
if (typeof target !== 'function') {
throw new TypeError('`target` must be a function');
}

descriptor.value = mem(target[propertyKey], options);
Richienb marked this conversation as resolved.
Show resolved Hide resolved

return target;
};

module.exports.clear = fn => {
if (!cacheStore.has(fn)) {
throw new Error('Can\'t clear a function that was not memoized!');
Expand Down
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ Use a different cache storage. Must implement the following methods: `.has(key)`

Refer to the [caching strategies](#caching-strategy) section for more information.

### mem.mixin(options)

Returns a decorator which memoizes the function provided.
Richienb marked this conversation as resolved.
Show resolved Hide resolved

#### options

Type: `object`

Same as options for `mem()`.

```js
const mem = require('mem');

class Example {
constructor() {
this.i = 0;
Richienb marked this conversation as resolved.
Show resolved Hide resolved
}

@mem.mixin();
Richienb marked this conversation as resolved.
Show resolved Hide resolved
counter() {
return ++this.i;
}
}
```

### mem.clear(fn)

Clear all cached data of a memoized function.
Expand Down
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ test('preserves the original function name', t => {
t.is(mem(function foo() {}).name, 'foo'); // eslint-disable-line func-names
});

test('.mixin()', t => {
class TestClass {
constructor() {
this.i = 0;
}

counter() {
return ++this.i;
}
}

mem.mixin()(TestClass.prototype.counter, 'counter', TestClass.prototype);

t.is(new TestClass().counter(), 1);
});

test('.clear()', t => {
let i = 0;
const fixture = () => i++;
Expand Down Expand Up @@ -194,6 +210,6 @@ test('prototype support', t => {

test('mem.clear() throws when called with a plain function', t => {
t.throws(() => {
mem.clear(() => {});
mem.clear(() => { });
fregante marked this conversation as resolved.
Show resolved Hide resolved
}, 'Can\'t clear a function that was not memoized!');
});