Created
January 15, 2022 14:35
-
-
Save gavr123456789/17fba0a83407e06ce27920e061273a64 to your computer and use it in GitHub Desktop.
pipe typescript
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
export interface Pipe<A> { | |
to<B, Rest extends unknown[]>( | |
fn: (value: A, ...args: Rest) => B, | |
...args: Rest | |
): Pipe<B>; | |
out<B, Rest extends unknown[]>( | |
fn: (value: A, ...args: Rest) => B, | |
...args: Rest | |
): B; | |
} | |
export const pipe = <A>(value: A): Pipe<A> => ({ | |
to<B, Rest extends unknown[]>( | |
fn: (value: A, ...args: Rest) => B, | |
...args: Rest | |
): Pipe<B> { | |
return pipe(fn(value, ...args)); | |
}, | |
out<B, Rest extends unknown[]>( | |
fn: (value: A, ...args: Rest) => B, | |
...args: Rest | |
): B { | |
return fn(value, ...args); | |
}, | |
}); | |
const removePrefix = (str: string, prefix: string): string => | |
str.startsWith(prefix) ? str.slice(prefix.length) : str; | |
const replace = (str: string, search: string, replaceWith: string) => | |
str.replace(search, replaceWith); | |
parseInt(replace(removePrefix("Value: 1300$", "Value: "), "$", ""), 10); | |
const result = pipe('Value: 1300$') | |
.to(removePrefix, 'Value: ') | |
.to(replace, '$', '') | |
.out(parseInt, 10) | |
function add(a:number, b: string) { | |
return a + b; | |
} | |
function mul(a:number, b: number) { | |
return a * b; | |
} | |
const sas = pipe(5) | |
.to(add, "jhk") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment