Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipes #9

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './result';
export * from './option';
export * from './match';
export * from './unit';
export * from './pipes';
45 changes: 45 additions & 0 deletions src/pipes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type Fn = (...args: any[]) => any;

interface Pipe {
<A>(value: A): A;
<A, B>(value: A, fn1: (input: A) => B): B;
<A, B, C>(value: A, fn1: (input: A) => B, fn2: (input: B) => C): C;
<A, B, C, D>(
value: A,
fn1: (input: A) => B,
fn2: (input: B) => C,
fn3: (input: C) => D
): D;
<A, B, C, D, E>(
value: A,
fn1: (input: A) => B,
fn2: (input: B) => C,
fn3: (input: C) => D,
fn4: (input: D) => E
): E;
}

/**
* Represents a function that pipes a value through a series of functions.
* @param {any} value - The initial value to be processed.
* @param {...Function} fns - Functions to be applied sequentially to the value.
* @returns {unknown} The result after applying all functions to the initial value.
* @typedef {Function} Pipe
*
* @example
* const addTwo = (x) => x + 2;
* const multiplyByThree = (x) => x * 3;
* const square = (x) => x * x;
*
* const result = pipe(
* 2,
* addTwo, // 4
* multiplyByThree, // 12
* square // 144
* );
*
*/
export const pipe: Pipe = (value: any, ...fns: Function[]): unknown => {
return fns.reduce((acc, fn) => fn(acc), value);
};

60 changes: 60 additions & 0 deletions tests/pipes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { pipe } from "@carbonteq/fp/pipes";

describe('pipe construction', () => {
it('pipe should handle transformation functions', () => {
const len = (s: string): number => s.length;
const double = (n: number): number => n * 2;
const square = (n: number): number => n ** 2;

const res = pipe(
"hi",
len,
double,
square
);

expect(res).toBe(16);
});

it('pipe should handle async functions', async () => {
const res = await pipe(
2,
async (a) => await a + 2,
async (b) => await b * 3,
);

expect(res).toBe(12);
});

it('pipe should handle complex transformation functions', async () => {
const res = await pipe(
2,
(a) => {
const x = Math.pow(2, 2);
const y = Math.random();
if (y > 0.5) {
return x;
}
return a;
},
async (b) => await b * 3,
);

expect(res).toBe(6 || 12);
});

it('nested pipes', async () => {

const res = await pipe(
2,
(a) => a + 3,
(b) => pipe(
b,
(c) => c * 3,
(d) => d > 10 ? true : false
)
);

expect(res).toBe(true);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"forceConsistentCasingInFileNames": true,
"inlineSources": false,
"isolatedModules": true,
"moduleResolution": "node",
"moduleResolution": "NodeNext",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
Expand Down