1.0.0 • Published 7 years ago

function-maybe v1.0.0

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
7 years ago

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