pax_global_header00006660000000000000000000000064132304632630014514gustar00rootroot0000000000000052 comment=07f0eef1fbdc21d6e6cb7e511c92036b52fd885c auto-bind-1.2.0/000077500000000000000000000000001323046326300133765ustar00rootroot00000000000000auto-bind-1.2.0/.editorconfig000066400000000000000000000002571323046326300160570ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 auto-bind-1.2.0/.gitattributes000066400000000000000000000000351323046326300162670ustar00rootroot00000000000000* text=auto *.js text eol=lf auto-bind-1.2.0/.gitignore000066400000000000000000000000271323046326300153650ustar00rootroot00000000000000node_modules yarn.lock auto-bind-1.2.0/.npmrc000066400000000000000000000000231323046326300145110ustar00rootroot00000000000000package-lock=false auto-bind-1.2.0/.travis.yml000066400000000000000000000000631323046326300155060ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' auto-bind-1.2.0/index.js000066400000000000000000000020611323046326300150420ustar00rootroot00000000000000'use strict'; module.exports = (self, options) => { options = Object.assign({}, options); const filter = key => { const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key); if (options.include) { return options.include.some(match); } if (options.exclude) { return !options.exclude.some(match); } return true; }; for (const key of Object.getOwnPropertyNames(self.constructor.prototype)) { const val = self[key]; if (key !== 'constructor' && typeof val === 'function' && filter(key)) { self[key] = val.bind(self); } } return self; }; const excludedReactMethods = [ 'componentWillMount', 'render', 'componentDidMount', 'componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'componentDidUpdate', 'componentWillUnmount', 'componentDidCatch', 'setState', 'forceUpdate' ]; module.exports.react = (self, options) => { options = Object.assign({}, options); options.exclude = (options.exclude || []).concat(excludedReactMethods); return module.exports(self, options); }; auto-bind-1.2.0/license000066400000000000000000000021251323046326300147430ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. auto-bind-1.2.0/package.json000066400000000000000000000011561323046326300156670ustar00rootroot00000000000000{ "name": "auto-bind", "version": "1.2.0", "description": "Automatically bind methods to their class instance", "license": "MIT", "repository": "sindresorhus/auto-bind", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "auto", "bind", "class", "methods", "method", "automatically", "prototype", "instance", "function", "this", "self", "react", "component" ], "devDependencies": { "ava": "*", "xo": "*" } } auto-bind-1.2.0/readme.md000066400000000000000000000031101323046326300151500ustar00rootroot00000000000000# auto-bind [![Build Status](https://travis-ci.org/sindresorhus/auto-bind.svg?branch=master)](https://travis-ci.org/sindresorhus/auto-bind) > Automatically bind methods to their class instance ## Install ``` $ npm install auto-bind ``` ## Usage ```js const autoBind = require('auto-bind'); class Unicorn { constructor(name) { this.name = name; autoBind(this); } message() { return `${this.name} is awesome!`; } } const unicorn = new Unicorn('Rainbow'); // Grab the method off the class instance const message = unicorn.message; // Still bound to the class instance message(); //=> 'Rainbow is awesome!' // Without `autoBind(this)`, the above would have resulted in message(); //=> Error: Cannot read property 'name' of undefined ``` ## API ### autoBind(self, [options]) Bind methods in `self` to their class instance. Returns the `self` object. #### self Type: `Object` Object with methods to bind. #### options Type: `Object` ##### include Type: `Array` Bind only the given methods. ##### exclude Type: `Array` Bind methods except for the given methods. ### autoBind.react(self, [options]) Same as `autoBind`, but excludes the default [React component methods](https://reactjs.org/docs/react-component.html). ```js class Foo extends React.Component { constructor(props) { super(props); autoBind.react(this); } // … } ``` ## Related - [bind-methods](https://github.com/sindresorhus/bind-methods) - Bind all methods in an object to itself or a specified context ## License MIT © [Sindre Sorhus](https://sindresorhus.com) auto-bind-1.2.0/test.js000066400000000000000000000027401323046326300147160ustar00rootroot00000000000000import test from 'ava'; import m from '.'; test('autoBind()', t => { let bounded; class Unicorn { constructor(name) { this.name = name; bounded = m(this); } message() { return `${this.name} is awesome!`; } } const unicorn = new Unicorn('Rainbow'); t.is(bounded, unicorn); const message = unicorn.message; t.is(message(), 'Rainbow is awesome!'); }); test('include option', t => { class Unicorn { constructor(name) { this.name = name; m(this, {include: ['bar']}); } foo() { return this.name; } bar() { return this.name; } } const unicorn = new Unicorn('Rainbow'); const foo = unicorn.foo; const bar = unicorn.bar; t.throws(() => { foo(); }); t.is(bar(), 'Rainbow'); }); test('exclude option', t => { class Unicorn { constructor(name) { this.name = name; m(this, {exclude: ['bar']}); } foo() { return this.name; } bar() { return this.name; } } const unicorn = new Unicorn('Rainbow'); const foo = unicorn.foo; const bar = unicorn.bar; t.is(foo(), 'Rainbow'); t.throws(() => { bar(); }); }); test('autoBind.react()', t => { class Unicorn { constructor(name) { this.name = name; m.react(this); } componentWillMount() { return this.name; } foo() { return this.name; } } const unicorn = new Unicorn('Rainbow'); const componentWillMount = unicorn.componentWillMount; const foo = unicorn.foo; t.throws(() => { componentWillMount(); }); t.is(foo(), 'Rainbow'); });