pax_global_header00006660000000000000000000000064135756646350014536gustar00rootroot0000000000000052 comment=1f3e975d842cbceaa40ed6abdf680e54a9a3ef43 auto-bind-4.0.0/000077500000000000000000000000001357566463500134215ustar00rootroot00000000000000auto-bind-4.0.0/.editorconfig000066400000000000000000000002571357566463500161020ustar00rootroot00000000000000root = 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-4.0.0/.gitattributes000066400000000000000000000000231357566463500163070ustar00rootroot00000000000000* text=auto eol=lf auto-bind-4.0.0/.github/000077500000000000000000000000001357566463500147615ustar00rootroot00000000000000auto-bind-4.0.0/.github/funding.yml000066400000000000000000000001331357566463500171330ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus custom: https://sindresorhus.com/donate auto-bind-4.0.0/.gitignore000066400000000000000000000000271357566463500154100ustar00rootroot00000000000000node_modules yarn.lock auto-bind-4.0.0/.npmrc000066400000000000000000000000231357566463500145340ustar00rootroot00000000000000package-lock=false auto-bind-4.0.0/.travis.yml000066400000000000000000000000651357566463500155330ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' auto-bind-4.0.0/index.d.ts000066400000000000000000000017751357566463500153340ustar00rootroot00000000000000declare namespace autoBind { interface Options { /** Bind only the given methods. */ readonly include?: ReadonlyArray; /** Bind methods except for the given methods. */ readonly exclude?: ReadonlyArray; } } /** Automatically bind methods to their class instance. @param self - Object with methods to bind. @example ``` import 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 ``` */ declare function autoBind( self: SelfType, options?: autoBind.Options ): SelfType; export = autoBind; auto-bind-4.0.0/index.js000066400000000000000000000017221357566463500150700ustar00rootroot00000000000000'use strict'; // Gets all non-builtin properties up the prototype chain const getAllProperties = object => { const properties = new Set(); do { for (const key of Reflect.ownKeys(object)) { properties.add([object, key]); } } while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype); return properties; }; module.exports = (self, {include, exclude} = {}) => { const filter = key => { const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key); if (include) { return include.some(match); } if (exclude) { return !exclude.some(match); } return true; }; for (const [object, key] of getAllProperties(self.constructor.prototype)) { if (key === 'constructor' || !filter(key)) { continue; } const descriptor = Reflect.getOwnPropertyDescriptor(object, key); if (descriptor && typeof descriptor.value === 'function') { self[key] = self[key].bind(self); } } return self; }; auto-bind-4.0.0/index.test-d.ts000066400000000000000000000012401357566463500162740ustar00rootroot00000000000000import {expectType} from 'tsd'; import {Component as ReactComponent} from 'react'; import autoBindReact = require('./react'); import autoBind = require('.'); const foo = { bar: 'bar', method(foo: string) { return this.bar; } }; expectType(autoBind(foo)); expectType(autoBind(foo, {include: ['foo', /bar/]})); expectType(autoBind(foo, {exclude: ['foo', /bar/]})); class Bar extends ReactComponent { constructor(props: object) { super(props); expectType(autoBindReact(this)); expectType(autoBindReact(this, {include: ['foo', /bar/]})); expectType(autoBindReact(this, {exclude: ['foo', /bar/]})); } } auto-bind-4.0.0/license000066400000000000000000000021251357566463500147660ustar00rootroot00000000000000MIT 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-4.0.0/package.json000066400000000000000000000014271357566463500157130ustar00rootroot00000000000000{ "name": "auto-bind", "version": "4.0.0", "description": "Automatically bind methods to their class instance", "license": "MIT", "repository": "sindresorhus/auto-bind", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts", "react.js", "react.d.ts" ], "keywords": [ "auto", "bind", "class", "methods", "method", "automatically", "prototype", "instance", "function", "this", "self", "react", "component" ], "devDependencies": { "@types/react": "^16.9.9", "ava": "^2.4.0", "tsd": "^0.11.0", "xo": "^0.25.3" } } auto-bind-4.0.0/react.d.ts000066400000000000000000000011071357566463500153100ustar00rootroot00000000000000import {Component as ReactComponent} from 'react'; import autoBind = require('.'); /** Same as `autoBind`, but excludes the default [React component methods](https://reactjs.org/docs/react-component.html). @param self - Object with methods to bind. @example ``` import autoBindReact = require('auto-bind/react'); class Foo extends React.Component { constructor(props) { super(props); autoBindReact(this); } // … } ``` */ declare function autoBindReact( self: SelfType, options?: autoBind.Options ): SelfType; export = autoBindReact; auto-bind-4.0.0/react.js000066400000000000000000000011351357566463500150550ustar00rootroot00000000000000'use strict'; const autoBind = require('.'); const excludedReactMethods = [ 'componentWillMount', 'UNSAFE_componentWillMount', 'render', 'getSnapshotBeforeUpdate', 'componentDidMount', 'componentWillReceiveProps', 'UNSAFE_componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'UNSAFE_componentWillUpdate', 'componentDidUpdate', 'componentWillUnmount', 'componentDidCatch', 'setState', 'forceUpdate' ]; module.exports = (self, {exclude = [], ...options} = {}) => { options.exclude = [ ...exclude, ...excludedReactMethods ]; return autoBind(self, options); }; auto-bind-4.0.0/readme.md000066400000000000000000000031201357566463500151740ustar00rootroot00000000000000# 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 It also correctly binds inherited properties. ## 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. ### React Same as `autoBind`, but excludes the default [React component methods](https://reactjs.org/docs/react-component.html). ```js const autoBindReact = require('auto-bind/react'); class Foo extends React.Component { constructor(props) { super(props); autoBindReact(this); } // … } ``` ## Related - [bind-methods](https://github.com/sindresorhus/bind-methods) - Bind all methods in an object to itself or a specified context auto-bind-4.0.0/test.js000066400000000000000000000044521357566463500147430ustar00rootroot00000000000000import test from 'ava'; import autoBindReact from './react'; import autoBind from '.'; test('autoBind()', t => { let bounded; class Unicorn { constructor(name) { this.name = name; bounded = autoBind(this); } message() { return `${this.name} is awesome!`; } get bad() { throw new Error('This getter somehow throws an error!'); } } const unicorn = new Unicorn('Rainbow'); t.is(bounded, unicorn); const {message} = unicorn; t.is(message(), 'Rainbow is awesome!'); }); test('include option', t => { class Unicorn { constructor(name) { this.name = name; autoBind(this, {include: ['bar']}); } foo() { return this.name; } bar() { return this.name; } } const {foo, bar} = new Unicorn('Rainbow'); t.throws(() => { foo(); }); t.is(bar(), 'Rainbow'); }); test('exclude option', t => { class Unicorn { constructor(name) { this.name = name; autoBind(this, {exclude: ['bar']}); } foo() { return this.name; } bar() { return this.name; } } const {foo, bar} = new Unicorn('Rainbow'); t.is(foo(), 'Rainbow'); t.throws(() => { bar(); }); }); test('symbol properties', t => { const messageSymbol = Symbol('message'); let bounded; class Unicorn { constructor(name) { this.name = name; bounded = autoBind(this); } [messageSymbol]() { return `${this.name} is awesome!`; } } const unicorn = new Unicorn('Rainbow'); t.is(bounded, unicorn); const message = unicorn[messageSymbol]; t.is(message(), 'Rainbow is awesome!'); }); test('binds inherited properties', t => { class Base { constructor(name) { this.name = name; } message() { return `${this.name} is awesome!`; } } class Base2 extends Base {} let bounded; class Unicorn extends Base2 { constructor(name) { super(name); bounded = autoBind(this); } } const unicorn = new Unicorn('Rainbow'); t.is(bounded, unicorn); const {message} = unicorn; t.is(message(), 'Rainbow is awesome!'); }); test('autoBindReact()', t => { class Unicorn { constructor(name) { this.name = name; autoBindReact(this); } componentWillMount() { return this.name; } foo() { return this.name; } } const {foo, componentWillMount} = new Unicorn('Rainbow'); t.throws(() => { componentWillMount(); }); t.is(foo(), 'Rainbow'); });