-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function cache to reduce boilerplate
- Loading branch information
Showing
10 changed files
with
99 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Caches the return value of the first call to the function and returns that on | ||
// subsequent calls. Only works with functions without arguments. | ||
module.exports = (func, bind = undefined) => { | ||
let cache = undefined | ||
|
||
return () => { | ||
if (cache === undefined) { | ||
cache = func.call(bind) | ||
} | ||
|
||
return cache | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const cacheFunction = require('../../../src/services/cache_function') | ||
|
||
test('accepts/calls `func` and returns the value', () => { | ||
const value = 'test123' | ||
const func = jest.fn(() => value) | ||
|
||
expect(cacheFunction(func)()).toEqual(value) | ||
expect(func).toHaveBeenCalled() | ||
}) | ||
|
||
test('accepts optional `bind` to set `this` for `func`', () => { | ||
const value = { | ||
value: 'test123', | ||
test() { | ||
return this.value | ||
}, | ||
} | ||
|
||
expect(cacheFunction(value.test, value)()).toEqual(value.value) | ||
}) | ||
|
||
test('sets `this` to `undefined` for `func` if `bind` not set', () => { | ||
const value = { | ||
value: 'test123', | ||
test() { | ||
return this.value | ||
}, | ||
} | ||
|
||
expect(cacheFunction(value.test)()).toBeUndefined() | ||
}) | ||
|
||
test('returns the cached value on subsequent calls', () => { | ||
const value = 'test123' | ||
const func = jest.fn(() => value) | ||
const cachedFunc = cacheFunction(func) | ||
|
||
expect(cachedFunc()).toEqual(value) | ||
expect(cachedFunc()).toEqual(value) | ||
expect(func).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('does not cache the value if `undefined`', () => { | ||
const value = undefined | ||
const func = jest.fn(() => value) | ||
const cachedFunc = cacheFunction(func) | ||
|
||
expect(cachedFunc()).toEqual(value) | ||
expect(cachedFunc()).toEqual(value) | ||
expect(func).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
test('does not cache the value if an error is thrown', () => { | ||
const error = new Error('test123') | ||
const func = jest.fn(() => { | ||
throw error | ||
}) | ||
const cachedFunc = cacheFunction(func) | ||
|
||
expect(() => cachedFunc()).toThrow(error) | ||
expect(() => cachedFunc()).toThrow(error) | ||
expect(func).toHaveBeenCalledTimes(2) | ||
}) |