Skip to content

Commit c7df99b

Browse files
no commit message
1 parent 3d67ed4 commit c7df99b

File tree

7 files changed

+42
-384
lines changed

7 files changed

+42
-384
lines changed

deno.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"noImplicitOverride": true,
77
"noUncheckedIndexedAccess": true
88
},
9-
<<<<<<< HEAD
109
"imports": {
1110
"@deno/doc": "jsr:@deno/doc@0.137",
1211
"@deno/graph": "jsr:@deno/graph@^0.74",
@@ -53,9 +52,6 @@
5352
"graphviz": "npm:node-graphviz@^0.1.1",
5453
"npm:/typescript": "npm:typescript@5.4.4"
5554
},
56-
=======
57-
"importMap": "./import_map.json",
58-
>>>>>>> 05b6d7eedd8e441cb8fa22078f377a6a37b4fa88
5955
"tasks": {
6056
"test": "deno test --unstable-http --unstable-webgpu --doc --allow-all --parallel --coverage --trace-leaks --clean",
6157
"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",

functions/curry.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

functions/deno.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.1.0",
44
"exports": {
55
".": "./mod.ts",
6-
"./curry": "./curry.ts",
76
"./pipe": "./pipe.ts"
87
}
98
}

functions/mod.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@
2828
* @module
2929
*/
3030

31-
export * from "./curry.ts";
3231
export * from "./pipe.ts";

functions/pipe.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
1717
: Acc
1818
: Acc;
1919

20+
export function pipe(): <T>(arg: T) => T;
2021
export function pipe<FirstFn extends AnyFunc, F extends AnyFunc[]>(
2122
firstFn: FirstFn,
2223
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
23-
): (arg: Parameters<FirstFn>[0]) => LastFnReturnType<F, ReturnType<FirstFn>> {
24+
): (arg: Parameters<FirstFn>[0]) => LastFnReturnType<F, ReturnType<FirstFn>>;
25+
26+
export function pipe<FirstFn extends AnyFunc, F extends AnyFunc[]>(
27+
firstFn?: FirstFn,
28+
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
29+
): any {
30+
if (!firstFn) {
31+
return <T>(arg: T) => arg;
32+
}
2433
return (arg: Parameters<FirstFn>[0]) =>
2534
(fns as AnyFunc[]).reduce((acc, fn) => fn(acc), firstFn(arg));
2635
}
27-
28-
if (import.meta.main) {
29-
const res = pipe(Math.abs, Math.sqrt, Math.floor);
30-
res(-2); // 1
31-
}

functions/pipe_test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2+
3+
import { assertEquals, assertThrows } from "@std/assert";
4+
import { pipe } from "./mod.ts";
5+
6+
Deno.test("pipe() handles mixed types", () => {
7+
const inputPipe = pipe(
8+
Math.abs,
9+
Math.sqrt,
10+
Math.floor,
11+
(num) => `result: ${num}`,
12+
);
13+
assertEquals(inputPipe(-2), "result: 1");
14+
});
15+
16+
Deno.test("en empty pipe is the identity function", () => {
17+
const inputPipe = pipe();
18+
assertEquals(inputPipe("hello"), "hello");
19+
});
20+
21+
Deno.test("pipe() throws an exceptions when a function throws an exception", () => {
22+
const inputPipe = pipe(
23+
Math.abs,
24+
Math.sqrt,
25+
Math.floor,
26+
(num) => {
27+
throw new Error("This is an error for " + num);
28+
},
29+
(num) => `result: ${num}`,
30+
);
31+
assertThrows(() => inputPipe(-2));
32+
});

0 commit comments

Comments
 (0)