Comparing version
@@ -8,2 +8,15 @@ # Changelog | ||
## [v1.0.8](https://github.com/ljharb/call-bind/compare/v1.0.7...v1.0.8) - 2024-12-05 | ||
### Commits | ||
- [Refactor] extract out some helpers and avoid get-intrinsic usage [`407fd5e`](https://github.com/ljharb/call-bind/commit/407fd5eec34ec58394522a6ce3badfa4788fd5ae) | ||
- [Refactor] replace code with extracted `call-bind-apply-helpers` [`81018fb`](https://github.com/ljharb/call-bind/commit/81018fb78902ff5acbc6c09300780e97f0db6a34) | ||
- [Tests] use `set-function-length/env` [`0fc311d`](https://github.com/ljharb/call-bind/commit/0fc311de0e115cfa6b02969b23a42ad45aadf224) | ||
- [actions] split out node 10-20, and 20+ [`77a0cad`](https://github.com/ljharb/call-bind/commit/77a0cad75f83f5b8050dc13baef4fa2cff537fa3) | ||
- [Dev Deps] update `@ljharb/eslint-config`, `auto-changelog`, `es-value-fixtures`, `gopd`, `object-inspect`, `tape` [`a145d10`](https://github.com/ljharb/call-bind/commit/a145d10fe847f350e11094f8541848b028ee8c91) | ||
- [Tests] replace `aud` with `npm audit` [`30ca3dd`](https://github.com/ljharb/call-bind/commit/30ca3dd7234648eb029947477d06b17879e10727) | ||
- [Deps] update `set-function-length` [`57c79a3`](https://github.com/ljharb/call-bind/commit/57c79a3666022ea797cc2a4a3b43fe089bc97d1b) | ||
- [Dev Deps] add missing peer dep [`601cfa5`](https://github.com/ljharb/call-bind/commit/601cfa5540066b6206039ceb9496cecbd134ff7b) | ||
## [v1.0.7](https://github.com/ljharb/call-bind/compare/v1.0.6...v1.0.7) - 2024-02-12 | ||
@@ -10,0 +23,0 @@ |
23
index.js
'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('call-bind-apply-helpers'); | ||
var applyBind = require('call-bind-apply-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 | ||
@@ -27,6 +20,2 @@ ); | ||
var applyBind = function applyBind() { | ||
return $reflectApply(bind, $apply, arguments); | ||
}; | ||
if ($defineProperty) { | ||
@@ -33,0 +22,0 @@ $defineProperty(module.exports, 'apply', { value: applyBind }); |
{ | ||
"name": "call-bind", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Robustly `.call.bind()` a function", | ||
@@ -20,3 +20,3 @@ "main": "index.js", | ||
"test": "npm run tests-only", | ||
"posttest": "aud --production", | ||
"posttest": "npx npm@'>=10.2' audit --production", | ||
"version": "auto-changelog && git add CHANGELOG.md", | ||
@@ -53,11 +53,16 @@ "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" | ||
"homepage": "https://github.com/ljharb/call-bind#readme", | ||
"dependencies": { | ||
"call-bind-apply-helpers": "^1.0.0", | ||
"es-define-property": "^1.0.0", | ||
"get-intrinsic": "^1.2.4", | ||
"set-function-length": "^1.2.2" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^21.1.0", | ||
"aud": "^2.0.4", | ||
"auto-changelog": "^2.4.0", | ||
"es-value-fixtures": "^1.4.2", | ||
"@ljharb/eslint-config": "^21.1.1", | ||
"auto-changelog": "^2.5.0", | ||
"encoding": "^0.1.13", | ||
"es-value-fixtures": "^1.5.0", | ||
"eslint": "=8.8.0", | ||
"evalmd": "^0.0.19", | ||
"for-each": "^0.3.3", | ||
"gopd": "^1.0.1", | ||
"has-strict-mode": "^1.0.1", | ||
@@ -67,13 +72,6 @@ "in-publish": "^2.0.1", | ||
"nyc": "^10.3.2", | ||
"object-inspect": "^1.13.1", | ||
"object-inspect": "^1.13.3", | ||
"safe-publish-latest": "^2.0.0", | ||
"tape": "^5.7.4" | ||
"tape": "^5.9.0" | ||
}, | ||
"dependencies": { | ||
"es-define-property": "^1.0.0", | ||
"es-errors": "^1.3.0", | ||
"function-bind": "^1.1.2", | ||
"get-intrinsic": "^1.2.4", | ||
"set-function-length": "^1.2.1" | ||
}, | ||
"testling": { | ||
@@ -80,0 +78,0 @@ "files": "test/index.js" |
'use strict'; | ||
var callBind = require('../'); | ||
var bind = require('function-bind'); | ||
var gOPD = require('gopd'); | ||
var hasStrictMode = require('has-strict-mode')(); | ||
@@ -17,7 +15,3 @@ var forEach = require('for-each'); | ||
*/ | ||
var functionsHaveConfigurableLengths = !!( | ||
gOPD | ||
&& Object.getOwnPropertyDescriptor | ||
&& Object.getOwnPropertyDescriptor(bind.call(function () {}), 'length').configurable | ||
); | ||
var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths; | ||
@@ -44,3 +38,3 @@ test('callBind', function (t) { | ||
var bound = callBind(func); | ||
t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths }); | ||
t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths }); | ||
t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args'); | ||
@@ -51,3 +45,3 @@ t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args'); | ||
var boundR = callBind(func, sentinel); | ||
t.equal(boundR.length, func.length, 'function length is preserved', { skip: !functionsHaveConfigurableLengths }); | ||
t.equal(boundR.length, func.length, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths }); | ||
t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args'); | ||
@@ -58,3 +52,3 @@ t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args'); | ||
var boundArg = callBind(func, sentinel, 1); | ||
t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths }); | ||
t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths }); | ||
t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args'); | ||
@@ -61,0 +55,0 @@ t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg'); |
22902
3.54%4
-20%14
-6.67%135
-10.6%- Removed
- Removed
Updated