A small library for functional programming; functions I found to be
missing elsewhere that were useful. Also shortcuts for fcns that do exist.
Whether using ramda or lodash you can get most of what is needed, but there
were some functions I wanted that were not availble or had awkward syntax.
npm install @jadesrochers/fpstreamline
const fps = require('@jadesrochers/fpstreamline')
const R = require('ramda')
Allows async functions to be piped, and also handles regular functions.
Consider it a regular piping function that is also async tolerant.
let testprom1 = n => Promise.resolve(n-1)
let testprom2 = n => Promise.resolve(n-2)
let testprom3 = n => Promise.resolve(n-3)
let test = async () => {
let rslt = await fps.pipeAsync(
R.adjust(1)(testprom1),
R.adjust(1)(testprom2),
R.adjust(1)(testprom3),
)([0,6])
console.log('Result: ', rslt)
}
test()
// [0,0]
I wanted to be able to use unknown length/type input, and these do. They will take single values or an array and app/pre pend either way.
R.pipe(
fps.append(2),
fps.append(3),
)(1)
// [ 1, 2, 3 ]
R.pipe(
fps.prepend(2),
fps.prepend(3),
)(1)
// [ 3, 2, 1 ]
Also append/prepend, but takes a function and index, and append/prepend the
result of running the function on the index.
Can take single value or array, if single value use (0) for index.
let testfn1 = n => n+1
let testfn2 = n => n+2
R.pipe(
fps.appendUseNth(1)(testfn1),
fps.appendUseNth(2)(testfn2),
)([0,1,2])
// [ 0, 1, 2, 2, 4 ]
R.pipe(
fps.prependUseNth(1)(testfn1),
fps.prependUseNth(2)(testfn2),
)([0,1,2])
// [ 3, 2, 0, 1, 2 ]
R.pipe(
fps.insertUseNth(1)(1)(testfn1),
fps.insertUseNth(2)(2)(testfn2),
)([0,1,2])
// [ 0, 2, 3, 1, 2 ]
The arity must be correct, nothing fancy going on here.
fps.runAll(a => b => c => d => e => a+b+c+d+e)([0,1,2,3,4])
// 10
fps.runN(3)(a => b => c => a+b+c)([0,1,2,3,4])
// 9
For quickly getting correctly escaped/formatted regular expression string
when the text to be searched has special characters.
fps.regexEscapeAll('-/\\^$*+?.()|[]{}')
fps.regexEscapeExPer('-/\\^$*+?.()|[]{}')
toRegex uses what you pass verbatim, getRegex escaped all special
characters except period.
fps.toRegex('i')('abc*(ghi+)?')
// /abc*(ghi+)?/i
fps.getRegex('abc*+?[]()ghi')
// /abc\*\+\?\[\]\(\)ghi/i
strSearchBool performs a search of the string using a regex but returns just a boolean of whether a match was found, not how many, where, or anything else.
fps.strSearchBool(/abc/)('filler abc filler')
// true
fps.strSearchBool(/abc/)('filler Abc filler')
// false
These are mostly just wrappers around other functions to
do testing/converting.
Supported types include object, number, boolean, string, null, array,
regexp, function, undefined
fps.isTypeof('object')({})
// true
fps.isTypeof('object')({a:1, b:2})
// true
Will match R.type rules.
fps.typeMatch('def')('abc')
// true
fps.typeMatch({})({a:1, b:2})
// true
fps.typeMatch(null)(undefined)
// false
fps.toJSON('{"a":1,"b":[1,2,3],"c":3}')
// { a: 1, b: [ 1, 2, 3 ], c: 3 }
Converts to number if it has just numbers.
fps.strToNum('4620')
// Converts
fps.strToNum('34-35')
// Keeps as string
fps.strToNum('34,351')
// keeps as string
It follows rules of Date() object creation, so anything valid
for that should work with this.
fps.toDate('11-05-1955')
fps.toDate('December 7, 1941')
fps.toDate('1453-05-29')
fps.toArray(5)
// [ 5 ]
fps.toArray('test string')
// [ 'test string' ]
fps.toArray({a: 1})
// [ {a: 1} ]
fps.toArray([1,2,3])
// [ 1, 2, 3 ]
These are implemented everywhere of course, I just wanted the option of dividing by the value passed first without R.flip. Not very useful.
R.pipe(
fps.subtract(3),
fps.divide(3),
)(12)
// 3