@rpearce/ts-fns
v1.2.2
Published
Public domain Typescript helper functions for copying & pasting into projects.
Downloads
7
Maintainers
Readme
@rpearce/ts-fns
Public domain (LICENSE) Typescript helper functions for copying & pasting into projects. You can try it out here: https://npm.runkit.com/%40rpearce%2Fts-fns.
Most functions export two calling styles, regular and partial application:
// Regular
hasProp('color', { color: 'blue' }) // true
// Partial application
hasPropU('color')({ color: 'blue' }) // true
Special thanks to the following for helping with some typings!
Typed functions
Note: many functions have a unary, partial application style counterpart ending
in U
; e.g., cond
also exports condU
.
F
T
classNames
compose2
compose3
cond
,condU
constant
doesPropEq
,doesPropEqU
filter
,filterU
hasProp
,hasPropU
identity
insertAt
,insertAtU
isArray
isFunction
isFunctor
isObject
lift2
,lift2U
lift3
,lift3U
log
map
,mapU
mapObject
,mapObjectU
omitKeys
,omitKeysU
pickKeys
,pickKeysU
pipe2
pipe3
propOr
,propOrU
reduce
,reduceU
reduceRight
,reduceRightU
shuffle
sum
takeN
,takeNU
unless
,unlessU
when
,whenU
zip
TODO
both
either
ifElse
neither
pluckKeys
sortAscBy
sortDescBy
- ??? Open an issue to request something!
Usage
Copy the functions and types you need into your project.
Example
import {
doesPropEq,
doesPropEqU,
takeN,
takeNU,
} from './wherever-i-copied-the-helpers-to'
takeN(3, [1, 2, 3, 4]) // [1, 2, 3]
takeNU(3)([1, 2, 3, 4]) // [1, 2, 3]
doesPropEq('color', 'blue', { a: 'foo', b: 'bar', color: 'blue' }) // true
doesPropEqU('color')('blue')({ a: 'foo', b: 'bar', color: 'blue' }) // true
More in-depth unary partial application example
import { lift2U, propOrU } from './wherever-i-copied-the-helpers-to'
const propOrNA = propOr('N/A')
const getName = propOrNA('name')
const getEmail = propOrNA('email')
const joinWithPipe = (x: string) => (y: string) => `${x} | ${y}`
const joinNameEmail = lift2U(joinWithPipe)(getName)(getEmail)
const input = { name: 'bobert', email: '[email protected]', foo: 'bar' }
console.log(joinNameEmail(input))
// 'bobert | [email protected]'