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 +21 -21
- package/README.md +175 -154
- package/dist/index.cjs +226 -226
- package/dist/index.d.ts +80 -80
- package/dist/index.mjs +288 -288
- package/package.json +59 -59
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
|
-
###
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
autoBind
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
```js
|
|
84
|
-
import
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
### `
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|