UNPKG

nochoices

Version:

Full featured implementation of options into typescript.

3 lines (2 loc) 3.49 kB
"use strict";(()=>{var a=class{};var o=class extends a{value;constructor(t){super(),this.value=t}isPresent(){return!0}isAbsent(){return!1}unwrap(){return this.value}map(t){let r=t(this.value);return e.Some(r)}filter(t){return t(this.value)?e.Some(this.value):e.None()}expect(t){return this.value}unwrapOr(t){return this.value}unwrapOrElse(t){return this.value}flatten(){return this.value instanceof e?this.value:e.Some(this.value)}zip(t){return t.zipWithSome(this)}zipWithSome(t){return e.Some([t.value,this.value])}and(t){return t}or(t,r){return t}xor(t){return t.xorWithSome(this)}xorWithNone(){return e.Some(this.value)}xorWithSome(t){return e.None()}andThen(t){return t(this.value)}orElse(t){return e.Some(this.value)}getOrInsert(t){return this}getOrInsertWith(t){return this}takeValue(){return e.Some(this.value)}isSomeAnd(t){return t(this.value)}ifSome(t){t(this.value)}ifNone(t){}toArray(){return[this.value]}equalsWith(t,r){return t.isSomeAnd(i=>r(this.value,i))}};var u=class extends a{isPresent(){return!1}isAbsent(){return!0}unwrap(){throw new Error("unwrap over None.")}map(t){return e.None()}filter(t){return e.None()}expect(t){throw t}unwrapOr(t){return t}unwrapOrElse(t){return t()}flatten(){return e.None()}zip(t){return e.None()}zipWithSome(t){return e.None()}and(t){return e.None()}or(t,r){return r}xor(t){return t.xorWithNone()}xorWithNone(){return e.None()}xorWithSome(t){return e.Some(t.unwrap())}andThen(t){return e.None()}orElse(t){return t()}getOrInsert(t){return new o(t)}getOrInsertWith(t){return new o(t())}takeValue(){return e.None()}isSomeAnd(t){return!1}ifSome(t){}ifNone(t){t()}toArray(){return[]}equalsWith(t,r){return t.isAbsent()}};var e=class n{value;constructor(t){this.value=t}static Some(t){return new n(new o(t))}static None(){return new n(new u)}static fromNullable(t){return t==null?n.None():n.Some(t)}isNone(){return this.value.isAbsent()}isSome(){return this.value.isPresent()}map(t){return this.value.map(t)}unwrap(){return this.value.unwrap()}unwrapOr(t){return this.value.unwrapOr(t)}unwrapOrElse(t){return this.value.unwrapOrElse(t)}filter(t){return this.value.filter(t)}expect(t){return this.value.expect(t)}flatten(){return this.value.flatten()}mapOr(t,r){return this.map(r).unwrapOr(t)}mapOrElse(t,r){return this.map(r).unwrapOrElse(t)}zip(t){return this.value.zip(t.value)}zipWith(t,r){return this.zip(t).map(([i,p])=>r(i,p))}and(t){return this.value.and(t)}or(t){return this.value.or(this,t)}xor(t){return this.value.xor(t.value)}andThen(t){return this.value.andThen(t)}orElse(t){return this.value.orElse(t)}insert(t){return this.value=new o(t),this}getOrInsert(t){return this.value=this.value.getOrInsert(t),this.unwrap()}getOrInsertWith(t){return this.value=this.value.getOrInsertWith(t),this.unwrap()}take(){let t=this.value.takeValue();return this.value=new u,t}replace(t){let r=this.value;return this.value=new o(t),new n(r)}isSomeAnd(t){return this.value.isSomeAnd(t)}isSomeBut(t){return this.value.isSomeAnd(r=>!t(r))}ifSome(t){return this.value.ifSome(t),this}ifNone(t){return this.value.ifNone(t),this}inspectContent(t){this.ifSome(t)}takeIf(t){return this.filter(t).andThen(()=>this.take())}toArray(){return this.value.toArray()}equals(t){return this.value.equalsWith(t.value,(r,i)=>r===i)}equalsWith(t,r){return this.value.equalsWith(t.value,r)}};function S(n){return n.unwrapOr(e.None())}function A(n){return n.map(([t,r])=>[e.Some(t),e.Some(r)]).unwrapOr([e.None(),e.None()])}})(); //# sourceMappingURL=index.iife.js.map