aboutsummaryrefslogtreecommitdiff
path: root/node_modules/lazy-cache/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-08-14 05:01:11 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-08-14 05:02:09 +0200
commit363723fc84f7b8477592e0105aeb331ec9a017af (patch)
tree29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/lazy-cache/index.js
parent5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff)
downloadwallet-core-363723fc84f7b8477592e0105aeb331ec9a017af.tar.xz
node_modules
Diffstat (limited to 'node_modules/lazy-cache/index.js')
-rw-r--r--node_modules/lazy-cache/index.js48
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) {