scats
Version:
Useful scala classes in typescript
172 lines (171 loc) • 4.03 kB
JavaScript
import { Collection } from './collection.js';
import { Left, left, Right, right } from './either.js';
import { ArrayIterable } from './array-iterable.js';
import { HashSet } from './hashset.js';
import { HashMap } from './hashmap.js';
export class Option extends ArrayIterable {
static when(cond) {
return a => {
if (cond) {
return some(a());
}
else {
return none;
}
};
}
static useless(cond) {
return Option.when(!cond);
}
fromArray(array) {
if (array.length <= 0) {
return none;
}
else {
return some(array[0]);
}
}
exists(p) {
if (this.isEmpty) {
return false;
}
else {
return p(this.get);
}
}
filter(p) {
if (this.isEmpty) {
return none;
}
else {
return p(this.get) ? this : none;
}
}
filterNot(p) {
if (this.isEmpty) {
return none;
}
else {
return p(this.get) ? none : this;
}
}
map(f) {
return this.isEmpty ? none : some(f(this.get));
}
flatMap(p) {
return this.isEmpty ? none : p(this.get);
}
async mapPromise(f) {
if (this.isEmpty) {
return Promise.resolve(none);
}
else {
return option(await f(this.get));
}
}
flatMapPromise(f) {
if (this.isEmpty) {
return Promise.resolve(none);
}
else {
return f(this.get);
}
}
foldValue(ifEmpty) {
if (this.isEmpty) {
return function () { return ifEmpty(); };
}
else {
return (f) => { return f(this.get); };
}
}
forall(p) {
return this.isEmpty ? true : p(this.get);
}
foreach(f) {
if (this.nonEmpty) {
f(this.get);
}
}
getOrElse(f) {
return this.isEmpty ? f() : this.get;
}
getOrElseValue(other) {
return this.isEmpty ? other : this.get;
}
getOrElseThrow(error) {
if (this.isEmpty) {
throw error();
}
else {
return this.get;
}
}
contains(x) {
return this.isEmpty ? false : x === this.get;
}
get isDefined() {
return !this.isEmpty;
}
orElse(alternative) {
return this.isEmpty ? alternative() : this;
}
orElseValue(alternative) {
return this.isEmpty ? alternative : this;
}
get orNull() {
return this.isEmpty ? null : this.get;
}
get orUndefined() {
return this.isEmpty ? undefined : this.get;
}
get toCollection() {
return this.isEmpty ? Collection.empty : Collection.of(this.get);
}
toRight(left) {
return this.isEmpty ? new Left(left()) : right(this.get);
}
toLeft(right) {
return this.isEmpty ? new Right(right()) : left(this.get);
}
get toArray() {
return this.isEmpty ? [] : [this.get];
}
get toSet() {
return this.isEmpty ? HashSet.empty : HashSet.of(this.get);
}
match(matcher) {
return this.isEmpty ? matcher.none() : matcher.some(this.get);
}
toMap(mapper) {
return this.isEmpty ? HashMap.empty : HashMap.of(...this.map(mapper).toArray);
}
}
export class Some extends Option {
value;
constructor(value) {
super();
this.value = value;
}
get get() {
return this.value;
}
get isEmpty() {
return false;
}
}
export class None extends Option {
get get() {
throw new Error('No such element.');
}
get isEmpty() {
return true;
}
}
export function option(value) {
return value === null || typeof value === 'undefined' ? none : some(value);
}
export function some(value) {
return new Some(value);
}
export const none = new None();