-
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.
- Loading branch information
1 parent
5f90247
commit 3379335
Showing
6 changed files
with
203 additions
and
1 deletion.
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
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,4 +1,15 @@ | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
|
||
export { assertEquals } from "https://deno.land/std@0.190.0/testing/asserts.ts"; | ||
export { | ||
afterEach, | ||
beforeEach, | ||
describe, | ||
it, | ||
} from "https://deno.land/std@0.190.0/testing/bdd.ts"; | ||
export { | ||
assertSpyCallArgs, | ||
type Spy, | ||
spy, | ||
} from "https://deno.land/std@0.190.0/testing/mock.ts"; | ||
export const assertEqualsTypes = <T, U extends T = T>(_actual?: U): void => {}; |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
// This module is browser compatible. | ||
|
||
export { curry } from "./curry.ts"; | ||
export { partial } from "./partial.ts"; |
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,31 @@ | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** Partially applies function arguments. | ||
* | ||
* @example | ||
* ```ts | ||
* import { partial } from "https://deno.land/x/curry@$VERSION/partial.ts"; | ||
* | ||
* declare const fn: (a: string, b: number, c: boolean) => void; | ||
* | ||
* const ternary = partial(fn); | ||
* const binary = partial(fn, ""); | ||
* const unary = partial(fn, "", 0); | ||
* const nullary = partial(fn, "", 0, false); | ||
* ``` | ||
*/ | ||
export function partial< | ||
T extends readonly unknown[], | ||
U extends readonly unknown[], | ||
R, | ||
>( | ||
fn: (...args: readonly [...T, ...U]) => R, | ||
...args: T | ||
): (...rest: U) => R { | ||
function partial(...rest: U): R { | ||
return fn(...args, ...rest); | ||
} | ||
|
||
return partial; | ||
} |
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,132 @@ | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
|
||
import { partial } from "./partial.ts"; | ||
import { | ||
assertEquals, | ||
assertSpyCallArgs, | ||
beforeEach, | ||
describe, | ||
it, | ||
Spy, | ||
spy, | ||
} from "./_dev_deps.ts"; | ||
|
||
interface Arity1 { | ||
(arg: string): unknown; | ||
} | ||
|
||
interface Arity2 { | ||
(arg: string, arg2: number): unknown; | ||
} | ||
|
||
interface Arity3 { | ||
(arg: string, arg2: number, arg3: boolean): unknown; | ||
} | ||
|
||
type Fn2Spy<T> = T extends (this: infer T, ...args: infer Args) => infer R | ||
? Spy<T, Args, R> | ||
: unknown; | ||
|
||
describe("partial", () => { | ||
describe("arity 1", function () { | ||
interface Context { | ||
fn: Fn2Spy<Arity1>; | ||
} | ||
beforeEach<Context>(function () { | ||
this.fn = spy((arg1: string) => arg1); | ||
}); | ||
it<Context>("should pass 0 argument", function () { | ||
const arg = ""; | ||
const fn = partial(this.fn); | ||
|
||
assertEquals(fn(arg), arg); | ||
assertSpyCallArgs(this.fn, 0, [arg]); | ||
}); | ||
|
||
it<Context>("should pass 1 argument", function () { | ||
const arg = ""; | ||
const fn = partial(this.fn, arg); | ||
|
||
assertEquals(fn(), arg); | ||
assertSpyCallArgs(this.fn, 0, [arg]); | ||
}); | ||
}); | ||
|
||
describe("arity 2", function () { | ||
interface Context { | ||
fn: Fn2Spy<Arity2>; | ||
} | ||
|
||
const arg = ""; | ||
const arg2 = 0; | ||
|
||
beforeEach<Context>(function () { | ||
this.fn = spy((arg1: string, arg2: number): string => arg1 + arg2); | ||
}); | ||
|
||
it<Context>("should pass 0 argument", function () { | ||
const fn = partial(this.fn); | ||
|
||
assertEquals(fn(arg, arg2), arg + arg2); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2]); | ||
}); | ||
|
||
it<Context>("should pass 1 argument", function () { | ||
const fn = partial(this.fn, arg); | ||
|
||
assertEquals(fn(arg2), arg + arg2); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2]); | ||
}); | ||
|
||
it<Context>("should pass 2 argument", function () { | ||
const fn = partial(this.fn, arg, arg2); | ||
|
||
assertEquals(fn(), arg + arg2); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2]); | ||
}); | ||
}); | ||
|
||
describe("arity 3", function () { | ||
interface Context { | ||
fn: Fn2Spy<Arity3>; | ||
} | ||
|
||
const arg = ""; | ||
const arg2 = 0; | ||
const arg3 = false; | ||
|
||
beforeEach<Context>(function () { | ||
this.fn = spy((arg1: string, arg2: number, arg3: boolean): string => | ||
arg1 + arg2 + arg3 | ||
); | ||
}); | ||
|
||
it<Context>("should pass 0 argument", function () { | ||
const fn = partial(this.fn); | ||
|
||
assertEquals(fn(arg, arg2, arg3), arg + arg2 + arg3); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2, arg3]); | ||
}); | ||
|
||
it<Context>("should pass 1 argument", function () { | ||
const fn = partial(this.fn, arg); | ||
|
||
assertEquals(fn(arg2, arg3), arg + arg2 + arg3); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2, arg3]); | ||
}); | ||
|
||
it<Context>("should pass 2 argument", function () { | ||
const fn = partial(this.fn, arg, arg2); | ||
|
||
assertEquals(fn(arg3), arg + arg2 + arg3); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2, arg3]); | ||
}); | ||
|
||
it<Context>("should pass 3 argument", function () { | ||
const fn = partial(this.fn, arg, arg2, arg3); | ||
|
||
assertEquals(fn(), arg + arg2 + arg3); | ||
assertSpyCallArgs(this.fn, 0, [arg, arg2, arg3]); | ||
}); | ||
}); | ||
}); |