1.0.1 • Published 6 years ago
@tdameritrade/halfpipe v1.0.1
halfpipe
Halfpipe is a functional programming library containing a pipe function, composable and pipe-able functions, and monadic constructs.
Install Instructions:
npm install @tdameritrade/halfpipe
Usage:
Functions are split into "namespaces" that operate on specific types of entities. Currently supported namespaces include:
ArraysEithersMapsMaybesObjectsPromisesSetsValidations
To use, import the pipe function as well as the desired namespaces:
import { pipe, Objects } from '@tdameritrade/halfpipe';
const myObj = {
x: 2,
y: 4
};
const hasZ = pipe(
myObj,
Objects.has('z')
);
// hasZ === falseAll functions are immutable:
import { pipe, Arrays } from '@tdameritrade/halfpipe';
const myArr = [1, 2, 3];
const reversedArr = pipe(
myArr,
Arrays.reverse()
);
// reversedArr === [ 3, 2, 1 ]
// myArr === [ 1, 2, 3 ]Functions can easily be composed:
import { pipe, Arrays, Objects } from '@tdameritrade/halfpipe';
const myArr = [{ x: 2 }, { x: 3, y: 4 }, { y: 4 }];
const allHaveX = pipe(
myArr,
Arrays.every(Objects.has('x'))
);
// allHaveX === falseAll functions that can return a potentially null/undefined value will return a Maybe instead:
import { pipe, Arrays, Objects, Maybes } from '@tdameritrade/halfpipe';
const myArr = [{ x: 1 }, { x: 4, y: 3 }, { x: 1, y: 2 }];
const firstXWithY = pipe(
myArr,
Arrays.find(Objects.has('y')),
Maybes.flatMap(Objects.get('x')),
Maybes.orNull()
);
// firstXWithY === 4