UNPKG

nochoices

Version:

Full featured implementation of options into typescript.

84 lines (83 loc) 1.61 kB
import { OptionalValue } from "./optional-value.js"; import { Option } from "./option.js"; import { Some } from "./some.js"; export class None extends OptionalValue { isPresent() { return false; } isAbsent() { return true; } unwrap() { throw new Error('unwrap over None.'); } map(_fn) { return Option.None(); } filter(_fn) { return Option.None(); } expect(err) { throw err; } unwrapOr(defaultValue) { return defaultValue; } unwrapOrElse(defaultFn) { return defaultFn(); } flatten() { return Option.None(); } zip(_another) { return Option.None(); } zipWithSome(_some) { return Option.None(); } and(_another) { return Option.None(); } or(_self, another) { return another; } xor(another) { return another.xorWithNone(); } xorWithNone() { return Option.None(); } xorWithSome(some) { return Option.Some(some.unwrap()); } andThen(_fn) { return Option.None(); } orElse(fn) { return fn(); } getOrInsert(value) { return new Some(value); } getOrInsertWith(fn) { return new Some(fn()); } takeValue() { return Option.None(); } isSomeAnd(_andFn) { return false; } ifSome(_param) { /* no-op */ } ifNone(fn) { fn(); } toArray() { return []; } equalsWith(another, _equality) { return another.isAbsent(); } }