import-lazy
Advanced tools
Comparing version 2.1.0 to 3.0.0
32
index.js
@@ -8,34 +8,2 @@ 'use strict'; | ||
return function () { | ||
if (arguments.length === 0) { | ||
mod = lazy(mod, fn, id); | ||
return mod; | ||
} | ||
const ret = {}; | ||
[].forEach.call(arguments, prop => { | ||
Object.defineProperty(ret, prop, { | ||
get: () => { | ||
mod = lazy(mod, fn, id); | ||
if (typeof mod[prop] === 'function') { | ||
return function () { | ||
return mod[prop].apply(mod, arguments); | ||
}; | ||
} | ||
return mod[prop]; | ||
} | ||
}); | ||
}); | ||
return ret; | ||
}; | ||
}; | ||
}; | ||
module.exports.proxy = fn => { | ||
return id => { | ||
let mod; | ||
const handler = { | ||
@@ -42,0 +10,0 @@ get: (target, property) => { |
{ | ||
"name": "import-lazy", | ||
"version": "2.1.0", | ||
"description": "Import modules lazily", | ||
"version": "3.0.0", | ||
"description": "Import a module lazily", | ||
"license": "MIT", | ||
@@ -19,3 +19,3 @@ "repository": "sindresorhus/import-lazy", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
@@ -22,0 +22,0 @@ "scripts": { |
# import-lazy [![Build Status](https://travis-ci.org/sindresorhus/import-lazy.svg?branch=master)](https://travis-ci.org/sindresorhus/import-lazy) | ||
> Import modules lazily | ||
> Import a module lazily | ||
Note: Version 3 is exclusively `Proxy`-based and requires Node.js 6+. Use [version 2](https://github.com/sindresorhus/import-lazy/tree/ed6c2fac31aaf8a7d91a27295756383487f3965d) if you need Node.js <=5 support. | ||
## Install | ||
``` | ||
$ npm install --save import-lazy | ||
$ npm install import-lazy | ||
``` | ||
@@ -20,19 +22,13 @@ | ||
// Where you would normally do | ||
// Instead of referring to its exported properties directly… | ||
_.isNumber(2); | ||
// You now instead call it as a function | ||
_().isNumber(2); | ||
// …it's cached on consecutive calls | ||
_.isNumber('unicorn'); | ||
// It's cached on consecutive calls | ||
_().isString('unicorn'); | ||
// It also works using destructuring assignment in ES2015 | ||
const {isNumber, isString} = importLazy('lodash'); | ||
// Extract lazy variations of the props you need | ||
const members = importLazy('lodash')('isNumber', 'isString'); | ||
// Useful when using destructuring assignment in ES2015 | ||
const {isNumber, isString} = importLazy('lodash')('isNumber', 'isString'); | ||
// Works out of the box for functions and regular properties | ||
const stuff = importLazy('./math-lib')('sum', 'PHI'); | ||
const stuff = importLazy('./math-lib'); | ||
console.log(stuff.sum(1, 2)); // => 3 | ||
@@ -42,14 +38,3 @@ console.log(stuff.PHI); // => 1.618033 | ||
### Proxy support in Node.js 6 or later | ||
If you use Node.js 6 or later, you can take advantage of ES2015 proxies and don't need to call it as a function. | ||
```js | ||
const importLazy = require('import-lazy').proxy(require); | ||
const _ = importLazy('lodash'); | ||
// No need to call it as a function but still lazily imported | ||
_.isNumber(2); | ||
``` | ||
## Related | ||
@@ -56,0 +41,0 @@ |
Sorry, the diff of this file is not supported yet
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
3926
18
50