Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lazy-cache

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazy-cache - npm Package Compare versions

Comparing version 1.0.4 to 2.0.0

91

index.js

@@ -19,36 +19,87 @@ 'use strict';

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;
// camel-case the module `name` if `alias` is not defined
if (typeof key !== 'string') {
key = camelcase(name);
}
Object.defineProperty(proxy, name, {
enumerable: true,
configurable: true,
get: getter
});
// 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();
}
defineGetter(proxy, key, getter);
return getter;
};
return proxy;
}
/**
* Used to camelcase the name to be stored on the `lazy` object.
* Return true if `process.env.LAZY` is true, or travis is running.
*/
function unlazy(env) {
return env.UNLAZY === 'true' || env.UNLAZY === true || env.TRAVIS;
}
/**
* Define a `getter` function for `prop` on the `cache` object. Dot notation is
* supported.
*
* @param {String} `str` String containing `_`, `.`, `-` or whitespace that will be camelcased.
* @return {String} camelcased string.
* @param {Object} `cache`
* @param {String} `prop`
* @param {Function} `getter` Getter function
* @return {Object}
*/
function defineGetter(cache, prop, getter) {
if (!~prop.indexOf('.')) {
defineProperty(cache, prop, getter);
return cache;
}
var keys = prop.split('.');
var last = keys.pop();
var proxy = cache;
var key;
while ((key = keys.shift())) {
while (key.slice(-1) === '\\') {
key = key.slice(0, -1) + '.' + keys.shift();
}
proxy = proxy[key] || (proxy[key] = {});
}
defineProperty(proxy, last, getter);
return cache;
}
/**
* Define a `getter` function for `prop` on the `obj`.
*
* @param {Object} `obj`
* @param {String} `prop`
* @param {Function} `getter` Getter function
*/
function defineProperty(obj, prop, getter) {
Object.defineProperty(obj, prop, {
configurable: true,
enumerable: true,
get: getter
});
}
/**
* Camelcase the the given module `name`.
*/
function camelcase(str) {

@@ -55,0 +106,0 @@ if (str.length === 1) {

{
"name": "lazy-cache",
"description": "Cache requires to be lazy-loaded when needed.",
"version": "1.0.4",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/lazy-cache",

@@ -23,6 +23,10 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"devDependencies": {
"ansi-cyan": "^0.1.1",
"ansi-magenta": "^0.1.1",
"ansi-yellow": "^0.1.1",
"glob": "^7.0.3",
"gulp-format-md": "^0.1.8",
"mocha": "^2.4.5"
"mocha": "^2.4.5",
"object.omit": "^2.0.0",
"object.pick": "^1.1.2"
},

@@ -29,0 +33,0 @@ "keywords": [

@@ -13,4 +13,6 @@ # lazy-cache [![NPM version](https://img.shields.io/npm/v/lazy-cache.svg?style=flat)](https://www.npmjs.com/package/lazy-cache) [![NPM downloads](https://img.shields.io/npm/dm/lazy-cache.svg?style=flat)](https://npmjs.org/package/lazy-cache) [![Build Status](https://img.shields.io/travis/jonschlinkert/lazy-cache.svg?style=flat)](https://travis-ci.org/jonschlinkert/lazy-cache)

If you use webpack and are experiencing issues, try using [unlazy-loader](https://github.com/doowb/unlazy-loader), a webpack loader that fixes the bug that prevents webpack from working with native javascript getters.
**webpack users**
If you use webpack and are experiencing issues, try using [unlazy-loader](https://github.com/doowb/unlazy-loader), a webpack loader that _fixes the webpack bug_ that prevents webpack from working with native javascript getters.
## Usage

@@ -66,2 +68,16 @@

Dot notation may also be used in the alias to create an object hierarchy.
**Example**
```js
var utils = require('lazy-cache')(require);
utils('ansi-cyan', 'color.cyan');
utils('ansi-yellow', 'color.yellow');
utils('ansi-magenta', 'color.magenta');
console.log(utils.color.cyan('foo'));
console.log(utils.color.yellow('bar'));
console.log(utils.color.magenta('baz'));
```
## Browserify usage

@@ -149,2 +165,2 @@

_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 22, 2016._
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 28, 2016._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc