Comparing version
29
index.js
@@ -5,6 +5,33 @@ 'use strict'; | ||
var mod; | ||
return function () { | ||
return mod !== undefined ? mod : mod = fn(id); | ||
if (!arguments.length) { | ||
mod = lazy(mod, fn, id); | ||
return mod; | ||
} | ||
var ret = {}; | ||
[].forEach.call(arguments, function (prop) { | ||
Object.defineProperty(ret, prop, { | ||
get: function () { | ||
mod = lazy(mod, fn, id); | ||
if (typeof mod[prop] === 'function') { | ||
return function () { | ||
return mod[prop].apply(mod, arguments); | ||
}; | ||
} | ||
return mod[prop]; | ||
} | ||
}); | ||
}); | ||
return ret; | ||
}; | ||
}; | ||
function lazy(mod, fn, id) { | ||
return mod !== undefined ? mod : fn(id); | ||
} | ||
}; |
{ | ||
"name": "lazy-req", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Require modules lazily", | ||
@@ -12,2 +12,8 @@ "license": "MIT", | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Jorge Bucaran", | ||
"email": "jbucaran@me.com" | ||
} | ||
], | ||
"engines": { | ||
@@ -17,3 +23,3 @@ "node": ">=0.10.0" | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "xo && node test.js" | ||
}, | ||
@@ -34,4 +40,5 @@ "files": [ | ||
"devDependencies": { | ||
"ava": "0.0.3" | ||
"ava": "0.0.4", | ||
"xo": "*" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # lazy-req [](https://travis-ci.org/sindresorhus/lazy-req) | ||
```sh | ||
``` | ||
$ npm install --save lazy-req | ||
@@ -19,3 +19,3 @@ ``` | ||
var lazyReq = require('lazy-req')(require); | ||
var lodash = require('lodash'); | ||
var _ = lazyReq('lodash'); | ||
@@ -30,2 +30,13 @@ // where you would normally do | ||
_().isString('unicorn'); | ||
// extract lazy variations of the props you need | ||
var members = lazyReq('lodash')('isNumber', 'isString'); | ||
// useful when using destructuring assignment in ES2015 | ||
const { isNumber, isString } = lazyReq('lodash')('isNumber', 'isString'); | ||
// works out of the box for functions and regular properties | ||
var stuff = lazyReq('./math-lib')('sum', 'PHI'); | ||
console.log(stuff.sum(1, 2)); // => 3 | ||
console.log(stuff.PHI); // => 1.618033 | ||
``` | ||
@@ -32,0 +43,0 @@ |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3467
159.12%4
33.33%30
233.33%45
32.35%0
-100%2
100%