-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from dhershman1/dev
Dev
- Loading branch information
Showing
7 changed files
with
179 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import _curry2 from '../_internals/_curry2.js' | ||
/** | ||
* @name memoizeWith | ||
* @function | ||
* @since v3.1.0 | ||
* @category Function | ||
* @sig (*... -> String) -> function -> function | ||
* @description Wraps a function with a memoization layer to cache the results of the function | ||
* @param {Function} keyFn The function to generate a key to store the results | ||
* @param {Function} fn The function to wrap with memoization | ||
* @return {Function} A new function that will cache the results of the function | ||
* @example | ||
* import { memoizeWith } from 'kyanite' | ||
* | ||
* const add = (a, b) => a + b | ||
* const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`, add) | ||
* | ||
* memoizedAdd(1, 2) // => 3 | ||
* memoizedAdd(1, 2) // => 3 (cached) | ||
*/ | ||
const memoizeWith = (keyFn, fn) => { | ||
const cache = {} | ||
|
||
return (...args) => { | ||
const key = keyFn(...args) | ||
const result = cache[key] = cache[key] || fn(...args) | ||
|
||
return result | ||
} | ||
} | ||
|
||
export default _curry2(memoizeWith) |
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,120 @@ | ||
import test from 'tape' | ||
import memoizeWith from '../../src/function/memoizeWith.js' | ||
|
||
test('memoizeWith - basic functionality', (t) => { | ||
const add = (a, b) => a + b | ||
const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`, add) | ||
|
||
t.equal(memoizedAdd(1, 2), 3, '1 + 2 should equal 3') | ||
t.equal(memoizedAdd(1, 2), 3, '1 + 2 should equal 3 (cached)') | ||
t.equal(memoizedAdd(2, 3), 5, '2 + 3 should equal 5') | ||
t.equal(memoizedAdd(2, 3), 5, '2 + 3 should equal 5 (cached)') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memoizeWith - cache functionality', (t) => { | ||
let callCount = 0 | ||
const add = (a, b) => { | ||
callCount++ | ||
return a + b | ||
} | ||
const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`, add) | ||
|
||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
memoizedAdd(2, 3) | ||
memoizedAdd(2, 3) | ||
|
||
t.equal(callCount, 2, 'Function should be called twice due to caching') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memoizeWith - is curried', t => { | ||
let callCount = 0 | ||
const add = (a, b) => { | ||
callCount++ | ||
return a + b | ||
} | ||
const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`)(add) | ||
|
||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
memoizedAdd(2, 3) | ||
memoizedAdd(2, 3) | ||
|
||
t.equal(callCount, 2, 'Function should be called twice due to caching') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memoizeWith - calculates the value for a given input only once', t => { | ||
let callCount = 0 | ||
const add = (a, b) => { | ||
callCount++ | ||
return a + b | ||
} | ||
const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`, add) | ||
|
||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
|
||
t.equal(callCount, 1, 'Function should be called once due to caching') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memoizeWith - handles multiple arguments', t => { | ||
let callCount = 0 | ||
const add = (...args) => { | ||
callCount++ | ||
return args.reduce((acc, val) => acc + val, 0) | ||
} | ||
const memoizedAdd = memoizeWith((...args) => args.join('-'), add) | ||
|
||
memoizedAdd(1, 2, 3) | ||
memoizedAdd(1, 2, 3) | ||
memoizedAdd(1, 2, 3) | ||
memoizedAdd(1, 2, 3) | ||
|
||
t.equal(callCount, 1, 'Function should be called once due to caching') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memozieWith - can be applied to a nullary function', t => { | ||
let callCount = 0 | ||
const get = () => { | ||
callCount++ | ||
return 'value' | ||
} | ||
const memoizedGet = memoizeWith(() => 'key', get) | ||
|
||
t.equal(memoizedGet(), 'value', 'Function should return the correct value') | ||
t.equal(memoizedGet(), 'value', 'Function should return the correct value') | ||
t.equal(memoizedGet(), 'value', 'Function should return the correct value') | ||
t.equal(callCount, 1, 'Function should be called once due to caching') | ||
|
||
t.end() | ||
}) | ||
|
||
test('memoizeWith - can be applied to a function with optional arguments', t => { | ||
let callCount = 0 | ||
const add = (a, b = 0) => { | ||
callCount++ | ||
return a + b | ||
} | ||
const memoizedAdd = memoizeWith((a, b) => `${a}-${b}`, add) | ||
|
||
memoizedAdd(1) | ||
memoizedAdd(1) | ||
memoizedAdd(1, 2) | ||
memoizedAdd(1, 2) | ||
|
||
t.equal(callCount, 2, 'Function should be called twice due to caching') | ||
|
||
t.end() | ||
}) |
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