memoize-fs
Advanced tools
Comparing version 0.0.0 to 0.0.1
13
index.js
@@ -8,3 +8,3 @@ 'use strict'; | ||
path = require('path'), | ||
rmdir = require('rimraf'), | ||
rmdir = require('rimraf'), | ||
crypto = require('crypto'); | ||
@@ -38,6 +38,15 @@ | ||
/* jshint unused: vars */ | ||
var fnJson = JSON.stringify(args, function (name, value) { | ||
var circRefColl = [], | ||
fnJson = JSON.stringify(args, function (name, value) { | ||
if (typeof value === 'function') { | ||
return value; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference found, discard key | ||
return; | ||
} | ||
// store value in collection | ||
circRefColl.push(value); | ||
} | ||
return value; | ||
@@ -44,0 +53,0 @@ }), |
{ | ||
"name": "memoize-fs", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "memoize/cache in file system solution for Node.js", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -147,2 +147,21 @@ 'use strict'; | ||
it('should save the result of a memoized function on first execution to its cache folder with salt', function (done) { | ||
var cachePath = path.join(__dirname, '../../build/cache'), | ||
memoize = memoizeFs({ cachePath: cachePath }), | ||
c = 3; | ||
memoize.fn(function (a, b) { return a + b + c; }, { cacheId: 'foobar', salt: 'qux' }).then(function (memFn) { | ||
memFn(1, 2).then(function (result) { | ||
assert.strictEqual(result, 6, 'expected result to strictly equal 6'); | ||
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar'); | ||
done(); | ||
} | ||
}); | ||
}, done); | ||
}, done); | ||
}); | ||
it('should save the result of a memoized function on first execution to the root cache folder if no cache id is provided', function (done) { | ||
@@ -305,3 +324,3 @@ var cachePath = path.join(__dirname, '../../build/cache'), | ||
it('should return the cached truthy result of type object of a previously memoized function', function (done) { | ||
it('should return the cached result of type object of a previously memoized function', function (done) { | ||
var cachePath = path.join(__dirname, '../../build/cache'), | ||
@@ -329,2 +348,26 @@ memoize = memoizeFs({ cachePath: cachePath }), | ||
it('should return the cached result of type object of a previously memoized function with a circular reference object as function argument', function (done) { | ||
var cachePath = path.join(__dirname, '../../build/cache'), | ||
memoize = memoizeFs({ cachePath: cachePath }), | ||
c = 3, | ||
circRefObj = { h: circRefObj }; | ||
memoize.fn(function (a, b, cro) { return { a: a, b: b, c: c, d : { e: [3, 2, 1], f: null, g: 'qux' } }; }, { cacheId: 'foobar' }).then(function (memFn) { | ||
memFn(1, 2, circRefObj).then(function (result) { | ||
assert.deepEqual(result, { a: 1, b: 2, c: 3, d : { e: [3, 2, 1], f: null, g: 'qux' } }, 'expected result to deeply equal the one provided'); | ||
c = 999; | ||
memFn(1, 2, circRefObj).then(function (result) { | ||
assert.deepEqual(result, { a: 1, b: 2, c: 3, d : { e: [3, 2, 1], f: null, g: 'qux' } }, 'expected result to deeply equal the one provided'); | ||
fs.readdir(path.join(cachePath, 'foobar'), function (err, files) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
assert.strictEqual(files.length, 1, 'expected exactly one file in cache with id foobar'); | ||
done(); | ||
} | ||
}); | ||
}, done); | ||
}, done); | ||
}, done); | ||
}); | ||
it('should return the cached result of a previously memoized promisified async function', function (done) { | ||
@@ -331,0 +374,0 @@ var cachePath = path.join(__dirname, '../../build/cache'), |
Sorry, the diff of this file is not supported yet
85362
8063
648