Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / lazy-cache / index.js
1 'use strict';
2
3 /**
4  * Cache results of the first function call to ensure only calling once.
5  *
6  * ```js
7  * var utils = require('lazy-cache')(require);
8  * // cache the call to `require('ansi-yellow')`
9  * utils('ansi-yellow', 'yellow');
10  * // use `ansi-yellow`
11  * console.log(utils.yellow('this is yellow'));
12  * ```
13  *
14  * @param  {Function} `fn` Function that will be called only once.
15  * @return {Function} Function that can be called to get the cached function
16  * @api public
17  */
18
19 function lazyCache(fn) {
20   var cache = {};
21   var proxy = function(mod, name) {
22     name = name || camelcase(mod);
23
24     // check both boolean and string in case `process.env` cases to string
25     if (process.env.UNLAZY === 'true' || process.env.UNLAZY === true || process.env.TRAVIS) {
26       cache[name] = fn(mod);
27     }
28
29     Object.defineProperty(proxy, name, {
30       enumerable: true,
31       configurable: true,
32       get: getter
33     });
34
35     function getter() {
36       if (cache.hasOwnProperty(name)) {
37         return cache[name];
38       }
39       return (cache[name] = fn(mod));
40     }
41     return getter;
42   };
43   return proxy;
44 }
45
46 /**
47  * Used to camelcase the name to be stored on the `lazy` object.
48  *
49  * @param  {String} `str` String containing `_`, `.`, `-` or whitespace that will be camelcased.
50  * @return {String} camelcased string.
51  */
52
53 function camelcase(str) {
54   if (str.length === 1) {
55     return str.toLowerCase();
56   }
57   str = str.replace(/^[\W_]+|[\W_]+$/g, '').toLowerCase();
58   return str.replace(/[\W_]+(\w|$)/g, function(_, ch) {
59     return ch.toUpperCase();
60   });
61 }
62
63 /**
64  * Expose `lazyCache`
65  */
66
67 module.exports = lazyCache;