Skip to content

Commit a0e165c

Browse files
committed
[Refactor] use set-function-length
1 parent 780eb36 commit a0e165c

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

index.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
var bind = require('function-bind');
44
var GetIntrinsic = require('get-intrinsic');
5+
var setFunctionLength = require('set-function-length');
56

67
var $apply = GetIntrinsic('%Function.prototype.apply%');
78
var $call = GetIntrinsic('%Function.prototype.call%');
89
var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply);
910

10-
var $gOPD = GetIntrinsic('%Object.getOwnPropertyDescriptor%', true);
1111
var $defineProperty = GetIntrinsic('%Object.defineProperty%', true);
1212
var $max = GetIntrinsic('%Math.max%');
1313

@@ -22,18 +22,11 @@ if ($defineProperty) {
2222

2323
module.exports = function callBind(originalFunction) {
2424
var func = $reflectApply(bind, $call, arguments);
25-
if ($gOPD && $defineProperty) {
26-
var desc = $gOPD(func, 'length');
27-
if (desc.configurable) {
28-
// original length, plus the receiver, minus any additional arguments (after the receiver)
29-
$defineProperty(
30-
func,
31-
'length',
32-
{ value: 1 + $max(0, originalFunction.length - (arguments.length - 1)) }
33-
);
34-
}
35-
}
36-
return func;
25+
return setFunctionLength(
26+
func,
27+
1 + $max(0, originalFunction.length - (arguments.length - 1)),
28+
true
29+
);
3730
};
3831

3932
var applyBind = function applyBind() {

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"auto-changelog": "^2.4.0",
5656
"eslint": "=8.8.0",
5757
"evalmd": "^0.0.19",
58+
"gopd": "^1.0.1",
59+
"has-strict-mode": "^1.0.1",
5860
"in-publish": "^2.0.1",
5961
"npmignore": "^0.3.0",
6062
"nyc": "^10.3.2",
@@ -63,7 +65,11 @@
6365
},
6466
"dependencies": {
6567
"function-bind": "^1.1.2",
66-
"get-intrinsic": "^1.2.1"
68+
"get-intrinsic": "^1.2.1",
69+
"set-function-length": "^1.1.0"
70+
},
71+
"testling": {
72+
"files": "test/index.js"
6773
},
6874
"auto-changelog": {
6975
"output": "CHANGELOG.md",

test/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var callBind = require('../');
44
var bind = require('function-bind');
5+
var gOPD = require('gopd');
6+
var hasStrictMode = require('has-strict-mode')();
57

68
var test = require('tape');
79

@@ -10,15 +12,16 @@ var test = require('tape');
1012
* in io.js v3, it is configurable except on bound functions, hence the .bind()
1113
*/
1214
var functionsHaveConfigurableLengths = !!(
13-
Object.getOwnPropertyDescriptor
15+
gOPD
16+
&& Object.getOwnPropertyDescriptor
1417
&& Object.getOwnPropertyDescriptor(bind.call(function () {}), 'length').configurable
1518
);
1619

1720
test('callBind', function (t) {
1821
var sentinel = { sentinel: true };
1922
var func = function (a, b) {
2023
// eslint-disable-next-line no-invalid-this
21-
return [this, a, b];
24+
return [!hasStrictMode && this === global ? undefined : this, a, b];
2225
};
2326
t.equal(func.length, 2, 'original function length is 2');
2427
t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
@@ -28,8 +31,8 @@ test('callBind', function (t) {
2831
var bound = callBind(func);
2932
t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
3033
t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
31-
t.deepEqual(bound(1, 2), [1, 2, undefined], 'bound func with right args');
32-
t.deepEqual(bound(1, 2, 3), [1, 2, 3], 'bound func with too many args');
34+
t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args');
35+
t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
3336

3437
var boundR = callBind(func, sentinel);
3538
t.equal(boundR.length, func.length, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });

0 commit comments

Comments
 (0)