import { Maybe, Just, Nothing } from 'purify-ts/Maybe'// Hard to chain additional transformations
// Doesn't protect against falsy values like empty string or 0
const config: Config | null = getConfig()
const port = config && config.port
? parseInt(config.port)
: 8080// Still hard to chain additional transformations
// This is a valid alternative if you don't mind the syntax
const port = parseInt(getConfig()?.port ?? '8080')const port = getConfig()
.chain(x => x.port)
.map(parseInt)
.orDefault(8080)Just(10)➔Just(10) // Maybe<number>Nothing➔Nothing // Maybe<never>Maybe.of(10)➔Just(10)Maybe.empty()➔NothingMaybe.zero()➔NothingMaybe.fromNullable(null)Maybe.fromNullable(10)➔➔NothingJust(10)Maybe.fromFalsy('')Maybe.fromFalsy(0)➔➔NothingNothingMaybe.fromPredicate(x => x > 0, 5)Maybe.fromPredicate(x => x > 0, -1)➔➔Just(5)NothingMaybe.catMaybes([Just(5), Nothing, Just(10)])➔[5, 10]Maybe.mapMaybe(x => isNaN(x) ? Nothing : Just(parseInt(x)), ['1', 'Apple', '3'])➔[1, 3]Maybe.sequence([Just(1), Just(5), Just(10)])Maybe.sequence([Just(1), Nothing, Just(10)])➔➔Just([1, 5, 10])NothingMaybe.encase(() => { throw Error('Always fails') })Maybe.encase(() => 10)➔➔NothingJust(10)Maybe.isMaybe(null)Maybe.isMaybe(Just(10))➔➔falsetrueJust(5).isJust()Nothing.isJust()➔➔truefalseJust(5).isNothing()Nothing.isNothing()➔➔falsetrueJust(5).caseOf({ Just: x => x + 1, Nothing: () => 0 })Nothing.caseOf({ Just: x => x + 1, Nothing: () => 0 })Nothing.caseOf({ _: () => 'anything'}) // wildcard➔➔➔60'anything'Just(5).equals(Just(5))Just(5).equals(Just(10))Just(5).equals(Nothing)➔➔➔truefalsefalseJust(5).map(x => x + 1)Nothing.map(x => x + 1)➔➔Just(6)NothingJust(5).ap(Just(x => x + 1))Just(5).ap(Nothing)Nothing.ap(Just(x => x + 1))Nothing.ap(Nothing)➔➔➔➔Just(6)NothingNothingNothingJust(5).alt(Just(6))Just(5).alt(Nothing)Nothing.alt(Just(5))Nothing.alt(Nothing)➔➔➔➔Just(5)Just(5)Just(5)NothingJust(5).chain(x => Just(x + 1))Nothing.chain(x => Just(x + 1))➔➔Just(6)NothingJust({prop: null}).chainNullable(x => x.prop)Just(5).chainNullable(x => x + 1)➔➔NothingJust(6)Just(Just(5)).join()Nothing.join()➔➔Just(5)NothingJust(5).reduce((acc, x) => x * acc, 2)Nothing.reduce((acc, x) => x * acc, 0)➔➔100Just(5).extend(x => x.isJust())Nothing.extend(x => x.isJust())➔➔Just(true)NothingJust(5).unsafeCoerce()Nothing.unsafeCoerce()➔➔5// Uncaught Error: Maybe#unsafeCoerce was ran on a NothingJust(5).orDefault(0)Nothing.orDefault(0)➔➔50Just(5).orDefaultLazy(() => expensiveComputation())Nothing.orDefaultLazy(() => 0)➔➔5 // expensiveComputation is never called0Just(5).mapOrDefault(x => x + 1, 0)Nothing.mapOrDefault(x => x + 1, 0)➔➔60Just(5).filter(x => x > 1)Just('apple').filter(x => x === 'banana')➔➔Just(5)NothingJust(5).extract()Nothing.extract()➔➔5undefinedJust(5).extractNullable()Nothing.extractNullable()➔➔5nullJust(5).toList()Nothing.toList()➔➔[5][]Just(5).toEither('Error')Nothing.toEither('Error')➔➔Right(5)Left('Error')Just(5).ifJust(() => console.log('success'))Nothing.ifJust(() => console.log('success'))➔➔// successJust(5).ifNothing(() => console.log('failure'))Nothing.ifNothing(() => console.log('failure'))➔➔// failure