auto-bind-js 1.1.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/dist/index.cjs CHANGED
@@ -1,226 +1,226 @@
1
- /**
2
- * auto-bind-js (CommonJS)
3
- */
4
-
5
- 'use strict';
6
-
7
- const BUILTIN_OBJECT_METHODS = new Set([
8
- 'constructor',
9
- 'toString',
10
- 'toLocaleString',
11
- 'valueOf',
12
- 'hasOwnProperty',
13
- 'isPrototypeOf',
14
- 'propertyIsEnumerable',
15
- '__defineGetter__',
16
- '__defineSetter__',
17
- '__lookupGetter__',
18
- '__lookupSetter__',
19
- ]);
20
-
21
- function getAllMethodNames(obj) {
22
- const methods = new Set();
23
- let proto = Object.getPrototypeOf(obj);
24
-
25
- while (proto && proto !== Object.prototype) {
26
- const keys = Object.getOwnPropertyNames(proto);
27
- for (const key of keys) {
28
- if (key === 'constructor') continue;
29
- const descriptor = Object.getOwnPropertyDescriptor(proto, key);
30
- if (descriptor && typeof descriptor.value === 'function') {
31
- methods.add(key);
32
- }
33
- }
34
-
35
- const symbols = Object.getOwnPropertySymbols(proto);
36
- for (const sym of symbols) {
37
- const descriptor = Object.getOwnPropertyDescriptor(proto, sym);
38
- if (descriptor && typeof descriptor.value === 'function') {
39
- methods.add(sym);
40
- }
41
- }
42
-
43
- proto = Object.getPrototypeOf(proto);
44
- }
45
-
46
- return methods;
47
- }
48
-
49
- function filterMethods(methods, options = {}) {
50
- let filtered = [...methods];
51
-
52
- filtered = filtered.filter((m) => typeof m === 'symbol' || !BUILTIN_OBJECT_METHODS.has(m));
53
-
54
- if (options.include) {
55
- const includeSet = new Set(options.include);
56
- filtered = filtered.filter((m) => includeSet.has(m));
57
- }
58
-
59
- if (options.exclude) {
60
- const excludeSet = new Set(options.exclude);
61
- filtered = filtered.filter((m) => !excludeSet.has(m));
62
- }
63
-
64
- if (options.pattern) {
65
- filtered = filtered.filter(
66
- (m) => typeof m === 'string' && options.pattern.test(m)
67
- );
68
- }
69
-
70
- return filtered;
71
- }
72
-
73
- function bindEager(self, methods) {
74
- for (const method of methods) {
75
- const val = self[method];
76
- if (typeof val === 'function') {
77
- Object.defineProperty(self, method, {
78
- value: val.bind(self),
79
- writable: true,
80
- configurable: true,
81
- enumerable: false,
82
- });
83
- }
84
- }
85
- }
86
-
87
- function findDescriptorInChain(obj, key) {
88
- let proto = Object.getPrototypeOf(obj);
89
- while (proto && proto !== Object.prototype) {
90
- const desc = Object.getOwnPropertyDescriptor(proto, key);
91
- if (desc) return desc;
92
- proto = Object.getPrototypeOf(proto);
93
- }
94
- return undefined;
95
- }
96
-
97
- function bindLazy(self, methods) {
98
- for (const method of methods) {
99
- const proto = Object.getPrototypeOf(self);
100
- const descriptor = Object.getOwnPropertyDescriptor(proto, method) ||
101
- findDescriptorInChain(self, method);
102
-
103
- if (!descriptor || typeof descriptor.value !== 'function') continue;
104
-
105
- const originalFn = descriptor.value;
106
-
107
- Object.defineProperty(self, method, {
108
- configurable: true,
109
- enumerable: false,
110
- get() {
111
- const boundFn = originalFn.bind(self);
112
- Object.defineProperty(self, method, {
113
- value: boundFn,
114
- writable: true,
115
- configurable: true,
116
- enumerable: false,
117
- });
118
- return boundFn;
119
- },
120
- });
121
- }
122
- }
123
-
124
- function autoBind(self, options) {
125
- if (typeof options === 'string') {
126
- const methodNames = [options, ...Array.from(arguments).slice(2)];
127
- options = { include: methodNames };
128
- }
129
-
130
- const allMethods = getAllMethodNames(self);
131
- const methods = filterMethods(allMethods, options);
132
-
133
- if (options && options.lazy) {
134
- bindLazy(self, methods);
135
- } else {
136
- bindEager(self, methods);
137
- }
138
-
139
- return self;
140
- }
141
-
142
- // React
143
- const REACT_LIFECYCLE_METHODS = new Set([
144
- 'render',
145
- 'componentDidMount',
146
- 'componentDidUpdate',
147
- 'componentWillUnmount',
148
- 'shouldComponentUpdate',
149
- 'getSnapshotBeforeUpdate',
150
- 'getDerivedStateFromProps',
151
- 'getDerivedStateFromError',
152
- 'componentDidCatch',
153
- 'UNSAFE_componentWillMount',
154
- 'UNSAFE_componentWillReceiveProps',
155
- 'UNSAFE_componentWillUpdate',
156
- 'getDefaultProps',
157
- 'getInitialState',
158
- 'componentWillMount',
159
- 'componentWillReceiveProps',
160
- 'componentWillUpdate',
161
- ]);
162
-
163
- function autoBindReact(self, options = {}) {
164
- const exclude = [
165
- ...(options.exclude || []),
166
- ...REACT_LIFECYCLE_METHODS,
167
- ];
168
- return autoBind(self, { ...options, exclude });
169
- }
170
-
171
- // Decorators
172
- function boundClass(target) {
173
- const original = target;
174
-
175
- const wrapped = function (...args) {
176
- const instance = new original(...args);
177
- autoBind(instance);
178
- return instance;
179
- };
180
-
181
- wrapped.prototype = original.prototype;
182
- Object.defineProperty(wrapped, 'name', { value: original.name });
183
- Object.setPrototypeOf(wrapped, original);
184
-
185
- for (const key of Object.getOwnPropertyNames(original)) {
186
- if (['length', 'name', 'prototype', 'arguments', 'caller'].includes(key)) continue;
187
- const desc = Object.getOwnPropertyDescriptor(original, key);
188
- if (desc) Object.defineProperty(wrapped, key, desc);
189
- }
190
-
191
- return wrapped;
192
- }
193
-
194
- function bound(target, key, descriptor) {
195
- if (!descriptor || typeof descriptor.value !== 'function') {
196
- throw new TypeError('@bound can only be applied to methods');
197
- }
198
-
199
- const fn = descriptor.value;
200
-
201
- return {
202
- configurable: true,
203
- enumerable: false,
204
- get() {
205
- if (this === target) {
206
- return fn;
207
- }
208
-
209
- const boundFn = fn.bind(this);
210
- Object.defineProperty(this, key, {
211
- value: boundFn,
212
- writable: true,
213
- configurable: true,
214
- enumerable: false,
215
- });
216
- return boundFn;
217
- },
218
- };
219
- }
220
-
221
- module.exports = autoBind;
222
- module.exports.default = autoBind;
223
- module.exports.autoBind = autoBind;
224
- module.exports.autoBindReact = autoBindReact;
225
- module.exports.boundClass = boundClass;
226
- module.exports.bound = bound;
1
+ /**
2
+ * auto-bind-js (CommonJS)
3
+ */
4
+
5
+ 'use strict';
6
+
7
+ const BUILTIN_OBJECT_METHODS = new Set([
8
+ 'constructor',
9
+ 'toString',
10
+ 'toLocaleString',
11
+ 'valueOf',
12
+ 'hasOwnProperty',
13
+ 'isPrototypeOf',
14
+ 'propertyIsEnumerable',
15
+ '__defineGetter__',
16
+ '__defineSetter__',
17
+ '__lookupGetter__',
18
+ '__lookupSetter__',
19
+ ]);
20
+
21
+ function getAllMethodNames(obj) {
22
+ const methods = new Set();
23
+ let proto = Object.getPrototypeOf(obj);
24
+
25
+ while (proto && proto !== Object.prototype) {
26
+ const keys = Object.getOwnPropertyNames(proto);
27
+ for (const key of keys) {
28
+ if (key === 'constructor') continue;
29
+ const descriptor = Object.getOwnPropertyDescriptor(proto, key);
30
+ if (descriptor && typeof descriptor.value === 'function') {
31
+ methods.add(key);
32
+ }
33
+ }
34
+
35
+ const symbols = Object.getOwnPropertySymbols(proto);
36
+ for (const sym of symbols) {
37
+ const descriptor = Object.getOwnPropertyDescriptor(proto, sym);
38
+ if (descriptor && typeof descriptor.value === 'function') {
39
+ methods.add(sym);
40
+ }
41
+ }
42
+
43
+ proto = Object.getPrototypeOf(proto);
44
+ }
45
+
46
+ return methods;
47
+ }
48
+
49
+ function filterMethods(methods, options = {}) {
50
+ let filtered = [...methods];
51
+
52
+ filtered = filtered.filter((m) => typeof m === 'symbol' || !BUILTIN_OBJECT_METHODS.has(m));
53
+
54
+ if (options.include) {
55
+ const includeSet = new Set(options.include);
56
+ filtered = filtered.filter((m) => includeSet.has(m));
57
+ }
58
+
59
+ if (options.exclude) {
60
+ const excludeSet = new Set(options.exclude);
61
+ filtered = filtered.filter((m) => !excludeSet.has(m));
62
+ }
63
+
64
+ if (options.pattern) {
65
+ filtered = filtered.filter(
66
+ (m) => typeof m === 'string' && options.pattern.test(m)
67
+ );
68
+ }
69
+
70
+ return filtered;
71
+ }
72
+
73
+ function bindEager(self, methods) {
74
+ for (const method of methods) {
75
+ const val = self[method];
76
+ if (typeof val === 'function') {
77
+ Object.defineProperty(self, method, {
78
+ value: val.bind(self),
79
+ writable: true,
80
+ configurable: true,
81
+ enumerable: false,
82
+ });
83
+ }
84
+ }
85
+ }
86
+
87
+ function findDescriptorInChain(obj, key) {
88
+ let proto = Object.getPrototypeOf(obj);
89
+ while (proto && proto !== Object.prototype) {
90
+ const desc = Object.getOwnPropertyDescriptor(proto, key);
91
+ if (desc) return desc;
92
+ proto = Object.getPrototypeOf(proto);
93
+ }
94
+ return undefined;
95
+ }
96
+
97
+ function bindLazy(self, methods) {
98
+ for (const method of methods) {
99
+ const proto = Object.getPrototypeOf(self);
100
+ const descriptor = Object.getOwnPropertyDescriptor(proto, method) ||
101
+ findDescriptorInChain(self, method);
102
+
103
+ if (!descriptor || typeof descriptor.value !== 'function') continue;
104
+
105
+ const originalFn = descriptor.value;
106
+
107
+ Object.defineProperty(self, method, {
108
+ configurable: true,
109
+ enumerable: false,
110
+ get() {
111
+ const boundFn = originalFn.bind(self);
112
+ Object.defineProperty(self, method, {
113
+ value: boundFn,
114
+ writable: true,
115
+ configurable: true,
116
+ enumerable: false,
117
+ });
118
+ return boundFn;
119
+ },
120
+ });
121
+ }
122
+ }
123
+
124
+ function autoBind(self, options) {
125
+ if (typeof options === 'string') {
126
+ const methodNames = [options, ...Array.from(arguments).slice(2)];
127
+ options = { include: methodNames };
128
+ }
129
+
130
+ const allMethods = getAllMethodNames(self);
131
+ const methods = filterMethods(allMethods, options);
132
+
133
+ if (options && options.lazy) {
134
+ bindLazy(self, methods);
135
+ } else {
136
+ bindEager(self, methods);
137
+ }
138
+
139
+ return self;
140
+ }
141
+
142
+ // React
143
+ const REACT_LIFECYCLE_METHODS = new Set([
144
+ 'render',
145
+ 'componentDidMount',
146
+ 'componentDidUpdate',
147
+ 'componentWillUnmount',
148
+ 'shouldComponentUpdate',
149
+ 'getSnapshotBeforeUpdate',
150
+ 'getDerivedStateFromProps',
151
+ 'getDerivedStateFromError',
152
+ 'componentDidCatch',
153
+ 'UNSAFE_componentWillMount',
154
+ 'UNSAFE_componentWillReceiveProps',
155
+ 'UNSAFE_componentWillUpdate',
156
+ 'getDefaultProps',
157
+ 'getInitialState',
158
+ 'componentWillMount',
159
+ 'componentWillReceiveProps',
160
+ 'componentWillUpdate',
161
+ ]);
162
+
163
+ function autoBindReact(self, options = {}) {
164
+ const exclude = [
165
+ ...(options.exclude || []),
166
+ ...REACT_LIFECYCLE_METHODS,
167
+ ];
168
+ return autoBind(self, { ...options, exclude });
169
+ }
170
+
171
+ // Decorators
172
+ function boundClass(target) {
173
+ const original = target;
174
+
175
+ const wrapped = function (...args) {
176
+ const instance = new original(...args);
177
+ autoBind(instance);
178
+ return instance;
179
+ };
180
+
181
+ wrapped.prototype = original.prototype;
182
+ Object.defineProperty(wrapped, 'name', { value: original.name });
183
+ Object.setPrototypeOf(wrapped, original);
184
+
185
+ for (const key of Object.getOwnPropertyNames(original)) {
186
+ if (['length', 'name', 'prototype', 'arguments', 'caller'].includes(key)) continue;
187
+ const desc = Object.getOwnPropertyDescriptor(original, key);
188
+ if (desc) Object.defineProperty(wrapped, key, desc);
189
+ }
190
+
191
+ return wrapped;
192
+ }
193
+
194
+ function bound(target, key, descriptor) {
195
+ if (!descriptor || typeof descriptor.value !== 'function') {
196
+ throw new TypeError('@bound can only be applied to methods');
197
+ }
198
+
199
+ const fn = descriptor.value;
200
+
201
+ return {
202
+ configurable: true,
203
+ enumerable: false,
204
+ get() {
205
+ if (this === target) {
206
+ return fn;
207
+ }
208
+
209
+ const boundFn = fn.bind(this);
210
+ Object.defineProperty(this, key, {
211
+ value: boundFn,
212
+ writable: true,
213
+ configurable: true,
214
+ enumerable: false,
215
+ });
216
+ return boundFn;
217
+ },
218
+ };
219
+ }
220
+
221
+ module.exports = autoBind;
222
+ module.exports.default = autoBind;
223
+ module.exports.autoBind = autoBind;
224
+ module.exports.autoBindReact = autoBindReact;
225
+ module.exports.boundClass = boundClass;
226
+ module.exports.bound = bound;
package/dist/index.d.ts CHANGED
@@ -1,80 +1,80 @@
1
- export interface AutoBindOptions {
2
- /** Only bind these methods */
3
- include?: (string | symbol)[];
4
- /** Don't bind these methods */
5
- exclude?: (string | symbol)[];
6
- /** Only bind methods matching this regex */
7
- pattern?: RegExp;
8
- /** Use lazy binding (bind on first access via getter) */
9
- lazy?: boolean;
10
- }
11
-
12
- /**
13
- * Automatically bind all methods of an object to itself.
14
- *
15
- * @param self - The class instance (usually `this`)
16
- * @param options - Configuration options
17
- * @returns The instance (for chaining)
18
- *
19
- * @example
20
- * ```ts
21
- * class MyClass {
22
- * constructor() {
23
- * autoBind(this);
24
- * }
25
- * myMethod() {
26
- * return this;
27
- * }
28
- * }
29
- * ```
30
- *
31
- * @example
32
- * ```ts
33
- * // Shorthand: bind specific methods
34
- * autoBind(this, 'method1', 'method2');
35
- * ```
36
- */
37
- declare function autoBind<T extends object>(self: T, options?: AutoBindOptions): T;
38
- declare function autoBind<T extends object>(self: T, ...methods: string[]): T;
39
-
40
- /**
41
- * React-aware autoBind. Automatically excludes React lifecycle methods.
42
- *
43
- * @param self - The React component instance
44
- * @param options - Same options as autoBind
45
- * @returns The instance
46
- */
47
- export declare function autoBindReact<T extends object>(self: T, options?: AutoBindOptions): T;
48
-
49
- /**
50
- * Class decorator that auto-binds all methods on instantiation.
51
- *
52
- * @example
53
- * ```ts
54
- * @boundClass
55
- * class MyClass {
56
- * handleClick() { ... }
57
- * }
58
- * ```
59
- */
60
- export declare function boundClass<T extends new (...args: any[]) => any>(target: T): T;
61
-
62
- /**
63
- * Method decorator that lazily binds the method to the instance.
64
- *
65
- * @example
66
- * ```ts
67
- * class MyClass {
68
- * @bound
69
- * handleClick() { ... }
70
- * }
71
- * ```
72
- */
73
- export declare function bound(
74
- target: object,
75
- key: string | symbol,
76
- descriptor: PropertyDescriptor
77
- ): PropertyDescriptor;
78
-
79
- export default autoBind;
80
- export { autoBind };
1
+ export interface AutoBindOptions {
2
+ /** Only bind these methods */
3
+ include?: (string | symbol)[];
4
+ /** Don't bind these methods */
5
+ exclude?: (string | symbol)[];
6
+ /** Only bind methods matching this regex */
7
+ pattern?: RegExp;
8
+ /** Use lazy binding (bind on first access via getter) */
9
+ lazy?: boolean;
10
+ }
11
+
12
+ /**
13
+ * Automatically bind all methods of an object to itself.
14
+ *
15
+ * @param self - The class instance (usually `this`)
16
+ * @param options - Configuration options
17
+ * @returns The instance (for chaining)
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * class MyClass {
22
+ * constructor() {
23
+ * autoBind(this);
24
+ * }
25
+ * myMethod() {
26
+ * return this;
27
+ * }
28
+ * }
29
+ * ```
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // Shorthand: bind specific methods
34
+ * autoBind(this, 'method1', 'method2');
35
+ * ```
36
+ */
37
+ declare function autoBind<T extends object>(self: T, options?: AutoBindOptions): T;
38
+ declare function autoBind<T extends object>(self: T, ...methods: string[]): T;
39
+
40
+ /**
41
+ * React-aware autoBind. Automatically excludes React lifecycle methods.
42
+ *
43
+ * @param self - The React component instance
44
+ * @param options - Same options as autoBind
45
+ * @returns The instance
46
+ */
47
+ export declare function autoBindReact<T extends object>(self: T, options?: AutoBindOptions): T;
48
+
49
+ /**
50
+ * Class decorator that auto-binds all methods on instantiation.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * @boundClass
55
+ * class MyClass {
56
+ * handleClick() { ... }
57
+ * }
58
+ * ```
59
+ */
60
+ export declare function boundClass<T extends new (...args: any[]) => any>(target: T): T;
61
+
62
+ /**
63
+ * Method decorator that lazily binds the method to the instance.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * class MyClass {
68
+ * @bound
69
+ * handleClick() { ... }
70
+ * }
71
+ * ```
72
+ */
73
+ export declare function bound(
74
+ target: object,
75
+ key: string | symbol,
76
+ descriptor: PropertyDescriptor
77
+ ): PropertyDescriptor;
78
+
79
+ export default autoBind;
80
+ export { autoBind };