Gathering detailed insights and metrics for @cdandrea/piping-ts
Gathering detailed insights and metrics for @cdandrea/piping-ts
Gathering detailed insights and metrics for @cdandrea/piping-ts
Gathering detailed insights and metrics for @cdandrea/piping-ts
npm install @cdandrea/piping-ts
Typescript
Module System
Node Version
NPM Version
TypeScript (99.11%)
JavaScript (0.89%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
1 Stars
20 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jul 20, 2024
Latest Version
1.0.8
Package Id
@cdandrea/piping-ts@1.0.8
Unpacked Size
120.52 kB
Size
6.50 kB
File Count
11
NPM Version
10.8.1
Node Version
22.4.0
Published on
Jul 20, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
pipe()
and compose()
functions written in TSAccording to Wikipedia, "Function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole".
Piping is basically the same idea, but in reverse order, where the functions are provided in a more natural order (i.e. the same order as they are actually invoked).
This package provides a simple way to transform functions into their piped or composed versions using Higher-Order Functions (HOFs) called
pipe()
andcompose()
, respectively, while keeping the original functions signatures, making sure that, for each function in the sequence:
pipe()
: the type of the output (result) of the previous function will always match the type of the input of the next function. The type of the parameter of the piped function will always match the type of its first function's parameter and the final result will have the type of its last function.compose()
: the type of the input of the previous function will always match the type of the output (result) of the next function. The type of the parameter of the composed function will always match the type of its last function's parameter and the final result will have the type of its first function.
Install from npm
using your favorite package manager:
npm install @cdandrea/piping-ts
yarn add @cdandrea/piping-ts
pnpm install @cdandrea/piping-ts
Simply supply the functions to be piped as parameters of the pipe()
HOF.
Basic example:
1import { pipe } from '@cdandrea/piping-ts' 2 3const isEven = (n: number): boolean => n % 2 === 0 4const asString = (b: boolean): string => b.toString() 5const asArray = (s: string): string[] => [s] 6 7const f = pipe(isEven, asString, asArray) 8 9// f's signature will be: `(a1: number) => string[]` 10// 11// Notice how `f` expects a `number` as input, which is the same 12// type expected by its first function parameter, `isEven`, and 13// returns a `string[]` as the result, which is the result of 14// its last function parameter, `asArray`. 15 16console.log(f(10)) // Prints ["true"]
More elaborate example, using pipe()
and curry()
:
1import { curry } from '@cdandrea/currying-ts' 2import { pipe } from '@cdandrea/piping-ts' 3 4const prop = curry((prop: string, obj: Record<string, number>) => obj[prop]) 5const add = curry((a: number, b: number) => b + a) 6const modulo = curry((a: number, b: number) => b % a) 7const amtAdd1Mod7 = pipe(prop('amount'), add(1), modulo(7)) 8 9console.log(amtAdd1Mod7({ amount: 17 })) // Prints 4 10console.log(amtAdd1Mod7({ amount: 987 })) // Prints 1 11console.log(amtAdd1Mod7({ amount: 68 })) // Prints 6
Notice that the piped function returned by pipe()
gets fully typed.
Simply supply the functions to be composed as parameters of the compose()
HOF:
1import { curry } from '@cdandrea/currying-ts' 2import { compose } from '@cdandrea/piping-ts' 3 4const prop = curry((prop: string, obj: Record<string, number>) => obj[prop]) 5const add = curry((a: number, b: number) => b + a) 6const modulo = curry((a: number, b: number) => b % a) 7const amtAdd1Mod7 = compose(modulo(7), add(1), prop('amount')) 8 9console.log(amtAdd1Mod7({ amount: 17 })) // Prints 4 10console.log(amtAdd1Mod7({ amount: 987 })) // Prints 1 11console.log(amtAdd1Mod7({ amount: 68 })) // Prints 6
No vulnerabilities found.
No security vulnerabilities found.