function-maybe

1.0.0 • Public • Published

function-maybe

Lift your function into maybe

Motivation

Clean up your code by ensuring that function arguments will never be undefined

// Turn this:
function toUpper(string) {
    if (string === undefined) {
        return undefined
    }
 
    return string.toUpperCase()
}
 
// Into this:
const toUpper = maybe(string => string.toUpperCase())

Installation

npm install function-maybe

Examples

import maybe from 'function-maybe'
 
const toUpper = maybe(string => string.toUpperCase())
console.log(toUpper('doge')) // => DOGE
console.log(toUpper(undefined)) // => undefined
 
const join = separator => maybe(list => list.join(separator))
const format = pipe(join(','), toUpper)
 
console.log(format(['doge', 'such', 'wow!'])) // => DOGE,SUCH,WOW!
console.log(format(undefined)) // => undefined

Testing

npm test

Package Sidebar

Install

npm i function-maybe

Weekly Downloads

1

Version

1.0.0

License

Apache-2.0

Last publish

Collaborators

  • machobearstudio