diff options
Diffstat (limited to 'node_modules/lazy-cache/index.js')
-rw-r--r-- | node_modules/lazy-cache/index.js | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/node_modules/lazy-cache/index.js b/node_modules/lazy-cache/index.js index da7897d0c..751fcbb5c 100644 --- a/node_modules/lazy-cache/index.js +++ b/node_modules/lazy-cache/index.js @@ -1,5 +1,7 @@ 'use strict'; +var set = require('set-getter'); + /** * Cache results of the first function call to ensure only calling once. * @@ -16,38 +18,42 @@ * @api public */ -function lazyCache(fn) { +function lazyCache(requireFn) { var cache = {}; - var proxy = function(mod, name) { - name = name || camelcase(mod); - // check both boolean and string in case `process.env` cases to string - if (process.env.UNLAZY === 'true' || process.env.UNLAZY === true || process.env.TRAVIS) { - cache[name] = fn(mod); - } + return function proxy(name, alias) { + var key = alias; - Object.defineProperty(proxy, name, { - enumerable: true, - configurable: true, - get: getter - }); + // camel-case the module `name` if `alias` is not defined + if (typeof key !== 'string') { + key = camelcase(name); + } + // create a getter to lazily invoke the module the first time it's called function getter() { - if (cache.hasOwnProperty(name)) { - return cache[name]; - } - return (cache[name] = fn(mod)); + return cache[key] || (cache[key] = requireFn(name)); } + + // trip the getter if `process.env.UNLAZY` is defined + if (unlazy(process.env)) { + getter(); + } + + set(proxy, key, getter); return getter; }; - return proxy; } /** - * Used to camelcase the name to be stored on the `lazy` object. - * - * @param {String} `str` String containing `_`, `.`, `-` or whitespace that will be camelcased. - * @return {String} camelcased string. + * Return true if `process.env.LAZY` is true, or travis is running. + */ + +function unlazy(env) { + return env.UNLAZY === 'true' || env.UNLAZY === true || env.TRAVIS; +} + +/** + * Camelcase the the given module `name`. */ function camelcase(str) { |