auto-bind-js 1.0.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Mohammad Alemzadeh
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohammad Alemzadeh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,154 +1,175 @@
1
- # auto-bind-js
2
-
3
- > Automatically bind class methods to their instance. Zero dependencies.
4
-
5
- A modern, feature-rich alternative to [`auto-bind`](https://www.npmjs.com/package/auto-bind) with extra capabilities: **lazy binding**, **regex pattern matching**, **decorators**, **Symbol-keyed methods**, and full **TypeScript** support.
6
-
7
- ## Install
8
-
9
- ```bash
10
- npm install auto-bind-js
11
- ```
12
-
13
- ## Usage
14
-
15
- ### Basic
16
-
17
- ```js
18
- import autoBind from 'auto-bind-js';
19
-
20
- class Unicorn {
21
- constructor(name) {
22
- this.name = name;
23
- autoBind(this);
24
- }
25
-
26
- message() {
27
- return `${this.name} is awesome!`;
28
- }
29
- }
30
-
31
- const unicorn = new Unicorn('Rainbow');
32
- const { message } = unicorn;
33
- message(); //=> 'Rainbow is awesome!'
34
- ```
35
-
36
- ### Bind specific methods (shorthand)
37
-
38
- ```js
39
- autoBind(this, 'handleClick', 'handleSubmit');
40
- ```
41
-
42
- ### Options
43
-
44
- ```js
45
- // Only bind these methods
46
- autoBind(this, { include: ['handleClick', 'handleSubmit'] });
47
-
48
- // Bind all except these
49
- autoBind(this, { exclude: ['render', 'toString'] });
50
-
51
- // Bind only methods matching a pattern
52
- autoBind(this, { pattern: /^handle/ });
53
-
54
- // Lazy binding — binds on first access (better perf for large classes)
55
- autoBind(this, { lazy: true });
56
- ```
57
-
58
- ### React
59
-
60
- Excludes all React lifecycle methods automatically:
61
-
62
- ```js
63
- import autoBindReact from 'auto-bind-js/react';
64
-
65
- class MyComponent extends React.Component {
66
- constructor(props) {
67
- super(props);
68
- autoBindReact(this);
69
- }
70
-
71
- handleClick() {
72
- // `this` is always the component instance
73
- }
74
-
75
- render() {
76
- return <button onClick={this.handleClick}>Click</button>;
77
- }
78
- }
79
- ```
80
-
81
- ### Decorators
82
-
83
- ```js
84
- import { boundClass, bound } from 'auto-bind-js/decorator';
85
-
86
- // Class decorator binds ALL methods
87
- @boundClass
88
- class Foo {
89
- constructor() {
90
- this.name = 'foo';
91
- }
92
-
93
- handleClick() {
94
- return this.name; // always bound
95
- }
96
- }
97
-
98
- // Method decorator — bind individual methods
99
- class Bar {
100
- constructor() {
101
- this.name = 'bar';
102
- }
103
-
104
- @bound
105
- handleClick() {
106
- return this.name; // always bound
107
- }
108
- }
109
- ```
110
-
111
- ## API
112
-
113
- ### `autoBind(self, options?)`
114
-
115
- Bind methods of `self` to the instance. Returns `self`.
116
-
117
- #### Options
118
-
119
- | Option | Type | Description |
120
- | --------- | ----------------- | -------------------------------------------- |
121
- | `include` | `(string\|symbol)[]` | Only bind these methods |
122
- | `exclude` | `(string\|symbol)[]` | Skip these methods |
123
- | `pattern` | `RegExp` | Only bind methods whose names match this regex |
124
- | `lazy` | `boolean` | Bind on first access instead of immediately |
125
-
126
- ### `autoBindReact(self, options?)`
127
-
128
- Same as `autoBind` but automatically excludes React lifecycle methods (`render`, `componentDidMount`, `shouldComponentUpdate`, etc.)
129
-
130
- ### `boundClass(target)`
131
-
132
- Class decorator. Auto-binds all methods when the class is instantiated.
133
-
134
- ### `bound(target, key, descriptor)`
135
-
136
- Method decorator. Lazily binds the decorated method to the instance on first access.
137
-
138
- ## Features
139
-
140
- - Zero dependencies
141
- - Binds inherited methods
142
- - Supports Symbol-keyed methods
143
- - ✅ Include/exclude filters
144
- - Regex pattern matching
145
- - Lazy binding mode
146
- - ✅ React lifecycle awareness
147
- - Class & method decorators
148
- - ✅ Full TypeScript declarations
149
- - ESM + CommonJS dual package
150
- - ✅ Lightweight (~2KB)
151
-
152
- ## License
153
-
154
- MIT
1
+ # auto-bind-js
2
+
3
+ > Automatically bind class methods to their instance. Zero dependencies.
4
+
5
+ A modern, feature-rich alternative to [`auto-bind`](https://www.npmjs.com/package/auto-bind) with extra capabilities: **lazy binding**, **regex pattern matching**, **decorators**, **Symbol-keyed methods**, and full **TypeScript** support.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install auto-bind-js
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Basic
16
+
17
+ ```js
18
+ import autoBind from 'auto-bind-js';
19
+
20
+ class Unicorn {
21
+ constructor(name) {
22
+ this.name = name;
23
+ autoBind(this);
24
+ }
25
+
26
+ message() {
27
+ return `${this.name} is awesome!`;
28
+ }
29
+ }
30
+
31
+ const unicorn = new Unicorn('Rainbow');
32
+ const { message } = unicorn;
33
+ message(); //=> 'Rainbow is awesome!'
34
+ ```
35
+
36
+ ### CommonJS
37
+
38
+ ```js
39
+ const autoBind = require('auto-bind-js');
40
+
41
+ module.exports = class Controller {
42
+ constructor() {
43
+ autoBind(this);
44
+ }
45
+
46
+ index(req, res) {
47
+ // `this` is always bound to the instance
48
+ res.json(this.getData());
49
+ }
50
+
51
+ getData() {
52
+ return { status: 'ok' };
53
+ }
54
+ };
55
+ ```
56
+
57
+ ### Bind specific methods (shorthand)
58
+
59
+ ```js
60
+ autoBind(this, 'handleClick', 'handleSubmit');
61
+ ```
62
+
63
+ ### Options
64
+
65
+ ```js
66
+ // Only bind these methods
67
+ autoBind(this, { include: ['handleClick', 'handleSubmit'] });
68
+
69
+ // Bind all except these
70
+ autoBind(this, { exclude: ['render', 'toString'] });
71
+
72
+ // Bind only methods matching a pattern
73
+ autoBind(this, { pattern: /^handle/ });
74
+
75
+ // Lazy binding — binds on first access (better perf for large classes)
76
+ autoBind(this, { lazy: true });
77
+ ```
78
+
79
+ ### React
80
+
81
+ Excludes all React lifecycle methods automatically:
82
+
83
+ ```js
84
+ import autoBindReact from 'auto-bind-js/react';
85
+
86
+ class MyComponent extends React.Component {
87
+ constructor(props) {
88
+ super(props);
89
+ autoBindReact(this);
90
+ }
91
+
92
+ handleClick() {
93
+ // `this` is always the component instance
94
+ }
95
+
96
+ render() {
97
+ return <button onClick={this.handleClick}>Click</button>;
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### Decorators
103
+
104
+ ```js
105
+ import { boundClass, bound } from 'auto-bind-js/decorator';
106
+
107
+ // Class decorator — binds ALL methods
108
+ @boundClass
109
+ class Foo {
110
+ constructor() {
111
+ this.name = 'foo';
112
+ }
113
+
114
+ handleClick() {
115
+ return this.name; // always bound
116
+ }
117
+ }
118
+
119
+ // Method decorator — bind individual methods
120
+ class Bar {
121
+ constructor() {
122
+ this.name = 'bar';
123
+ }
124
+
125
+ @bound
126
+ handleClick() {
127
+ return this.name; // always bound
128
+ }
129
+ }
130
+ ```
131
+
132
+ ## API
133
+
134
+ ### `autoBind(self, options?)`
135
+
136
+ Bind methods of `self` to the instance. Returns `self`.
137
+
138
+ #### Options
139
+
140
+ | Option | Type | Description |
141
+ | --------- | -------------------- | ---------------------------------------------- |
142
+ | `include` | `(string\|symbol)[]` | Only bind these methods |
143
+ | `exclude` | `(string\|symbol)[]` | Skip these methods |
144
+ | `pattern` | `RegExp` | Only bind methods whose names match this regex |
145
+ | `lazy` | `boolean` | Bind on first access instead of immediately |
146
+
147
+ ### `autoBindReact(self, options?)`
148
+
149
+ Same as `autoBind` but automatically excludes React lifecycle methods (`render`, `componentDidMount`, `shouldComponentUpdate`, etc.)
150
+
151
+ ### `boundClass(target)`
152
+
153
+ Class decorator. Auto-binds all methods when the class is instantiated.
154
+
155
+ ### `bound(target, key, descriptor)`
156
+
157
+ Method decorator. Lazily binds the decorated method to the instance on first access.
158
+
159
+ ## Features
160
+
161
+ - ✅ Zero dependencies
162
+ - ✅ Binds inherited methods
163
+ - ✅ Supports Symbol-keyed methods
164
+ - ✅ Include/exclude filters
165
+ - ✅ Regex pattern matching
166
+ - ✅ Lazy binding mode
167
+ - ✅ React lifecycle awareness
168
+ - ✅ Class & method decorators
169
+ - ✅ Full TypeScript declarations
170
+ - ✅ ESM + CommonJS dual package
171
+ - ✅ Lightweight (~2KB)
172
+
173
+ ## License
174
+
175
+ MIT