Skip to content

Commit

Permalink
feat: adds callfnwith
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 15, 2021
1 parent f9ffd07 commit 1d01ad1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/__tests__/applicative.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { check, bind, fallback, panic, pass } from "../applicative";
import { array, num } from '..';
import { check, bind, fallback, panic, pass, callfnwith } from "../applicative";
import { pipe } from '../pipe';

describe("applicative", () => {
it("bind", () => {
Expand All @@ -9,6 +11,16 @@ describe("applicative", () => {
expect(fn(teststring)).toEqual(boundfn());
});

it("callfnwith", () => {
const piped = pipe(
num.umultiply(2),
num.uadd,
);

expect(callfnwith(10)(piped)(2)).toBe(22);
expect(callfnwith(4)(piped)(2)).toBe(10);
});

it("check", () => {
expect(() => check(true, "will not throw")(123)).not.toThrow();
expect(() => check(false, "will not throw")(123)).toThrow();
Expand Down
25 changes: 25 additions & 0 deletions src/applicative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ export const bind = <Fn extends FnBase>(
...args: ArgsOf<Fn>
): (() => ReturnType<Fn>) => () => fn(...args) as ReturnType<Fn>;

/**
* Given some arguments, returns a function which accepts another function.
* This function will be called with the given arguments.
* @since 2.13.0
* @example
*
* ```ts
* import { callfnwith, num, pipe } from 'tiinvo';
*
* const piped = pipe(
* num.umultiply(2),
* num.uadd,
* );
*
* callfnwith(10)(piped)(2) // 22
* callfnwith(4)(piped)(2) // 10
*
* ```
*
*
* @param args
* @returns
*/
export const callfnwith = <Args extends any[]>(... args: Args) => <Fn extends (... args: Args) => any>(fn: Fn): ReturnType<Fn> => fn.apply(null, args);

/**
* Checks if a given condition is true, otherwise throws an error with the given error message
*
Expand Down

0 comments on commit 1d01ad1

Please sign in to comment.