es-toolkit 1.13.1-dev.368 → 1.13.1-dev.371

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.
@@ -55,6 +55,7 @@ export { noop } from '../function/noop.mjs';
55
55
  export { once } from '../function/once.mjs';
56
56
  export { throttle } from '../function/throttle.mjs';
57
57
  export { negate } from '../function/negate.mjs';
58
+ export { bind } from '../function/bind.mjs';
58
59
  export { clamp } from '../math/clamp.mjs';
59
60
  export { inRange } from '../math/inRange.mjs';
60
61
  export { mean } from '../math/mean.mjs';
@@ -55,6 +55,7 @@ export { noop } from '../function/noop.js';
55
55
  export { once } from '../function/once.js';
56
56
  export { throttle } from '../function/throttle.js';
57
57
  export { negate } from '../function/negate.js';
58
+ export { bind } from '../function/bind.js';
58
59
  export { clamp } from '../math/clamp.js';
59
60
  export { inRange } from '../math/inRange.js';
60
61
  export { mean } from '../math/mean.js';
@@ -462,6 +462,7 @@ exports.delay = promise_index.delay;
462
462
  exports.withTimeout = promise_index.withTimeout;
463
463
  exports.after = function_index.after;
464
464
  exports.before = function_index.before;
465
+ exports.bind = function_index.bind;
465
466
  exports.debounce = function_index.debounce;
466
467
  exports.negate = function_index.negate;
467
468
  exports.noop = function_index.noop;
@@ -55,6 +55,7 @@ export { noop } from '../function/noop.mjs';
55
55
  export { once } from '../function/once.mjs';
56
56
  export { throttle } from '../function/throttle.mjs';
57
57
  export { negate } from '../function/negate.mjs';
58
+ export { bind } from '../function/bind.mjs';
58
59
  export { clamp } from '../math/clamp.mjs';
59
60
  export { inRange } from '../math/inRange.mjs';
60
61
  export { mean } from '../math/mean.mjs';
@@ -0,0 +1,32 @@
1
+ /**
2
+ *
3
+ * Creates a function that invokes `func` with the `this` binding of `thisArg` and `partials` prepended to the arguments it receives.
4
+ *
5
+ * The `bind.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
6
+ *
7
+ * **Note:** Unlike native `Function#bind`, this method doesn't set the `length` property of bound functions.
8
+ *
9
+ * @param {(...args: any[]) => any} func The function to bind.
10
+ * @param {any} thisArg The `this` binding of `func`.
11
+ * @param {any[]} partials The arguments to be partially applied.
12
+ * @returns {(...args: any[]) => any} Returns the new bound function.
13
+ *
14
+ * @example
15
+ * function greet(greeting, punctuation) {
16
+ * return greeting + ' ' + this.user + punctuation;
17
+ * }
18
+ * const object = { user: 'fred' };
19
+ * let bound = bind(greet, object, 'hi');
20
+ * bound('!');
21
+ * // => 'hi fred!'
22
+ *
23
+ * bound = bind(greet, object, bind.placeholder, '!');
24
+ * bound('hi');
25
+ * // => 'hi fred!'
26
+ */
27
+ declare function bind(func: (...args: any[]) => any, thisArg?: any, ...partials: any[]): (...args: any[]) => any;
28
+ declare namespace bind {
29
+ var placeholder: symbol;
30
+ }
31
+
32
+ export { bind };
@@ -0,0 +1,32 @@
1
+ /**
2
+ *
3
+ * Creates a function that invokes `func` with the `this` binding of `thisArg` and `partials` prepended to the arguments it receives.
4
+ *
5
+ * The `bind.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
6
+ *
7
+ * **Note:** Unlike native `Function#bind`, this method doesn't set the `length` property of bound functions.
8
+ *
9
+ * @param {(...args: any[]) => any} func The function to bind.
10
+ * @param {any} thisArg The `this` binding of `func`.
11
+ * @param {any[]} partials The arguments to be partially applied.
12
+ * @returns {(...args: any[]) => any} Returns the new bound function.
13
+ *
14
+ * @example
15
+ * function greet(greeting, punctuation) {
16
+ * return greeting + ' ' + this.user + punctuation;
17
+ * }
18
+ * const object = { user: 'fred' };
19
+ * let bound = bind(greet, object, 'hi');
20
+ * bound('!');
21
+ * // => 'hi fred!'
22
+ *
23
+ * bound = bind(greet, object, bind.placeholder, '!');
24
+ * bound('hi');
25
+ * // => 'hi fred!'
26
+ */
27
+ declare function bind(func: (...args: any[]) => any, thisArg?: any, ...partials: any[]): (...args: any[]) => any;
28
+ declare namespace bind {
29
+ var placeholder: symbol;
30
+ }
31
+
32
+ export { bind };
@@ -0,0 +1,22 @@
1
+ function bind(func, thisArg, ...partials) {
2
+ const wrapper = function (...args) {
3
+ let index = 0;
4
+ const result = partials.map(bindArg => {
5
+ if (bindArg === bind.placeholder) {
6
+ return args[index++];
7
+ }
8
+ return bindArg;
9
+ });
10
+ for (let i = index; i < args.length; i++) {
11
+ result.push(args[i]);
12
+ }
13
+ if (this instanceof wrapper) {
14
+ return new func(...result);
15
+ }
16
+ return func.apply(thisArg, result);
17
+ };
18
+ return wrapper;
19
+ }
20
+ bind.placeholder = Symbol('bind.placeholder');
21
+
22
+ export { bind };
@@ -5,3 +5,4 @@ export { noop } from './noop.mjs';
5
5
  export { once } from './once.mjs';
