Comparing version 1.0.2 to 1.0.3
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.reactAutoBind = reactAutoBind; | ||
function autoBind(obj, filter) { | ||
filter = filter || function () { | ||
return true; | ||
}; | ||
filter = filter || function () { | ||
return true; | ||
}; | ||
Object.getOwnPropertyNames(obj.constructor.prototype).forEach(function (name) { | ||
var val = obj[name]; | ||
Object.getOwnPropertyNames(obj.constructor.prototype).forEach(function (name) { | ||
var val = obj[name]; | ||
if (name !== 'constructor' && typeof val === 'function' && filter(name)) { | ||
obj[name] = val.bind(obj); | ||
} | ||
}); | ||
if (name !== 'constructor' && typeof val === 'function' && filter(name)) { | ||
obj[name] = val.bind(obj); | ||
} | ||
}); | ||
return obj; | ||
return obj; | ||
} | ||
@@ -26,13 +26,13 @@ | ||
var isReactMethod = exports.isReactMethod = function isReactMethod(name) { | ||
'render', 'componentWillReceiveProps', 'componentDidMount', 'componentDidUpdate', 'shouldComponentUpdate', 'componentWillUnmount', 'componentWillUpdate', 'forceUpdate', 'componentWillMount'; | ||
return ['render', 'componentWillReceiveProps', 'componentDidMount', 'componentDidUpdate', 'shouldComponentUpdate', 'componentWillUnmount', 'componentWillUpdate', 'forceUpdate', 'componentWillMount'].includes(name); | ||
}; | ||
function reactAutoBind(obj, filter) { | ||
filter = filter || function () { | ||
return true; | ||
}; | ||
return autoBind(obj, function (name) { | ||
return !isReactMethod(name) && filter(name); | ||
}); | ||
filter = filter || function () { | ||
return true; | ||
}; | ||
return autoBind(obj, function (name) { | ||
return !isReactMethod(name) && filter(name); | ||
}); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "auto-bind2", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "An ES6 class method automatic binder", | ||
@@ -11,2 +11,5 @@ "main": "dist/index.js", | ||
}, | ||
"files": [ | ||
"dist/**" | ||
], | ||
"repository": "git@github.com:Jamoki/auto-bind2.git", | ||
@@ -13,0 +16,0 @@ "keywords": [ |
@@ -1,2 +0,78 @@ | ||
# auto-bind2 | ||
A Javascript autoBind() function with a customizable filter and defaults for React | ||
# ES6 Class Auto Binder | ||
A Javascript ES6 class autoBind() function with a customizable filter and defaults for React. | ||
## Why Build This? | ||
This package exposes an `autoBind` function that calls `bind()` on 'methods' in you ES6 `class` so that `this` always refers to the `class` object instance and not `window` or some other random object. It avoids the need to call `bind()` multiple times in your constructor, like this: | ||
```Javascript | ||
class Foo { | ||
constructor() { | ||
this.onClick = this.onClick.bind() | ||
this.onDrag = this.onDrag.bind() | ||
this.onWhatever = this.onWhatever.bind() | ||
... | ||
} | ||
... | ||
} | ||
``` | ||
Instead you just write: | ||
```Javascript | ||
class Foo { | ||
constructor() { | ||
autoBind(this) | ||
} | ||
... | ||
} | ||
``` | ||
There are a few `autoBind` functions out there already, and they work great. However what I really felt was needed was a fully customizable function that used a filter callback so that it would work with whatever naming conventions you used for your callback functions. | ||
## Usage | ||
Install the package with: | ||
```Shell | ||
npm install auto-bind2 | ||
``` | ||
Import it using: | ||
```Javascript | ||
import autoBind from 'auto-bind2' | ||
// OR | ||
var autoBind = require('auto-bind2') | ||
``` | ||
The function takes a `this` pointer and an optional filter method, e.g.: | ||
```Javascript | ||
autoBind(this) | ||
autoBind(this, (funcName) => (funcName.startsWith('on'))) | ||
autoBind(this, (funcName) => (['onClick', 'onDrag', 'onEvent'].includes(funcName))) | ||
``` | ||
The library includes support for React. React `Component` methods are already bound, so you can exclude them with: | ||
```Javascript | ||
import { reactAutoBind } from 'auto-bind2' | ||
reactAutoBind(this) | ||
``` | ||
or: | ||
```Javascript | ||
import { isReactMethod, autoBind } from 'auto-bind2' | ||
autoBind(this, isReactMethod) | ||
``` | ||
## About | ||
The code is written in ES6 Javascript and built to target NodeJS v8, Chrome v60 and uglification. |
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
79
1
0
6612
5
30