[Pkg-javascript-commits] [node-auto-bind] 01/04: New upstream version 1.2.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Wed Jan 31 14:55:45 UTC 2018


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-auto-bind.

commit f002996bbb7d2094ecb3083cd2ec6fb85a18d735
Author: Pirate Praveen <praveen at debian.org>
Date:   Wed Jan 31 20:09:33 2018 +0530

    New upstream version 1.2.0
---
 .editorconfig  |  2 +-
 .gitattributes |  1 +
 .gitignore     |  1 +
 .npmrc         |  1 +
 .travis.yml    |  3 +-
 index.js       | 48 ++++++++++++++++++++++++++----
 license        | 20 +++----------
 package.json   | 74 +++++++++++++++++++++++----------------------
 readme.md      | 53 ++++++++++++++++++++++++++++++---
 test.js        | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 10 files changed, 229 insertions(+), 68 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 98a761d..1c6314a 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -7,6 +7,6 @@ charset = utf-8
 trim_trailing_whitespace = true
 insert_final_newline = true
 
-[{package.json,*.yml}]
+[*.yml]
 indent_style = space
 indent_size = 2
diff --git a/.gitattributes b/.gitattributes
index 176a458..391f0a4 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,2 @@
 * text=auto
+*.js text eol=lf
diff --git a/.gitignore b/.gitignore
index 3c3629e..239ecff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 node_modules
+yarn.lock
diff --git a/.npmrc b/.npmrc
new file mode 100644
index 0000000..43c97e7
--- /dev/null
+++ b/.npmrc
@@ -0,0 +1 @@
+package-lock=false
diff --git a/.travis.yml b/.travis.yml
index e526ffa..7d69d74 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
 language: node_js
 node_js:
+  - '8'
   - '6'
   - '4'
-  - '0.12'
-  - '0.10'
diff --git a/index.js b/index.js
index 555ef2c..b72f5f3 100644
--- a/index.js
+++ b/index.js
@@ -1,10 +1,48 @@
 'use strict';
-module.exports = function (self) {
-	Object.getOwnPropertyNames(self.constructor.prototype).forEach(function (key) {
-		var val = self[key];
+module.exports = (self, options) => {
+	options = Object.assign({}, options);
 
-		if (key !== 'constructor' && typeof val === 'function') {
+	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);
 };
diff --git a/license b/license
index 654d0bf..e7af2f7 100644
--- a/license
+++ b/license
@@ -1,21 +1,9 @@
-The MIT License (MIT)
+MIT License
 
 Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (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:
+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 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.
+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.
diff --git a/package.json b/package.json
index 4fe1b6c..03387a9 100644
--- a/package.json
+++ b/package.json
@@ -1,38 +1,40 @@
 {
-  "name": "auto-bind",
-  "version": "0.1.0",
-  "description": "Automatically bind methods to their class instance",
-  "license": "MIT",
-  "repository": "sindresorhus/auto-bind",
-  "author": {
-    "name": "Sindre Sorhus",
-    "email": "sindresorhus at gmail.com",
-    "url": "sindresorhus.com"
-  },
-  "engines": {
-    "node": ">=0.10.0"
-  },
-  "scripts": {
-    "test": "xo && ava"
-  },
-  "files": [
-    "index.js"
-  ],
-  "keywords": [
-    "auto",
-    "bind",
-    "class",
-    "methods",
-    "method",
-    "automatically",
-    "prototype",
-    "instance",
-    "function",
-    "this",
-    "self"
-  ],
-  "devDependencies": {
-    "ava": "*",
-    "xo": "*"
-  }
+	"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 at 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": "*"
+	}
 }
diff --git a/readme.md b/readme.md
index 4aaf930..a40bcc4 100644
--- a/readme.md
+++ b/readme.md
@@ -6,7 +6,7 @@
 ## Install
 
 ```
-$ npm install --save auto-bind
+$ npm install auto-bind
 ```
 
 
@@ -20,6 +20,7 @@ class Unicorn {
 		this.name = name;
 		autoBind(this);
 	}
+
 	message() {
 		return `${this.name} is awesome!`;
 	}
@@ -27,19 +28,63 @@ class Unicorn {
 
 const unicorn = new Unicorn('Rainbow');
 
-// grab the method off the class instance
+// Grab the method off the class instance
 const message = unicorn.message;
 
-// still bound to the class instance
+// Still bound to the class instance
 message();
 //=> 'Rainbow is awesome!'
 
-// without `autoBind(this)`, the above would have resulted in
+// 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<string|RegExp>`
+
+Bind only the given methods.
+
+##### exclude
+
+Type: `Array<string|RegExp>`
+
+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
diff --git a/test.js b/test.js
index b7c83dc..57b7117 100644
--- a/test.js
+++ b/test.js
@@ -1,19 +1,105 @@
 import test from 'ava';
-import m from './';
+import m from '.';
+
+test('autoBind()', t => {
+	let bounded;
 
-test(t => {
 	class Unicorn {
 		constructor(name) {
 			this.name = name;
-			m(this);
+			bounded = m(this);
 		}
+
 		message() {
 			return `${this.name} is awesome!`;
 		}
 	}
 
 	const unicorn = new Unicorn('Rainbow');
-	const message = unicorn.message;
+	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');
+});
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-auto-bind.git



More information about the Pkg-javascript-commits mailing list