Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
@typed/maybe
Advanced tools
Well-typed Maybe data structure
yarn add @typed/maybe
# or
npm install --save @typed/maybe
All functions are curried!
Creates a Just given a value.
export function of<A>(value: A): Just<A> {
return { '@typed/Just': value }
}
}
Creates a Maybe containg a value
export const of: <A>(value: A) => Maybe<A> = Just.of
}
Applies the function contained in a Maybe
to the value contained in a
second Maybe
.
export const ap: MaybeAp = curry2(__ap)
function __ap<A, B>(fn: Maybe<(value: A) => B>, maybe: Maybe<A>): Maybe<B> {
return chain(f => map(f, maybe), fn)
}
export interface MaybeAp {
<A, B>(fn: Maybe<(value: A) => B>, value: Maybe<A>): Maybe<B>
<A, B>(fn: Maybe<(value: A) => B>): (value: Maybe<A>) => Maybe<B>
}
Maps a Maybe
to another Maybe
.
export const chain: MaybeChain = curry2(__chain)
function __chain<A, B>(f: (value: A) => Maybe<B>, maybe: Maybe<A>): Maybe<B> {
return isNothing(maybe) ? maybe : f(fromJust(maybe))
}
export interface MaybeChain {
<A, B>(f: (value: A) => Maybe<B>, maybe: Maybe<A>): Maybe<B>
<A, B>(f: (value: A) => Maybe<B>): (maybe: Maybe<A>) => Maybe<B>
}
Extract the value contained in a Just
import { fromJust, Just } from '@typed/maybe'
const value = fromJust(Just.of(1))
console.log(value) // logs '1'
export function fromJust<A>(just: Just<A>): A {
return just['@typed/Just']
}
Given a default value and a Maybe returns the default value if the Maybe is a Nothing or the value contained in a Just.
export const fromMaybe: FromMaybe = curry2(__fromMaybe)
function __fromMaybe<A>(defaultValue: A, maybe: Maybe<A>): A {
return isJust(maybe) ? fromJust(maybe) : defaultValue
}
export interface FromMaybe {
<A>(defaultValue: A, maybe: Maybe<A>): A
<A>(defaultValue: A): (maybe: Maybe<A>) => A
}
Given a Maybe<A> it returns true if the Maybe<A> is Just<A> or false if it is a Nothing.
import { isJust, Nothing, Maybe } from '@typed/maybe'
console.log(isJust(Nothing)) // logs false
console.log(isJust(Maybe.of(1))) // logs true
export function isJust<A>(maybe: Maybe<A>): maybe is Just<A> {
return maybe.hasOwnProperty('@typed/Just')
}
Given a Maybe<A> it returns false if the Maybe<A> is Just<A> or true if it is a Nothing.
import { isNothing, Maybe, Nothing } from '@typed/maybe'
console.log(isNothing(Nothing)) // logs true
console.log(isNothing(Maybe.of(1))) // logs false
export function isNothing<A>(maybe: Maybe<A>): maybe is Nothing {
return (maybe as Nothing)['@typed/Nothing'] === true
}
Applies a function to the value possibly contained in a Maybe
. If the
maybe is a Nothing
just the Nothing
is returned.
export const map: MaybeMap = curry2(__map)
function __map<A, B>(f: (value: A) => B, maybe: Maybe<A>): Maybe<B> {
return chain(a => Maybe.of(f(a)), maybe)
}
export interface MaybeMap {
<A, B>(f: (value: A) => B, maybe: Maybe<A>): Maybe<B>
<A, B>(f: (value: A) => B): (maybe: Maybe<A>) => Maybe<B>
}
FAQs
Data structure for working with values that may not exist
We found that @typed/maybe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.