-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation, tests, readme
- Loading branch information
Showing
27 changed files
with
433 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"root": true, | ||
|
||
"extends": "@ljharb", | ||
|
||
"rules": { | ||
"func-name-matching": 0, | ||
"id-length": 0, | ||
"new-cap": [2, { | ||
"capIsNewExceptions": [ | ||
"GetIntrinsic", | ||
], | ||
}], | ||
"no-magic-numbers": 0, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: '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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: 'Tests: pretest/posttest' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/pretest.yml@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: '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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: '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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: Automatic Rebase | ||
|
||
on: [pull_request_target] | ||
|
||
jobs: | ||
_: | ||
uses: ljharb/actions/.github/workflows/rebase.yml@main | ||
secrets: | ||
token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Require “Allow Edits” | ||
|
||
on: [pull_request_target] | ||
|
||
jobs: | ||
_: | ||
name: "Require “Allow Edits”" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: ljharb/require-allow-edits@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
package-lock=false | ||
allow-same-version=true | ||
message=v%s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"all": true, | ||
"check-coverage": false, | ||
"reporter": ["text-summary", "text", "html", "json"], | ||
"exclude": [ | ||
"coverage", | ||
"test" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,62 @@ | ||
# call-bind-apply-helpers | ||
# call-bind-apply-helpers <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
|
||
[![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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export = Reflect.apply; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import actualApply from './actualApply'; | ||
|
||
type TupleSplitHead<T extends any[], N extends number> = T['length'] extends N | ||
? T | ||
: T extends [...infer R, any] | ||
? TupleSplitHead<R, N> | ||
: never | ||
|
||
type TupleSplitTail<T, N extends number, O extends any[] = []> = O['length'] extends N | ||
? T | ||
: T extends [infer F, ...infer R] | ||
? TupleSplitTail<[...R], N, [...O, F]> | ||
: never | ||
|
||
type TupleSplit<T extends any[], N extends number> = [TupleSplitHead<T, N>, TupleSplitTail<T, N>] | ||
|
||
declare function applyBind(...args: TupleSplit<Parameters<typeof actualApply>, 2>[1]): ReturnType<typeof actualApply>; | ||
|
||
export = applyBind; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export = Function.prototype.apply; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
/** @type {import('./functionApply')} */ | ||
module.exports = Function.prototype.apply; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export = Function.prototype.call; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
/** @type {import('./functionCall')} */ | ||
module.exports = Function.prototype.call; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
type RemoveFromTuple< | ||
Tuple extends unknown[], | ||
RemoveCount extends number, | ||
Index extends 1[] = [] | ||
> = Index["length"] extends RemoveCount | ||
? Tuple | ||
: Tuple extends [first: unknown, ...infer Rest] | ||
? RemoveFromTuple<Rest, RemoveCount, [...Index, 1]> | ||
: Tuple; | ||
|
||
type ConcatTuples< | ||
Prefix extends unknown[], | ||
Suffix extends unknown[] | ||
> = [...Prefix, ...Suffix]; | ||
|
||
type ReplaceThis<T, NewThis> = T extends (this: infer OldThis, ...args: infer A) => infer R | ||
? (this: NewThis, ...args: A) => R | ||
: never; | ||
|
||
type BindFunction< | ||
TThis, | ||
T extends (this: TThis, ...args: any[]) => any, // Allow specific types to propagate | ||
TBoundArgs extends unknown[], | ||
ReceiverBound extends boolean | ||
> = ReceiverBound extends true | ||
? (...args: RemoveFromTuple<Parameters<T>, TBoundArgs["length"] & number>) => ReturnType<ReplaceThis<T, TThis>> | ||
: (...args: ConcatTuples<[TThis], RemoveFromTuple<Parameters<T>, TBoundArgs["length"] & number>>) => ReturnType<T>; | ||
|
||
declare function callBind< | ||
TThis, | ||
T extends (this: TThis, ...args: any[]) => any, | ||
TBoundArgs extends Partial<Parameters<T>> | ||
>( | ||
args: [fn: T, thisArg: TThis, ...boundArgs: TBoundArgs] | ||
): BindFunction<TThis, T, TBoundArgs, true>; | ||
|
||
declare function callBind< | ||
TThis, | ||
T extends (this: TThis, ...args: any[]) => any, | ||
TBoundArgs extends Partial<Parameters<T>> | ||
>( | ||
args: [fn: T, ...boundArgs: TBoundArgs] | ||
): BindFunction<TThis, T, TBoundArgs, false>; | ||
|
||
export as namespace callBind; | ||
export = callBind; |
Oops, something went wrong.