├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function bindMethods(object, context) { 2 | context = context || object; 3 | 4 | for (const [key, value] of Object.entries(object)) { 5 | if (typeof value === 'function') { 6 | object[key] = value.bind(context); 7 | } 8 | } 9 | 10 | return object; 11 | } 12 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bind-methods", 3 | "version": "2.0.0", 4 | "description": "Bind all methods in an object to itself or a specified context", 5 | "license": "MIT", 6 | "repository": "sindresorhus/bind-methods", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "bind", 26 | "methods", 27 | "object", 28 | "method", 29 | "function", 30 | "all", 31 | "instance", 32 | "this", 33 | "self", 34 | "context" 35 | ], 36 | "devDependencies": { 37 | "ava": "^3.15.0", 38 | "xo": "^0.38.2" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # bind-methods 2 | 3 | > Bind all methods in an object to itself or a specified context 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install bind-methods 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import bindMethods from 'bind-methods'; 15 | 16 | const unicorn = { 17 | name: 'Rainbow', 18 | message() { 19 | return `${this.name} is awesome!`; 20 | } 21 | }; 22 | 23 | const message = unicorn.message; 24 | message(); 25 | //=> Error: Cannot read property 'name' of undefined 26 | 27 | bindMethods(unicorn); 28 | 29 | const message2 = unicorn.message; 30 | message2(); 31 | //=> 'Rainbow is awesome!' 32 | ``` 33 | 34 | ## API 35 | 36 | ### bindMethods(input, context?) 37 | 38 | Bind methods in `input` to itself or `context` if specified. 39 | 40 | Returns the `input` object. 41 | 42 | #### input 43 | 44 | Type: `object` 45 | 46 | Object with methods to bind. 47 | 48 | #### context 49 | 50 | Type: `object`\ 51 | Default: The `input` object 52 | 53 | Object to bind the methods to. 54 | 55 | ## Related 56 | 57 | - [auto-bind](https://github.com/sindresorhus/auto-bind) - Automatically bind methods to their class instance 58 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import bindMethods from './index.js'; 3 | 4 | test('main', t => { 5 | const unicorn = { 6 | name: 'Rainbow', 7 | message() { 8 | return `${this.name} is awesome!`; 9 | } 10 | }; 11 | 12 | const {message} = unicorn; 13 | t.throws(() => { 14 | message(); 15 | }, { 16 | message: /Cannot read/ 17 | }); 18 | 19 | const bounded = bindMethods(unicorn); 20 | t.is(bounded, unicorn); 21 | 22 | const message2 = unicorn.message; 23 | t.is(message2(), 'Rainbow is awesome!'); 24 | }); 25 | --------------------------------------------------------------------------------