es-toolkit 1.37.2-dev.1273 → 1.37.2-dev.1275

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.d.mts CHANGED
@@ -169,4 +169,5 @@ export { upperFirst } from './string/upperFirst.mjs';
169
169
  export { words } from './string/words.mjs';
170
170
  export { attempt } from './util/attempt.mjs';
171
171
  export { attemptAsync } from './util/attemptAsync.mjs';
172
+ export { bindAll } from './util/bindAll.mjs';
172
173
  export { invariant } from './util/invariant.mjs';
package/dist/index.d.ts CHANGED
@@ -169,4 +169,5 @@ export { upperFirst } from './string/upperFirst.js';
169
169
  export { words } from './string/words.js';
170
170
  export { attempt } from './util/attempt.js';
171
171
  export { attemptAsync } from './util/attemptAsync.js';
172
+ export { bindAll } from './util/bindAll.js';
172
173
  export { invariant } from './util/invariant.js';
package/dist/index.js CHANGED
@@ -199,4 +199,5 @@ exports.upperCase = upperFirst.upperCase;
199
199
  exports.upperFirst = upperFirst.upperFirst;
200
200
  exports.attempt = util_index.attempt;
201
201
  exports.attemptAsync = util_index.attemptAsync;
202
+ exports.bindAll = util_index.bindAll;
202
203
  exports.invariant = util_index.invariant;
package/dist/index.mjs CHANGED
@@ -169,4 +169,5 @@ export { upperFirst } from './string/upperFirst.mjs';
169
169
  export { words } from './string/words.mjs';
170
170
  export { attempt } from './util/attempt.mjs';
171
171
  export { attemptAsync } from './util/attemptAsync.mjs';
172
+ export { bindAll } from './util/bindAll.mjs';
172
173
  export { invariant } from './util/invariant.mjs';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Binds methods of an object to the object itself, overwriting the existing method.
3
+ * Method names may be specified as individual arguments or as arrays of method names.
4
+ *
5
+ * @template T - Type of the object with methods to bind.
6
+ * @param {T} object - The object to bind methods to.
7
+ * @param {...(string|string[])} methodNames - The method names to bind, specified individually or in arrays.
8
+ * @returns {T} - Returns the object with bound methods.
9
+ *
10
+ * @example
11
+ * const view = {
12
+ * 'label': 'docs',
13
+ * 'click': function() {
14
+ * console.log('clicked ' + this.label);
15
+ * }
16
+ * };
17
+ *
18
+ * bindAll(view, ['click']);
19
+ * jQuery(element).on('click', view.click);
20
+ * // => Logs 'clicked docs' when clicked.
21
+ */
22
+ declare function bindAll<T extends {
23
+ [key: string]: unknown;
24
+ }>(object: T, ...methodNames: Array<string | string[] | number | IArguments>): T;
25
+
26
+ export { bindAll };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Binds methods of an object to the object itself, overwriting the existing method.
3
+ * Method names may be specified as individual arguments or as arrays of method names.
4
+ *
5
+ * @template T - Type of the object with methods to bind.
6
+ * @param {T} object - The object to bind methods to.
7
+ * @param {...(string|string[])} methodNames - The method names to bind, specified individually or in arrays.
8
+ * @returns {T} - Returns the object with bound methods.
9
+ *
10
+ * @example
11
+ * const view = {
12
+ * 'label': 'docs',
13
+ * 'click': function() {
14
+ * console.log('clicked ' + this.label);
15
+ * }
16
+ * };
17
+ *
18
+ * bindAll(view, ['click']);
19
+ * jQuery(element).on('click', view.click);
20
+ * // => Logs 'clicked docs' when clicked.
21
+ */
22
+ declare function bindAll<T extends {
23
+ [key: string]: unknown;
24
+ }>(object: T, ...methodNames: Array<string | string[] | number | IArguments>): T;
25
+
26
+ export { bindAll };
@@ -0,0 +1,29 @@
1
+ function bindAll(object, ...methodNames) {
2
+ if (Array.isArray(object) && methodNames.length === 0) {
3
+ return object;
4
+ }
5
+ const bindValue = (obj, key) => {
6
+ const value = obj[key];
7
+ if (typeof value === 'function') {
8
+ obj[key] = value.bind(obj);
9
+ }
10
+ };
11
+ methodNames.forEach(name => {
12
+ if (Array.isArray(name)) {
13
+ name.forEach(key => bindValue(object, String(key)));
14
+ }
15
+ else if (typeof name === 'number') {
16
+ const key = Object.is(name, -0) ? '-0' : String(name);
17
+ bindValue(object, key);
18
+ }
19
+ else if (name && typeof name === 'object' && 'length' in name) {
20
+ Array.from(name).forEach(key => bindValue(object, String(key)));
21
+ }
22
+ else if (name) {
23
+ bindValue(object, String(name));
24
+ }
25
+ });
26
+ return object;
27
+ }
28
+
29
+ export { bindAll };
@@ -1,3 +1,4 @@
1
1
  export { attempt } from './attempt.mjs';
2
2
  export { attemptAsync } from './attemptAsync.mjs';
3
+ export { bindAll } from './bindAll.mjs';
3
4
  export { invariant } from './invariant.mjs';
@@ -1,3 +1,4 @@
1
1
  export { attempt } from './attempt.js';
2
2
  export { attemptAsync } from './attemptAsync.js';
3
+ export { bindAll } from './bindAll.js';
3
4
  export { invariant } from './invariant.js';
@@ -21,6 +21,34 @@ async function attemptAsync(func) {
21
21
  }
22
22
  }
23
23
 
24
+ function bindAll(object, ...methodNames) {
25
+ if (Array.isArray(object) && methodNames.length === 0) {
26
+ return object;
27
+ }
28
+ const bindValue = (obj, key) => {
29
+ const value = obj[key];
30
+ if (typeof value === 'function') {
31
+ obj[key] = value.bind(obj);
32
+ }
33
+ };
34
+ methodNames.forEach(name => {
35
+ if (Array.isArray(name)) {
36
+ name.forEach(key => bindValue(object, String(key)));
37
+ }
38
+ else if (typeof name === 'number') {
39
+ const key = Object.is(name, -0) ? '-0' : String(name);
40
+ bindValue(object, key);
41
+ }
42
+ else if (name && typeof name === 'object' && 'length' in name) {
43
+ Array.from(name).forEach(key => bindValue(object, String(key)));
44
+ }
45
+ else if (name) {
46
+ bindValue(object, String(name));
47
+ }
48
+ });
49
+ return object;
50
+ }
51
+
24
52
  function invariant(condition, message) {
25
53
  if (condition) {
26
54
  return;
@@ -30,4 +58,5 @@ function invariant(condition, message) {
30
58
 
31
59
  exports.attempt = attempt;
32
60
  exports.attemptAsync = attemptAsync;
61
+ exports.bindAll = bindAll;
33
62
  exports.invariant = invariant;
@@ -1,3 +1,4 @@
1
1
  export { attempt } from './attempt.mjs';
2
2
  export { attemptAsync } from './attemptAsync.mjs';
3
+ export { bindAll } from './bindAll.mjs';
3
4
  export { invariant } from './invariant.mjs';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.37.2-dev.1273+cdc77fe5",
4
+ "version": "1.37.2-dev.1275+3de4a303",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {