denim-memoize
Advanced tools
Comparing version 1.0.1 to 1.0.2
10
index.js
@@ -1,10 +0,8 @@ | ||
export default (fn) => { | ||
export default fn => { | ||
const cache = {} | ||
return (...fnArgs) => { | ||
const cacheKey = fnArgs.toString() | ||
if(typeof cache[cacheKey] === 'undefined'){ | ||
let result = fn(...fnArgs) | ||
cache[cacheKey] = result | ||
if (typeof cache[cacheKey] === 'undefined') { | ||
let result = fn(...fnArgs) | ||
cache[cacheKey] = result | ||
} | ||
@@ -11,0 +9,0 @@ return cache[cacheKey] |
import { expect, assert } from 'chai' | ||
import memoizeFactory from './index' | ||
describe('memoizeFactory', ()=>{ | ||
it('should return a function', ()=>{ | ||
const result = memoizeFactory(()=>{}) | ||
describe('memoizeFactory', () => { | ||
it('should return a function', () => { | ||
const result = memoizeFactory(() => {}) | ||
assert.isFunction(result) | ||
}) | ||
it('should return memoized result of function', ()=> { | ||
it('should return memoized result of function', () => { | ||
const result_gen = () => Math.floor(Math.random() * 100000) + 1 | ||
@@ -24,5 +24,5 @@ const functionToMemoize = () => result_gen() | ||
it('should return memoized result of function with parameters', ()=> { | ||
it('should return memoized result of function with parameters', () => { | ||
const result_gen = () => Math.floor(Math.random() * 100000) + 1 | ||
const functionToMemoize = (x) => result_gen() | ||
const functionToMemoize = x => result_gen() | ||
const nonMemoizeResult1 = functionToMemoize(1) | ||
@@ -38,2 +38,18 @@ const nonMemoizeResult2 = functionToMemoize(1) | ||
}) | ||
it('should return memoized result of async funcition', done => { | ||
const result_gen = () => Math.floor(Math.random() * 100000) + 1 | ||
const functionToMemoize = async x => result_gen() | ||
const nonMemoizeResult1 = functionToMemoize(1) | ||
const nonMemoizeResult2 = functionToMemoize(1) | ||
const memoizedFunction = memoizeFactory(functionToMemoize) | ||
const memoizeResult1 = memoizedFunction(1) | ||
const memoizeResult2 = memoizedFunction(1) | ||
Promise.all([memoizeResult1, memoizeResult2]).then(results => { | ||
assert.notEqual(nonMemoizeResult1, nonMemoizeResult2) | ||
assert.strictEqual(results[0], results[1]) | ||
done() | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "denim-memoize", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Utility to memoize functions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
19836
14
73