Skip to content

Instantly share code, notes, and snippets.

@mceachen
Last active October 9, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mceachen/ba885d4ef56c35f963fb9c6bb68d5cd4 to your computer and use it in GitHub Desktop.
Save mceachen/ba885d4ef56c35f963fb9c6bb68d5cd4 to your computer and use it in GitHub Desktop.
Option support for TypeScript
/*!
Copyright (c) 2017 Matthew McEachen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
export type Maybe<T> = T | undefined
export type MaybeNull<T> = Maybe<T> | null
// Scala got a Some things right.
// HUR HUR I AM HILLLARIOUS
export type MaybeOption<T> = Option<T> | MaybeNull<T>
/**
* @see http://www.scala-lang.org/api/current/scala/Option.html
*/
export interface Option<A> {
/**
* @return true if the option is an instance of Some, false otherwise
*/
isDefined: boolean
/**
* @return true if the option is None, false otherwise
*/
isEmpty: boolean
/**
* @return false if the option is None, true otherwise.
*/
nonEmpty: boolean
/**
* @return the option's value.
*/
get(): Maybe<A>
/**
* @return true if this option is nonempty and the predicate `p` returns true
* when applied to this Option's value.
*/
exists(p: (a: A) => boolean): boolean
/**
* @return a `Some` containing the result of applying `f` to this `Option`'s value
* if this `Option` is nonempty.
*/
map<B>(f: (a: A) => B): Option<B>
/**
* @return the result of applying `f` to this `Option`'s value if this
* `Option` is nonempty. By supporting `undefined` or `B`, we make caller's
* lives a little easier--we'll wrap the result in an `Option` for you.
*/
flatMap<B>(f: (a: A) => MaybeOption<B>): Option<B>
/**
* @return this `Option` if it is both nonempty
* and applying the predicate `p` to this `Option`'s value returns true.
*/
filter(p: (a: A) => boolean): Option<A>
/**
* Apply the given procedure `f` to the `Option`'s value
* if this `Option` is nonempty.
*/
forEach(f: (a: A) => void): void
/**
* @return this `Option`'s value if this `Option` is nonempty,
* otherwise return the result of evaluating `f`.
*/
getOrElse(f: () => A): A
/**
* @return this `Option`'s value if this `Option` is nonempty,
* otherwise return the result of evaluating `f`.
*/
orElse(f: () => MaybeOption<A>): Option<A>
/**
* @param f will only be invoked if both `this` and `b` are defined
*/
zip1<B, T>(b: MaybeOption<B>, f: (a: A, b: B) => MaybeOption<T>): Option<T>
zip2<B, C, T>(
b: MaybeOption<B>,
c: MaybeOption<C>,
f: (a: A, b: B, c: C) => MaybeOption<T>
): Option<T>
zip3<B, C, D, T>(
b: MaybeOption<B>,
c: MaybeOption<C>,
d: MaybeOption<D>,
f: (a: A, b: B, c: C, d: D) => MaybeOption<T>
): Option<T>
}
namespace NoneImpl {
export const isDefined = false
export const isEmpty = true
export const nonEmpty = false
export function get(): any {
return
}
export const exists = () => false
const noop = () => NoneImpl
export const map = noop
export const flatMap = noop
export const filter = noop
export const forEach = noop
export function getOrElse<A>(f: () => A): A {
return f()
}
export function orElse<A>(f: () => MaybeOption<A>): Option<A> {
return Option(f())
}
export const zip1 = noop
export const zip2 = noop
export const zip3 = noop
}
export const None = NoneImpl as Option<any>
export class Some<A> implements Option<A> {
readonly isDefined = true
readonly isEmpty = false
readonly nonEmpty = true
constructor(private readonly a: A) {}
get(): A {
return this.a
}
exists(f: (a: A) => boolean): boolean {
return f(this.a)
}
map<B>(f: (a: A) => B): Option<B> {
return new Some(f(this.a))
}
flatMap<B>(f: (a: A) => Option<B> | MaybeNull<B>): Option<B> {
const b = f(this.a)
return isOption(b) ? b : Option(b)
}
filter(f: (a: A) => boolean): Option<A> {
return Option(f(this.a) ? this.a : undefined)
}
forEach(f: (a: A) => void): void {
f(this.a)
}
getOrElse(_orElse: () => A): A {
return this.a
}
orElse(_orElse: () => Option<A>): Option<A> {
return this
}
zip1<B, T>(b: MaybeOption<B>, f: (a: A, b: B) => MaybeOption<T>): Option<T> {
return Option(b).flatMap(eb => f(this.a, eb))
}
zip2<B, C, T>(
b: Option<B>,
c: Option<C>,
f: (a: A, b: B, c: C) => MaybeOption<T>
): Option<T> {
return Option(b).flatMap(eb => Option(c).flatMap(ec => f(this.a, eb, ec)))
}
zip3<B, C, D, T>(
b: MaybeOption<B>,
c: MaybeOption<C>,
d: MaybeOption<D>,
f: (a: A, b: B, c: C, d: D) => MaybeOption<T>
): Option<T> {
return Option(b).flatMap(eb =>
Option(c).flatMap(ec => Option(d).flatMap(ed => f(this.a, eb, ec, ed)))
)
}
}
/**
* @see http://www.scala-lang.org/api/current/scala/Option.html
*/
export function Option<A>(a: MaybeOption<A>): Option<A> {
return isOption(a) ? a : a != null ? new Some(a) : None
}
export function isOption<A>(a: MaybeOption<A>): a is Option<A> {
return a instanceof Some || a === None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment