diff --git a/src/index.js b/src/index.js index 0f2a4ac..cc384a6 100644 --- a/src/index.js +++ b/src/index.js @@ -12,17 +12,17 @@ const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ]; const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents))); const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents))); const cache = fn => { - let cache = Object.create(null); - const wrapped = (s, cb = false) => { - if (s in cache === false) { + const cache = new Map(); + const wrapped = (s, cb) => { + if (cache.has(s) === false) { cache[s] = fn(s).catch(err => { - delete cache[s]; + cache.delete(s); throw err; }); } - return cb ? cache[s].then(v => cb(null, v), cb) : cache[s]; + return cache.get(s).then(v => cb(null, v), cb); }; - wrapped.clear = () => { cache = {}; }; + wrapped.clear = () => cache.clear(); return wrapped; }; const ignoreENOENT = err => {