Skip to content

Commit

Permalink
no commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-borderless committed Dec 18, 2024
1 parent 3d67ed4 commit c7df99b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 384 deletions.
4 changes: 0 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true
},
<<<<<<< HEAD
"imports": {
"@deno/doc": "jsr:@deno/doc@0.137",
"@deno/graph": "jsr:@deno/graph@^0.74",
Expand Down Expand Up @@ -53,9 +52,6 @@
"graphviz": "npm:node-graphviz@^0.1.1",
"npm:/typescript": "npm:typescript@5.4.4"
},
=======
"importMap": "./import_map.json",
>>>>>>> 05b6d7eedd8e441cb8fa22078f377a6a37b4fa88
"tasks": {
"test": "deno test --unstable-http --unstable-webgpu --doc --allow-all --parallel --coverage --trace-leaks --clean",
"test:browser": "git grep --name-only \"This module is browser compatible.\" | grep -v deno.json | grep -v .github/workflows | grep -v _tools | grep -v encoding/README.md | grep -v media_types/vendor/update.ts | xargs deno check --config browser-compat.tsconfig.json",
Expand Down
77 changes: 0 additions & 77 deletions functions/curry.ts

This file was deleted.

1 change: 0 additions & 1 deletion functions/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.1.0",
"exports": {
".": "./mod.ts",
"./curry": "./curry.ts",
"./pipe": "./pipe.ts"
}
}
1 change: 0 additions & 1 deletion functions/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@
* @module
*/

export * from "./curry.ts";
export * from "./pipe.ts";
16 changes: 10 additions & 6 deletions functions/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
: Acc
: Acc;

export function pipe(): <T>(arg: T) => T;
export function pipe<FirstFn extends AnyFunc, F extends AnyFunc[]>(
firstFn: FirstFn,
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): (arg: Parameters<FirstFn>[0]) => LastFnReturnType<F, ReturnType<FirstFn>> {
): (arg: Parameters<FirstFn>[0]) => LastFnReturnType<F, ReturnType<FirstFn>>;

export function pipe<FirstFn extends AnyFunc, F extends AnyFunc[]>(
firstFn?: FirstFn,
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): any {
if (!firstFn) {
return <T>(arg: T) => arg;
}
return (arg: Parameters<FirstFn>[0]) =>
(fns as AnyFunc[]).reduce((acc, fn) => fn(acc), firstFn(arg));
}

if (import.meta.main) {
const res = pipe(Math.abs, Math.sqrt, Math.floor);
res(-2); // 1
}
32 changes: 32 additions & 0 deletions functions/pipe_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals, assertThrows } from "@std/assert";
import { pipe } from "./mod.ts";

Deno.test("pipe() handles mixed types", () => {
const inputPipe = pipe(
Math.abs,
Math.sqrt,
Math.floor,
(num) => `result: ${num}`,
);
assertEquals(inputPipe(-2), "result: 1");
});

Deno.test("en empty pipe is the identity function", () => {
const inputPipe = pipe();
assertEquals(inputPipe("hello"), "hello");
});

Deno.test("pipe() throws an exceptions when a function throws an exception", () => {
const inputPipe = pipe(
Math.abs,
Math.sqrt,
Math.floor,
(num) => {
throw new Error("This is an error for " + num);
},
(num) => `result: ${num}`,
);
assertThrows(() => inputPipe(-2));
});
Loading

0 comments on commit c7df99b

Please sign in to comment.