Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

katis/pipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pipe

yarn add @katis/pipe
npm install @katis/pipe

TypeScript library for transforming values in the style of the Elm and F# pipe-operator.

Motivation

Transforming values by chaining multiple function calls can make the code harder to read:

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);

Pipe makes those calls sequential:

import { pipe } from '@katis/pipe'

const result = pipe('Value: 1300$')
  .to(removePrefix, 'Value: ')
  .to(replace, '$', '')
  .out(parseInt, 10)

API

pipe(value)

Create a pipeline that can be used to transform the provided value.

.to(fn, ...fnExtraArgs)

Provide a function for the pipeline that transforms the value. The function is called with the value as the first parameter, and with the optional extra arguments as rest of the parameters.

Returns a new pipeline where the result is the return value of the function.

.out(fn, ...fnExtraArgs)

Like .to() it takes a function that transforms the value, but just returns the transformed value, ending the pipeline.

About

TypeScript library for transforming values in the style of the Elm and F# pipe-operator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published