Socket
Socket
Sign inDemoInstall

lazy-req

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazy-req - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

37

index.js
'use strict';
module.exports = function (fn) {
return function (id) {
var mod;
const lazy = (mod, fn, id) => mod === undefined ? fn(id) : mod;
module.exports = fn => {
return id => {
let mod;
return function () {
if (!arguments.length) {
if (arguments.length === 0) {
mod = lazy(mod, fn, id);

@@ -12,7 +14,7 @@ return mod;

var ret = {};
const ret = {};
[].forEach.call(arguments, function (prop) {
[].forEach.call(arguments, prop => {
Object.defineProperty(ret, prop, {
get: function () {
get: () => {
mod = lazy(mod, fn, id);

@@ -33,6 +35,21 @@ if (typeof mod[prop] === 'function') {

};
};
function lazy(mod, fn, id) {
return mod !== undefined ? mod : fn(id);
}
module.exports.proxy = fn => {
return id => {
let mod;
const handler = {
get: (target, property) => {
mod = lazy(mod, fn, id);
return Reflect.get(mod, property);
},
apply: (target, thisArg, argumentsList) => {
mod = lazy(mod, fn, id);
return Reflect.apply(mod, thisArg, argumentsList);
}
};
return new Proxy(() => {}, handler);
};
};
{
"name": "lazy-req",
"version": "1.1.0",
"version": "2.0.0",
"description": "Require modules lazily",

@@ -10,3 +10,3 @@ "license": "MIT",

"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
"url": "sindresorhus.com"
},

@@ -20,6 +20,6 @@ "contributors": [

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && node test.js"
"test": "xo && ava"
},

@@ -37,8 +37,10 @@ "files": [

"defer",
"deferred"
"deferred",
"proxy",
"proxies"
],
"devDependencies": {
"ava": "0.0.4",
"ava": "*",
"xo": "*"
}
}

@@ -17,22 +17,22 @@ # lazy-req [![Build Status](https://travis-ci.org/sindresorhus/lazy-req.svg?branch=master)](https://travis-ci.org/sindresorhus/lazy-req)

// pass in `require` or a custom require function
var lazyReq = require('lazy-req')(require);
var _ = lazyReq('lodash');
const lazyReq = require('lazy-req')(require);
const _ = lazyReq('lodash');
// where you would normally do
// Where you would normally do
_.isNumber(2);
// you now instead call it as a function
// You now instead call it as a function
_().isNumber(2);
// it's cached on consecutive calls
// It's cached on consecutive calls
_().isString('unicorn');
// extract lazy variations of the props you need
var members = lazyReq('lodash')('isNumber', 'isString');
// Extract lazy variations of the props you need
const members = lazyReq('lodash')('isNumber', 'isString');
// useful when using destructuring assignment in ES2015
const { isNumber, isString } = 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');
// Works out of the box for functions and regular properties
const stuff = lazyReq('./math-lib')('sum', 'PHI');
console.log(stuff.sum(1, 2)); // => 3

@@ -42,5 +42,25 @@ 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 lazyReq = require('lazy-req').proxy(require);
const _ = lazyReq('lodash');
// No need to call it as a function but still lazily required
_.isNumber(2);
```
## Related
- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
- [req-from](https://github.com/sindresorhus/req-from) - Require a module from a given path
- [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
- [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
- [define-lazy-prop](https://github.com/sindresorhus/define-lazy-prop) - Define a lazily evaluated property on an object
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
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