memo-cache
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "memo-cache", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/mrodrig/memo-cache", | ||
@@ -5,0 +5,0 @@ "moduleType": [ |
@@ -48,4 +48,9 @@ 'use strict'; | ||
*/ | ||
memoHashFunction: function (args) { | ||
return args.length ? args[0].toString() : '__noArgs'; | ||
memoHashFunction: function (arg1) { | ||
if (arg1 === null) { | ||
return 'null'; | ||
} else if (arg1 !== undefined) { | ||
return arg1.toString(); | ||
} | ||
return '__noArgs'; | ||
} | ||
@@ -121,3 +126,3 @@ }; | ||
// Generate the hashValue, or key to store the function result at | ||
hashValue = arguments.length > 1 ? options.memoHashFunction.apply(this, arguments) : hashValue; | ||
hashValue = options.memoHashFunction.apply(this, arguments); | ||
// If the cache does not already have the stored result, compute it and store it | ||
@@ -124,0 +129,0 @@ if (!fnCache.exists(hashValue)) { |
@@ -5,3 +5,3 @@ { | ||
"description": "A memoization and caching library for NodeJS", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -35,3 +35,3 @@ var should = require('should'), | ||
}, | ||
customOptions = { | ||
customSingleOptions = { | ||
cloneValues : true, // Should values be cloned before storing and before returning them to the caller | ||
@@ -44,5 +44,27 @@ maxSize : 5, // The maximum number of keys that should be kept in the cache | ||
*/ | ||
memoHashFunction: function (args) { | ||
return args.length ? args[0].toString() : '__noArgs'; | ||
memoHashFunction: function (arg1) { | ||
if (arg1 === null) { | ||
return 'null'; | ||
} else if (arg1 !== undefined) { | ||
return arg1.toString(); | ||
} | ||
return '__noArgs'; | ||
} | ||
}, | ||
customObjectOptions = { | ||
cloneValues : true, // Should values be cloned before storing and before returning them to the caller | ||
maxSize : 5, // The maximum number of keys that should be kept in the cache | ||
/** | ||
* Function which maps the input arguments for memoization to a unique string for storing the result in the cache | ||
* @param args Array argument array | ||
* @returns {string} key to be used to store the result in the function cache | ||
*/ | ||
memoHashFunction: function (arg1) { | ||
if (arg1 === null) { | ||
return 'null'; | ||
} else if (arg1 !== undefined) { | ||
return arg1.val.toString(); | ||
} | ||
return '__noArgs'; | ||
} | ||
}; | ||
@@ -69,7 +91,7 @@ | ||
// Cache Key: __function1 | ||
testFunctionSingleParamCustomMemoized = memoCache.memoize(testFunctionSingleParam, customOptions); | ||
testFunctionSingleParamCustomMemoized = memoCache.memoize(testFunctionSingleParam, customSingleOptions); | ||
// Cache Key: __function2 | ||
testFunctionObjectParamDefaultMemoized = memoCache.memoize(testFunctionObjectParam); // defaultOptions | ||
// Cache Key: __function3 | ||
testFunctionObjectParamCustomMemoized = memoCache.memoize(testFunctionObjectParam, customOptions); | ||
testFunctionObjectParamCustomMemoized = memoCache.memoize(testFunctionObjectParam, customObjectOptions); | ||
@@ -222,3 +244,3 @@ }); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
tmp.should.equal(customOptions.maxSize); | ||
tmp.should.equal(customSingleOptions.maxSize); | ||
done(); | ||
@@ -238,3 +260,3 @@ }); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
tmp.should.equal(customOptions.maxSize); | ||
tmp.should.equal(customSingleOptions.maxSize); | ||
done(); | ||
@@ -254,3 +276,3 @@ }); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionObjectParamDefaultMemoized('test'); | ||
testFunctionObjectParamDefaultMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
@@ -264,7 +286,7 @@ var tmp = testFunctionObjectParamDefaultMemoized.size(); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionObjectParamDefaultMemoized('test'); | ||
testFunctionObjectParamDefaultMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
var tmp = testFunctionObjectParamDefaultMemoized.size(); | ||
tmp.should.equal(1); | ||
testFunctionObjectParamDefaultMemoized('test'); | ||
testFunctionObjectParamDefaultMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
@@ -287,3 +309,3 @@ done(); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionObjectParamDefaultMemoized('test'); | ||
testFunctionObjectParamDefaultMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
@@ -304,8 +326,8 @@ var tmp = testFunctionObjectParamDefaultMemoized.size(); | ||
var tmp = cacheMissCounter; | ||
testFunctionObjectParamDefaultMemoized(num.toString()); | ||
cacheMissCounter.should.equal(tmp+1); | ||
testFunctionObjectParamDefaultMemoized({val: num.toString()}); | ||
cacheMissCounter.should.equal(1); // Default toString returns [object Object] for hash | ||
}); | ||
cacheMissCounter.should.equal(NUM_ITERATIONS); | ||
cacheMissCounter.should.equal(1); | ||
var tmp = testFunctionObjectParamDefaultMemoized.size(); | ||
tmp.should.equal(NUM_ITERATIONS); | ||
tmp.should.equal(1); | ||
done(); | ||
@@ -320,8 +342,8 @@ }); | ||
sleep(1); // sleep 1 millisecond | ||
testFunctionObjectParamDefaultMemoized(num.toString()); | ||
cacheMissCounter.should.equal(tmp+1); | ||
testFunctionObjectParamDefaultMemoized({val: num.toString()}); | ||
cacheMissCounter.should.equal(1); | ||
}); | ||
cacheMissCounter.should.equal(NUM_ITERATIONS); | ||
cacheMissCounter.should.equal(1); // Default toString returns [object Object] hash, only 1 val cached | ||
var tmp = testFunctionObjectParamDefaultMemoized.size(); | ||
tmp.should.equal(NUM_ITERATIONS); | ||
tmp.should.equal(1); | ||
done(); | ||
@@ -331,6 +353,6 @@ }); | ||
describe('Single Param - Custom Options', function () { | ||
describe('Object Param - Custom Options', function () { | ||
it('should have an empty cache initially', function (done) { | ||
cacheMissCounter.should.equal(0); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(0); | ||
@@ -342,5 +364,5 @@ done(); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionSingleParamCustomMemoized('test'); | ||
testFunctionObjectParamCustomMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(1); | ||
@@ -352,7 +374,7 @@ done(); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionSingleParamCustomMemoized('test'); | ||
testFunctionObjectParamCustomMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(1); | ||
testFunctionSingleParamCustomMemoized('test'); | ||
testFunctionObjectParamCustomMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
@@ -364,6 +386,6 @@ done(); | ||
cacheMissCounter.should.equal(0); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(0); | ||
testFunctionSingleParamCustomMemoized.clear(); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
testFunctionObjectParamCustomMemoized.clear(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(0); | ||
@@ -376,8 +398,8 @@ cacheMissCounter.should.equal(0); | ||
cacheMissCounter.should.equal(0); | ||
testFunctionSingleParamCustomMemoized('test'); | ||
testFunctionObjectParamCustomMemoized({val : 'test'}); | ||
cacheMissCounter.should.equal(1); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(1); | ||
testFunctionSingleParamCustomMemoized.clear(); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
testFunctionObjectParamCustomMemoized.clear(); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(0); | ||
@@ -393,8 +415,8 @@ cacheMissCounter.should.equal(1); | ||
var tmp = cacheMissCounter; | ||
testFunctionSingleParamCustomMemoized(num.toString()); | ||
testFunctionObjectParamCustomMemoized({val:num.toString()}); | ||
cacheMissCounter.should.equal(tmp+1); | ||
}); | ||
cacheMissCounter.should.equal(NUM_ITERATIONS); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
tmp.should.equal(customOptions.maxSize); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(customObjectOptions.maxSize); | ||
done(); | ||
@@ -409,8 +431,8 @@ }); | ||
sleep(1); // sleep 1 millisecond | ||
testFunctionSingleParamCustomMemoized(num.toString()); | ||
testFunctionObjectParamCustomMemoized({val:num.toString()}); | ||
cacheMissCounter.should.equal(tmp+1); | ||
}); | ||
cacheMissCounter.should.equal(NUM_ITERATIONS); | ||
var tmp = testFunctionSingleParamCustomMemoized.size(); | ||
tmp.should.equal(customOptions.maxSize); | ||
var tmp = testFunctionObjectParamCustomMemoized.size(); | ||
tmp.should.equal(customObjectOptions.maxSize); | ||
done(); | ||
@@ -417,0 +439,0 @@ }); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63406
11
1180
0