6
6
  export { throttle } from './throttle.mjs';
7
7
  export { negate } from './negate.mjs';
8
+ export { bind } from './bind.mjs';
@@ -5,3 +5,4 @@ export { noop } from './noop.js';
5
5
  export { once } from './once.js';
6
6
  export { throttle } from './throttle.js';
7
7
  export { negate } from './negate.js';
8
+ export { bind } from './bind.js';
@@ -87,8 +87,30 @@ function negate(func) {
87
87
  return ((...args) => !func(...args));
88
88
  }
89
89
 
90
+ function bind(func, thisArg, ...partials) {
91
+ const wrapper = function (...args) {
92
+ let index = 0;
93
+ const result = partials.map(bindArg => {
94
+ if (bindArg === bind.placeholder) {
95
+ return args[index++];
96
+ }
97
+ return bindArg;
98
+ });
99
+ for (let i = index; i < args.length; i++) {
100
+ result.push(args[i]);
101
+ }
102
+ if (this instanceof wrapper) {
103
+ return new func(...result);
104
+ }
105
+ return func.apply(thisArg, result);
106
+ };
107
+ return wrapper;
108
+ }
109
+ bind.placeholder = Symbol('bind.placeholder');
110
+
90
111
  exports.after = after;
91
112
  exports.before = before;
113
+ exports.bind = bind;
92
114
  exports.debounce = debounce;
93
115
  exports.negate = negate;
94
116
  exports.noop = noop;
@@ -5,3 +5,4 @@ export { noop } from './noop.mjs';
5
5
  export { once } from './once.mjs';
6
6
  export { throttle } from './throttle.mjs';
7
7
  export { negate } from './negate.mjs';
8
+ export { bind } from './bind.mjs';
package/dist/index.d.mts CHANGED
@@ -60,6 +60,7 @@ export { noop } from './function/noop.mjs';
60
60
  export { once } from './function/once.mjs';
61
61
  export { throttle } from './function/throttle.mjs';
62
62
  export { negate } from './function/negate.mjs';
63
+ export { bind } from './function/bind.mjs';
63
64
  export { clamp } from './math/clamp.mjs';
64
65
  export { inRange } from './math/inRange.mjs';
65
66
  export { mean } from './math/mean.mjs';
package/dist/index.d.ts CHANGED
@@ -60,6 +60,7 @@ export { noop } from './function/noop.js';
60
60
  export { once } from './function/once.js';
61
61
  export { throttle } from './function/throttle.js';
62
62
  export { negate } from './function/negate.js';
63
+ export { bind } from './function/bind.js';
63
64
  export { clamp } from './math/clamp.js';
64
65
  export { inRange } from './math/inRange.js';
65
66
  export { mean } from './math/mean.js';
package/dist/index.js CHANGED
@@ -76,6 +76,7 @@ exports.delay = promise_index.delay;
76
76
  exports.withTimeout = promise_index.withTimeout;
77
77
  exports.after = function_index.after;
78
78
  exports.before = function_index.before;
79
+ exports.bind = function_index.bind;
79
80
  exports.debounce = function_index.debounce;
80
81
  exports.negate = function_index.negate;
81
82
  exports.noop = function_index.noop;
package/dist/index.mjs CHANGED
@@ -60,6 +60,7 @@ export { noop } from './function/noop.mjs';
60
60
  export { once } from './function/once.mjs';
61
61
  export { throttle } from './function/throttle.mjs';
62
62
  export { negate } from './function/negate.mjs';
63
+ export { bind } from './function/bind.mjs';
63
64
  export { clamp } from './math/clamp.mjs';
64
65
  export { inRange } from './math/inRange.mjs';
65
66
  export { mean } from './math/mean.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.13.1-dev.368+b0011fa0",
4
+ "version": "1.13.1-dev.371+86fe776f",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {