Skip to content

Commit

Permalink
[Refactor] extract out some helpers and avoid get-intrinsic usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 4, 2024
1 parent 57c79a3 commit 407fd5e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
9 changes: 9 additions & 0 deletions helpers/actualApply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var bind = require('function-bind');

var $apply = require('./functionApply');
var $call = require('./functionCall');
var $reflectApply = require('./reflectApply');

module.exports = $reflectApply || bind.call($call, $apply);
9 changes: 9 additions & 0 deletions helpers/applyBind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var bind = require('function-bind');
var $apply = require('./functionApply');
var actualApply = require('./actualApply');

module.exports = function applyBind() {
return actualApply(bind, $apply, arguments);
};
14 changes: 14 additions & 0 deletions helpers/callBindBasic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var bind = require('function-bind');
var $TypeError = require('es-errors/type');

var $call = require('./functionCall');
var actualApply = require('./actualApply');

module.exports = function callBindBasic(args) {
if (args.length < 1 || typeof args[0] !== 'function') {
throw new $TypeError('a function is required');
}
return actualApply(bind, $call, args);
};
3 changes: 3 additions & 0 deletions helpers/functionApply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = Function.prototype.apply;
3 changes: 3 additions & 0 deletions helpers/functionCall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = Function.prototype.call;
3 changes: 3 additions & 0 deletions helpers/reflectApply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = typeof Reflect === 'function' && Reflect.apply;
23 changes: 6 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
'use strict';

var bind = require('function-bind');
var GetIntrinsic = require('get-intrinsic');
var setFunctionLength = require('set-function-length');

var $TypeError = require('es-errors/type');
var $apply = GetIntrinsic('%Function.prototype.apply%');
var $call = GetIntrinsic('%Function.prototype.call%');
var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply);

var $defineProperty = require('es-define-property');
var $max = GetIntrinsic('%Math.max%');

var callBindBasic = require('./helpers/callBindBasic');
var applyBind = require('./helpers/applyBind');

module.exports = function callBind(originalFunction) {
if (typeof originalFunction !== 'function') {
throw new $TypeError('a function is required');
}
var func = $reflectApply(bind, $call, arguments);
var func = callBindBasic(arguments);
var adjustedLength = originalFunction.length - (arguments.length - 1);
return setFunctionLength(
func,
1 + $max(0, originalFunction.length - (arguments.length - 1)),
1 + (adjustedLength > 0 ? adjustedLength : 0),
true
);
};

var applyBind = function applyBind() {
return $reflectApply(bind, $apply, arguments);
};

if ($defineProperty) {
$defineProperty(module.exports, 'apply', { value: applyBind });
} else {
Expand Down

0 comments on commit 407fd5e

Please sign in to comment.