diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index 7d69d74..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' - - '4' diff --git a/index.js b/index.js index 42dc788..5c29152 100644 --- a/index.js +++ b/index.js @@ -4,47 +4,54 @@ const isPromise = require('p-is-promise'); const cacheStore = new WeakMap(); -const defaultCacheKey = function (x) { - if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) { - return x; +const defaultCacheKey = (...args) => { + if (args.length === 1) { + const [firstArgument] = args; + if ( + firstArgument === null || + firstArgument === undefined || + (typeof firstArgument !== 'function' && typeof firstArgument !== 'object') + ) { + return firstArgument; + } } - return JSON.stringify(arguments); + return JSON.stringify(args); }; -module.exports = (fn, opts) => { - opts = Object.assign({ +module.exports = (fn, options) => { + options = Object.assign({ cacheKey: defaultCacheKey, cache: new Map(), cachePromiseRejection: false - }, opts); + }, options); - const memoized = function () { + const memoized = function (...args) { const cache = cacheStore.get(memoized); - const key = opts.cacheKey.apply(null, arguments); + const key = options.cacheKey(...args); if (cache.has(key)) { const c = cache.get(key); - if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) { + if (typeof options.maxAge !== 'number' || Date.now() < c.maxAge) { return c.data; } cache.delete(key); } - const ret = fn.apply(this, arguments); + const ret = fn.call(this, ...args); const setData = (key, data) => { cache.set(key, { data, - maxAge: Date.now() + (opts.maxAge || 0) + maxAge: Date.now() + (options.maxAge || 0) }); }; setData(key, ret); - if (isPromise(ret) && opts.cachePromiseRejection === false) { + if (isPromise(ret) && options.cachePromiseRejection === false) { // Remove rejected promises from cache unless `cachePromiseRejection` is set to `true` ret.catch(() => cache.delete(key)); } @@ -54,7 +61,7 @@ module.exports = (fn, opts) => { mimicFn(memoized, fn); - cacheStore.set(memoized, opts.cache); + cacheStore.set(memoized, options.cache); return memoized; }; diff --git a/package.json b/package.json index bee8462..0ace1d7 100644 --- a/package.json +++ b/package.json @@ -1,43 +1,43 @@ { - "name": "mem", - "version": "3.0.1", - "description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", - "license": "MIT", - "repository": "sindresorhus/mem", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "memoize", - "function", - "mem", - "memoization", - "cache", - "caching", - "optimize", - "performance", - "ttl", - "expire", - "promise" - ], - "dependencies": { - "mimic-fn": "^1.0.0", - "p-is-promise": "^1.1.0" - }, - "devDependencies": { - "ava": "*", - "delay": "^2.0.0", - "xo": "*" - } + "name": "mem", + "version": "3.0.1", + "description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", + "license": "MIT", + "repository": "sindresorhus/mem", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "memoize", + "function", + "mem", + "memoization", + "cache", + "caching", + "optimize", + "performance", + "ttl", + "expire", + "promise" + ], + "dependencies": { + "mimic-fn": "^1.0.0", + "p-is-promise": "^1.1.0" + }, + "devDependencies": { + "ava": "*", + "delay": "^3.0.0", + "xo": "*" + } } diff --git a/readme.md b/readme.md index ee9d89e..72575a7 100644 --- a/readme.md +++ b/readme.md @@ -40,35 +40,37 @@ memoized('bar'); const mem = require('mem'); let i = 0; -const counter = () => Promise.resolve(++i); +const counter = async () => ++i; const memoized = mem(counter); -memoized().then(a => { - console.log(a); +(async () => { + console.log(await memoized()); //=> 1 - memoized().then(b => { - // The return value didn't increase as it's cached - console.log(b); - //=> 1 - }); -}); + // The return value didn't increase as it's cached + console.log(await memoized()); + //=> 1 +})(); ``` ```js const mem = require('mem'); const got = require('got'); +const delay = require('delay'); + const memGot = mem(got, {maxAge: 1000}); -memGot('sindresorhus.com').then(() => { +(async () => { + await memGot('sindresorhus.com'); + // This call is cached - memGot('sindresorhus.com').then(() => { - setTimeout(() => { - // This call is not cached as the cache has expired - memGot('sindresorhus.com').then(() => {}); - }, 2000); - }); -}); + await memGot('sindresorhus.com'); + + await delay(2000); + + // This call is not cached as the cache has expired + await memGot('sindresorhus.com'); +})(); ``` @@ -84,6 +86,8 @@ Function to be memoized. #### options +Type: `Object` + ##### maxAge Type: `number`
@@ -140,12 +144,14 @@ const got = require('got'); const cache = new StatsMap(); const memGot = mem(got, {cache}); -memGot('sindresorhus.com') - .then(() => memGot('sindresorhus.com')) - .then(() => memGot('sindresorhus.com')); +(async () => { + await memGot('sindresorhus.com'); + await memGot('sindresorhus.com'); + await memGot('sindresorhus.com'); -console.log(cache.stats); -//=> {hits: 2, misses: 1} + console.log(cache.stats); + //=> {hits: 2, misses: 1} +})(); ```