UNPKG

nochoices

Version:

Full featured implementation of options into typescript.

3 lines (2 loc) 3.7 kB
var s=Object.defineProperty;var T=(n,t,e)=>t in n?s(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var p=(n,t,e)=>(T(n,typeof t!="symbol"?t+"":t,e),e);var i=class{};var a=class extends i{constructor(e){super();p(this,"value");this.value=e}isPresent(){return!0}isAbsent(){return!1}unwrap(){return this.value}map(e){let o=e(this.value);return r.Some(o)}filter(e){return e(this.value)?r.Some(this.value):r.None()}expect(e){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}flatten(){return this.value instanceof r?this.value:r.Some(this.value)}zip(e){return e.zipWithSome(this)}zipWithSome(e){return r.Some([e.value,this.value])}and(e){return e}or(e,o){return e}xor(e){return e.xorWithSome(this)}xorWithNone(){return r.Some(this.value)}xorWithSome(e){return r.None()}andThen(e){return e(this.value)}orElse(e){return r.Some(this.value)}getOrInsert(e){return this}getOrInsertWith(e){return this}takeValue(){return r.Some(this.value)}isSomeAnd(e){return e(this.value)}ifSome(e){e(this.value)}ifNone(e){}toArray(){return[this.value]}equalsWith(e,o){return e.isSomeAnd(l=>o(this.value,l))}};var u=class extends i{isPresent(){return!1}isAbsent(){return!0}unwrap(){throw new Error("unwrap over None.")}map(t){return r.None()}filter(t){return r.None()}expect(t){throw t}unwrapOr(t){return t}unwrapOrElse(t){return t()}flatten(){return r.None()}zip(t){return r.None()}zipWithSome(t){return r.None()}and(t){return r.None()}or(t,e){return e}xor(t){return t.xorWithNone()}xorWithNone(){return r.None()}xorWithSome(t){return r.Some(t.unwrap())}andThen(t){return r.None()}orElse(t){return t()}getOrInsert(t){return new a(t)}getOrInsertWith(t){return new a(t())}takeValue(){return r.None()}isSomeAnd(t){return!1}ifSome(t){}ifNone(t){t()}toArray(){return[]}equalsWith(t,e){return t.isAbsent()}};var r=class n{constructor(t){p(this,"value");this.value=t}static Some(t){return new n(new a(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,e){return this.map(e).unwrapOr(t)}mapOrElse(t,e){return this.map(e).unwrapOrElse(t)}zip(t){return this.value.zip(t.value)}zipWith(t,e){return this.zip(t).map(([o,l])=>e(o,l))}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 a(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 e=this.value;return this.value=new a(t),new n(e)}isSomeAnd(t){return this.value.isSomeAnd(t)}isSomeBut(t){return this.value.isSomeAnd(e=>!t(e))}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,(e,o)=>e===o)}equalsWith(t,e){return this.value.equalsWith(t.value,e)}};function E(n){return n.unwrapOr(r.None())}function _(n){return n.map(([t,e])=>[r.Some(t),r.Some(e)]).unwrapOr([r.None(),r.None()])}export{r as Option,E as flatten,_ as unzip}; //# sourceMappingURL=index.es2020.js.map