pax_global_header00006660000000000000000000000064147531726000014517gustar00rootroot0000000000000052 comment=7068296a67b98424d5299606599d2f8117ded880 ljharb-call-bind-apply-helpers-ef266c3/000077500000000000000000000000001475317260000200075ustar00rootroot00000000000000ljharb-call-bind-apply-helpers-ef266c3/.eslintrc000066400000000000000000000003501475317260000216310ustar00rootroot00000000000000{ "root": true, "extends": "@ljharb", "rules": { "func-name-matching": 0, "id-length": 0, "new-cap": [2, { "capIsNewExceptions": [ "GetIntrinsic", ], }], "no-extra-parens": 0, "no-magic-numbers": 0, }, } ljharb-call-bind-apply-helpers-ef266c3/.github/000077500000000000000000000000001475317260000213475ustar00rootroot00000000000000ljharb-call-bind-apply-helpers-ef266c3/.github/FUNDING.yml000066400000000000000000000011221475317260000231600ustar00rootroot00000000000000# These are supported funding model platforms github: [ljharb] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: npm/call-bind-apply-helpers community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/000077500000000000000000000000001475317260000234045ustar00rootroot00000000000000ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/node-aught.yml000066400000000000000000000005161475317260000261640ustar00rootroot00000000000000name: 'Tests: node.js < 10' on: [pull_request, push] jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: range: '< 10' type: minors command: npm run tests-only node: name: 'node < 10' needs: [tests] runs-on: ubuntu-latest steps: - run: 'echo tests completed' ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/node-pretest.yml000066400000000000000000000002061475317260000265360ustar00rootroot00000000000000name: 'Tests: pretest/posttest' on: [pull_request, push] jobs: tests: uses: ljharb/actions/.github/workflows/pretest.yml@main ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/node-tens.yml000066400000000000000000000003721475317260000260250ustar00rootroot00000000000000name: 'Tests: node.js 10 - 20' on: [pull_request, push] permissions: contents: read jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: range: '>= 10 < 20' type: minors command: npm run tests-only ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/node-twenties.yml000066400000000000000000000003631475317260000267160ustar00rootroot00000000000000name: 'Tests: node.js >= 20' on: [pull_request, push] permissions: contents: read jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: range: '>= 20' type: minors command: npm run tests-only ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/rebase.yml000066400000000000000000000002571475317260000253740ustar00rootroot00000000000000name: Automatic Rebase on: [pull_request_target] jobs: _: uses: ljharb/actions/.github/workflows/rebase.yml@main secrets: token: ${{ secrets.GITHUB_TOKEN }} ljharb-call-bind-apply-helpers-ef266c3/.github/workflows/require-allow-edits.yml000066400000000000000000000003031475317260000300210ustar00rootroot00000000000000name: Require “Allow Edits” on: [pull_request_target] jobs: _: name: "Require “Allow Edits”" runs-on: ubuntu-latest steps: - uses: ljharb/require-allow-edits@main ljharb-call-bind-apply-helpers-ef266c3/.gitignore000066400000000000000000000032511475317260000220000ustar00rootroot00000000000000# Logs logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage *.lcov # nyc test coverage .nyc_output # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) .grunt # Bower dependency directory (https://bower.io/) bower_components # node-waf configuration .lock-wscript # Compiled binary addons (https://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules/ jspm_packages/ # TypeScript v1 declaration files typings/ # TypeScript cache *.tsbuildinfo # Optional npm cache directory .npm # Optional eslint cache .eslintcache # Microbundle cache .rpt2_cache/ .rts2_cache_cjs/ .rts2_cache_es/ .rts2_cache_umd/ # Optional REPL history .node_repl_history # Output of 'npm pack' *.tgz # Yarn Integrity file .yarn-integrity # dotenv environment variables file .env .env.test # parcel-bundler cache (https://parceljs.org/) .cache # Next.js build output .next # Nuxt.js build / generate output .nuxt dist # Gatsby files .cache/ # Comment in the public line in if your project uses Gatsby and *not* Next.js # https://nextjs.org/blog/next-9-1#public-directory-support # public # vuepress build output .vuepress/dist # Serverless directories .serverless/ # FuseBox cache .fusebox/ # DynamoDB Local files .dynamodb/ # TernJS port file .tern-port # Only apps should have lockfiles npm-shrinkwrap.json package-lock.json yarn.lock .npmignore ljharb-call-bind-apply-helpers-ef266c3/.npmrc000066400000000000000000000000671475317260000211320ustar00rootroot00000000000000package-lock=false allow-same-version=true message=v%s ljharb-call-bind-apply-helpers-ef266c3/.nycrc000066400000000000000000000002131475317260000211220ustar00rootroot00000000000000{ "all": true, "check-coverage": false, "reporter": ["text-summary", "text", "html", "json"], "exclude": [ "coverage", "test" ] } ljharb-call-bind-apply-helpers-ef266c3/CHANGELOG.md000066400000000000000000000036221475317260000216230ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [v1.0.2](https://github.com/ljharb/call-bind-apply-helpers/compare/v1.0.1...v1.0.2) - 2025-02-12 ### Commits - [types] improve inferred types [`e6f9586`](https://github.com/ljharb/call-bind-apply-helpers/commit/e6f95860a3c72879cb861a858cdfb8138fbedec1) - [Dev Deps] update `@arethetypeswrong/cli`, `@ljharb/tsconfig`, `@types/tape`, `es-value-fixtures`, `for-each`, `has-strict-mode`, `object-inspect` [`e43d540`](https://github.com/ljharb/call-bind-apply-helpers/commit/e43d5409f97543bfbb11f345d47d8ce4e066d8c1) ## [v1.0.1](https://github.com/ljharb/call-bind-apply-helpers/compare/v1.0.0...v1.0.1) - 2024-12-08 ### Commits - [types] `reflectApply`: fix types [`4efc396`](https://github.com/ljharb/call-bind-apply-helpers/commit/4efc3965351a4f02cc55e836fa391d3d11ef2ef8) - [Fix] `reflectApply`: oops, Reflect is not a function [`83cc739`](https://github.com/ljharb/call-bind-apply-helpers/commit/83cc7395de6b79b7730bdf092f1436f0b1263c75) - [Dev Deps] update `@arethetypeswrong/cli` [`80bd5d3`](https://github.com/ljharb/call-bind-apply-helpers/commit/80bd5d3ae58b4f6b6995ce439dd5a1bcb178a940) ## v1.0.0 - 2024-12-05 ### Commits - Initial implementation, tests, readme [`7879629`](https://github.com/ljharb/call-bind-apply-helpers/commit/78796290f9b7430c9934d6f33d94ae9bc89fce04) - Initial commit [`3f1dc16`](https://github.com/ljharb/call-bind-apply-helpers/commit/3f1dc164afc43285631b114a5f9dd9137b2b952f) - npm init [`081df04`](https://github.com/ljharb/call-bind-apply-helpers/commit/081df048c312fcee400922026f6e97281200a603) - Only apps should have lockfiles [`5b9ca0f`](https://github.com/ljharb/call-bind-apply-helpers/commit/5b9ca0fe8101ebfaf309c549caac4e0a017ed930) ljharb-call-bind-apply-helpers-ef266c3/LICENSE000066400000000000000000000020571475317260000210200ustar00rootroot00000000000000MIT License Copyright (c) 2024 Jordan Harband Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ljharb-call-bind-apply-helpers-ef266c3/README.md000066400000000000000000000044321475317260000212710ustar00rootroot00000000000000# call-bind-apply-helpers [![Version Badge][npm-version-svg]][package-url] [![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![dependency status][deps-svg]][deps-url] [![dev dependency status][dev-deps-svg]][dev-deps-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![npm badge][npm-badge-png]][package-url] Helper functions around Function call/apply/bind, for use in `call-bind`. The only packages that should likely ever use this package directly are `call-bind` and `get-intrinsic`. Please use `call-bind` unless you have a very good reason not to. ## Getting started ```sh npm install --save call-bind-apply-helpers ``` ## Usage/Examples ```js const assert = require('assert'); const callBindBasic = require('call-bind-apply-helpers'); function f(a, b) { assert.equal(this, 1); assert.equal(a, 2); assert.equal(b, 3); assert.equal(arguments.length, 2); } const fBound = callBindBasic([f, 1]); delete Function.prototype.call; delete Function.prototype.bind; fBound(2, 3); ``` ## Tests Clone the repo, `npm install`, and run `npm test` [package-url]: https://npmjs.org/package/call-bind-apply-helpers [npm-version-svg]: https://versionbadg.es/ljharb/call-bind-apply-helpers.svg [deps-svg]: https://david-dm.org/ljharb/call-bind-apply-helpers.svg [deps-url]: https://david-dm.org/ljharb/call-bind-apply-helpers [dev-deps-svg]: https://david-dm.org/ljharb/call-bind-apply-helpers/dev-status.svg [dev-deps-url]: https://david-dm.org/ljharb/call-bind-apply-helpers#info=devDependencies [npm-badge-png]: https://nodei.co/npm/call-bind-apply-helpers.png?downloads=true&stars=true [license-image]: https://img.shields.io/npm/l/call-bind-apply-helpers.svg [license-url]: LICENSE [downloads-image]: https://img.shields.io/npm/dm/call-bind-apply-helpers.svg [downloads-url]: https://npm-stat.com/charts.html?package=call-bind-apply-helpers [codecov-image]: https://codecov.io/gh/ljharb/call-bind-apply-helpers/branch/main/graphs/badge.svg [codecov-url]: https://app.codecov.io/gh/ljharb/call-bind-apply-helpers/ [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/call-bind-apply-helpers [actions-url]: https://github.com/ljharb/call-bind-apply-helpers/actions ljharb-call-bind-apply-helpers-ef266c3/actualApply.d.ts000066400000000000000000000000271475317260000230570ustar00rootroot00000000000000export = Reflect.apply;ljharb-call-bind-apply-helpers-ef266c3/actualApply.js000066400000000000000000000004301475317260000226210ustar00rootroot00000000000000'use strict'; var bind = require('function-bind'); var $apply = require('./functionApply'); var $call = require('./functionCall'); var $reflectApply = require('./reflectApply'); /** @type {import('./actualApply')} */ module.exports = $reflectApply || bind.call($call, $apply); ljharb-call-bind-apply-helpers-ef266c3/applyBind.d.ts000066400000000000000000000011461475317260000225250ustar00rootroot00000000000000import actualApply from './actualApply'; type TupleSplitHead = T['length'] extends N ? T : T extends [...infer R, any] ? TupleSplitHead : never type TupleSplitTail = O['length'] extends N ? T : T extends [infer F, ...infer R] ? TupleSplitTail<[...R], N, [...O, F]> : never type TupleSplit = [TupleSplitHead, TupleSplitTail] declare function applyBind(...args: TupleSplit, 2>[1]): ReturnType; export = applyBind;ljharb-call-bind-apply-helpers-ef266c3/applyBind.js000066400000000000000000000004101475317260000222620ustar00rootroot00000000000000'use strict'; var bind = require('function-bind'); var $apply = require('./functionApply'); var actualApply = require('./actualApply'); /** @type {import('./applyBind')} */ module.exports = function applyBind() { return actualApply(bind, $apply, arguments); }; ljharb-call-bind-apply-helpers-ef266c3/functionApply.d.ts000066400000000000000000000000421475317260000234300ustar00rootroot00000000000000export = Function.prototype.apply;ljharb-call-bind-apply-helpers-ef266c3/functionApply.js000066400000000000000000000001431475317260000231760ustar00rootroot00000000000000'use strict'; /** @type {import('./functionApply')} */ module.exports = Function.prototype.apply; ljharb-call-bind-apply-helpers-ef266c3/functionCall.d.ts000066400000000000000000000000411475317260000232150ustar00rootroot00000000000000export = Function.prototype.call;ljharb-call-bind-apply-helpers-ef266c3/functionCall.js000066400000000000000000000001411475317260000227620ustar00rootroot00000000000000'use strict'; /** @type {import('./functionCall')} */ module.exports = Function.prototype.call; ljharb-call-bind-apply-helpers-ef266c3/index.d.ts000066400000000000000000000042621475317260000217140ustar00rootroot00000000000000type RemoveFromTuple< Tuple extends readonly unknown[], RemoveCount extends number, Index extends 1[] = [] > = Index["length"] extends RemoveCount ? Tuple : Tuple extends [infer First, ...infer Rest] ? RemoveFromTuple : Tuple; type ConcatTuples< Prefix extends readonly unknown[], Suffix extends readonly unknown[] > = [...Prefix, ...Suffix]; type ExtractFunctionParams = T extends (this: infer TThis, ...args: infer P extends readonly unknown[]) => infer R ? { thisArg: TThis; params: P; returnType: R } : never; type BindFunction< T extends (this: any, ...args: any[]) => any, TThis, TBoundArgs extends readonly unknown[], ReceiverBound extends boolean > = ExtractFunctionParams extends { thisArg: infer OrigThis; params: infer P extends readonly unknown[]; returnType: infer R; } ? ReceiverBound extends true ? (...args: RemoveFromTuple>) => R extends [OrigThis, ...infer Rest] ? [TThis, ...Rest] // Replace `this` with `thisArg` : R : >>( thisArg: U, ...args: RemainingArgs ) => R extends [OrigThis, ...infer Rest] ? [U, ...ConcatTuples] // Preserve bound args in return type : R : never; declare function callBind< const T extends (this: any, ...args: any[]) => any, Extracted extends ExtractFunctionParams, const TBoundArgs extends Partial & readonly unknown[], const TThis extends Extracted["thisArg"] >( args: [fn: T, thisArg: TThis, ...boundArgs: TBoundArgs] ): BindFunction; declare function callBind< const T extends (this: any, ...args: any[]) => any, Extracted extends ExtractFunctionParams, const TBoundArgs extends Partial & readonly unknown[] >( args: [fn: T, ...boundArgs: TBoundArgs] ): BindFunction; declare function callBind( args: [fn: Exclude, ...rest: TArgs] ): never; // export as namespace callBind; export = callBind; ljharb-call-bind-apply-helpers-ef266c3/index.js000066400000000000000000000007771475317260000214670ustar00rootroot00000000000000'use strict'; var bind = require('function-bind'); var $TypeError = require('es-errors/type'); var $call = require('./functionCall'); var $actualApply = require('./actualApply'); /** @type {(args: [Function, thisArg?: unknown, ...args: unknown[]]) => Function} TODO FIXME, find a way to use import('.') */ 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); }; ljharb-call-bind-apply-helpers-ef266c3/package.json000066400000000000000000000046601475317260000223030ustar00rootroot00000000000000{ "name": "call-bind-apply-helpers", "version": "1.0.2", "description": "Helper functions around Function call/apply/bind, for use in `call-bind`", "main": "index.js", "exports": { ".": "./index.js", "./actualApply": "./actualApply.js", "./applyBind": "./applyBind.js", "./functionApply": "./functionApply.js", "./functionCall": "./functionCall.js", "./reflectApply": "./reflectApply.js", "./package.json": "./package.json" }, "scripts": { "prepack": "npmignore --auto --commentLines=auto", "prepublish": "not-in-publish || npm run prepublishOnly", "prepublishOnly": "safe-publish-latest", "prelint": "evalmd README.md", "lint": "eslint --ext=.js,.mjs .", "postlint": "tsc -p . && attw -P", "pretest": "npm run lint", "tests-only": "nyc tape 'test/**/*.js'", "test": "npm run tests-only", "posttest": "npx npm@'>=10.2' audit --production", "version": "auto-changelog && git add CHANGELOG.md", "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" }, "repository": { "type": "git", "url": "git+https://github.com/ljharb/call-bind-apply-helpers.git" }, "author": "Jordan Harband ", "license": "MIT", "bugs": { "url": "https://github.com/ljharb/call-bind-apply-helpers/issues" }, "homepage": "https://github.com/ljharb/call-bind-apply-helpers#readme", "dependencies": { "es-errors": "^1.3.0", "function-bind": "^1.1.2" }, "devDependencies": { "@arethetypeswrong/cli": "^0.17.3", "@ljharb/eslint-config": "^21.1.1", "@ljharb/tsconfig": "^0.2.3", "@types/for-each": "^0.3.3", "@types/function-bind": "^1.1.10", "@types/object-inspect": "^1.13.0", "@types/tape": "^5.8.1", "auto-changelog": "^2.5.0", "encoding": "^0.1.13", "es-value-fixtures": "^1.7.1", "eslint": "=8.8.0", "evalmd": "^0.0.19", "for-each": "^0.3.5", "has-strict-mode": "^1.1.0", "in-publish": "^2.0.1", "npmignore": "^0.3.1", "nyc": "^10.3.2", "object-inspect": "^1.13.4", "safe-publish-latest": "^2.0.0", "tape": "^5.9.0", "typescript": "next" }, "testling": { "files": "test/index.js" }, "auto-changelog": { "output": "CHANGELOG.md", "template": "keepachangelog", "unreleased": false, "commitLimit": false, "backfillLimit": false, "hideCredit": true }, "publishConfig": { "ignore": [ ".github/workflows" ] }, "engines": { "node": ">= 0.4" } } ljharb-call-bind-apply-helpers-ef266c3/reflectApply.d.ts000066400000000000000000000001221475317260000232260ustar00rootroot00000000000000declare const reflectApply: false | typeof Reflect.apply; export = reflectApply; ljharb-call-bind-apply-helpers-ef266c3/reflectApply.js000066400000000000000000000002041475317260000227730ustar00rootroot00000000000000'use strict'; /** @type {import('./reflectApply')} */ module.exports = typeof Reflect !== 'undefined' && Reflect && Reflect.apply; ljharb-call-bind-apply-helpers-ef266c3/test/000077500000000000000000000000001475317260000207665ustar00rootroot00000000000000ljharb-call-bind-apply-helpers-ef266c3/test/index.js000066400000000000000000000051421475317260000224350ustar00rootroot00000000000000'use strict'; var callBind = require('../'); var hasStrictMode = require('has-strict-mode')(); var forEach = require('for-each'); var inspect = require('object-inspect'); var v = require('es-value-fixtures'); var test = require('tape'); test('callBindBasic', function (t) { forEach(v.nonFunctions, function (nonFunction) { t['throws']( // @ts-expect-error function () { callBind([nonFunction]); }, TypeError, inspect(nonFunction) + ' is not a function' ); }); var sentinel = { sentinel: true }; /** @type {(this: T, a: A, b: B) => [T | undefined, A, B]} */ var func = function (a, b) { // eslint-disable-next-line no-invalid-this return [!hasStrictMode && this === global ? undefined : this, a, b]; }; t.equal(func.length, 2, 'original function length is 2'); /** type {(thisArg: unknown, a: number, b: number) => [unknown, number, number]} */ var bound = callBind([func]); /** type {((a: number, b: number) => [typeof sentinel, typeof a, typeof b])} */ var boundR = callBind([func, sentinel]); /** type {((b: number) => [typeof sentinel, number, typeof b])} */ var boundArg = callBind([func, sentinel, /** @type {const} */ (1)]); // @ts-expect-error t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with no args'); // @ts-expect-error t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args'); // @ts-expect-error t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func too few args'); // @ts-expect-error t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args'); // @ts-expect-error t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args'); t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args'); t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with right args'); t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args'); t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg'); // @ts-expect-error t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args'); // @ts-expect-error t.deepEqual(bound(1, 2, 3, 4), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args'); // @ts-expect-error t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args'); // @ts-expect-error t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args'); t.end(); }); ljharb-call-bind-apply-helpers-ef266c3/tsconfig.json000066400000000000000000000001631475317260000225160ustar00rootroot00000000000000{ "extends": "@ljharb/tsconfig", "compilerOptions": { "target": "es2021", }, "exclude": [ "coverage", ], }