lazy-cache
Advanced tools
Comparing version 1.0.1 to 1.0.2
25
index.js
@@ -7,12 +7,10 @@ 'use strict'; | ||
* ```js | ||
* var lazy = require('lazy-cache')(require); | ||
* var utils = require('lazy-cache')(require); | ||
* // cache the call to `require('ansi-yellow')` | ||
* lazy('ansi-yellow', 'yellow'); | ||
* utils('ansi-yellow', 'yellow'); | ||
* // use `ansi-yellow` | ||
* console.log(lazy.yellow('this is yellow')); | ||
* console.log(utils.yellow('this is yellow')); | ||
* ``` | ||
* | ||
* @param {Function} `fn` Function that will be called only once. | ||
* @param {Object} `options` Options to determine how modules are cached. | ||
* @param {Boolean} `options.unlazy` When set to `true`, `fn` is called immediately. Defaults to `false`. | ||
* @return {Function} Function that can be called to get the cached function | ||
@@ -22,8 +20,9 @@ * @api public | ||
function lazyCache(fn, opts) { | ||
opts = opts || {}; | ||
function lazyCache(fn) { | ||
var cache = {}; | ||
var proxy = function (mod, name) { | ||
var proxy = function(mod, name) { | ||
name = name || camelcase(mod); | ||
if (opts.unlazy === true) { | ||
// check both boolean and string in case `process.env` cases to string | ||
if (process.env.UNLAZY === 'true' || process.env.UNLAZY === true) { | ||
cache[name] = fn(mod); | ||
@@ -38,3 +37,3 @@ } | ||
function getter () { | ||
function getter() { | ||
if (cache.hasOwnProperty(name)) { | ||
@@ -58,5 +57,7 @@ return cache[name]; | ||
function camelcase(str) { | ||
if (str.length === 1) { return str.toLowerCase(); } | ||
if (str.length === 1) { | ||
return str.toLowerCase(); | ||
} | ||
str = str.replace(/^[\W_]+|[\W_]+$/g, '').toLowerCase(); | ||
return str.replace(/[\W_]+(\w|$)/g, function (_, ch) { | ||
return str.replace(/[\W_]+(\w|$)/g, function(_, ch) { | ||
return ch.toUpperCase(); | ||
@@ -63,0 +64,0 @@ }); |
{ | ||
"name": "lazy-cache", | ||
"description": "Cache requires to be lazy-loaded when needed.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"homepage": "https://github.com/jonschlinkert/lazy-cache", | ||
@@ -37,3 +37,2 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"verb": { | ||
"plugins": ["gulp-format-md"], | ||
"related": { | ||
@@ -43,4 +42,7 @@ "list": [ | ||
] | ||
} | ||
}, | ||
"plugins": [ | ||
"gulp-format-md" | ||
] | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# lazy-cache [![NPM version](https://img.shields.io/npm/v/lazy-cache.svg)](https://www.npmjs.com/package/lazy-cache) | ||
# lazy-cache [![NPM version](https://img.shields.io/npm/v/lazy-cache.svg)](https://www.npmjs.com/package/lazy-cache) [![Build Status](https://img.shields.io/travis/jonschlinkert/lazy-cache.svg)](https://travis-ci.org/jonschlinkert/lazy-cache) | ||
@@ -67,7 +67,7 @@ > Cache requires to be lazy-loaded when needed. | ||
```js | ||
var lazy = require('lazy-cache')(require); | ||
var utils = require('lazy-cache')(require); | ||
// temporarily re-assign `require` to trick browserify | ||
var fn = require; | ||
require = lazy; | ||
// list module dependencies here (`require` is actually `lazy`) | ||
require = utils; | ||
// list module dependencies (here, `require` is actually `lazy-cache`) | ||
require('glob'); | ||
@@ -77,10 +77,10 @@ require = fn; // restore the native `require` function | ||
/** | ||
* Now you can use glob with the `lazy.glob` variable | ||
* Now you can use glob with the `utils.glob` variable | ||
*/ | ||
// sync | ||
console.log(lazy.glob.sync('*.js')); | ||
console.log(utils.glob.sync('*.js')); | ||
// async | ||
lazy.glob('*.js', function (err, files) { | ||
utils.glob('*.js', function (err, files) { | ||
console.log(files.join('\n')); | ||
@@ -90,2 +90,12 @@ }); | ||
## Kill switch | ||
In certain rare edge cases, it may be necessary to unlazy all lazy-cached dependencies (two reported cases out of > 9 million downloads). | ||
To force lazy-cache to immediately invoke all dependencies, do: | ||
```js | ||
process.env.UNLAZY = true; | ||
``` | ||
## Related | ||
@@ -121,2 +131,2 @@ | ||
_This file was generated by [verb](https://github.com/verbose/verb) on December 08, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb) on December 09, 2015._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6503
128