"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Future: () => Future_exports, Iterator: () => Iterator_exports, Option: () => Option_exports, Result: () => Result_exports }); module.exports = __toCommonJS(src_exports); // src/Result.ts var Result_exports = {}; __export(Result_exports, { ResultProto: () => ResultProto, bind: () => bind2, err: () => err, fromNullable: () => fromNullable2, fromOption: () => fromOption, isErr: () => isErr, isOk: () => isOk, ok: () => ok, tryCatch: () => tryCatch2 }); // src/Future.ts var Future_exports = {}; __export(Future_exports, { FutureProto: () => FutureProto, future: () => future, tryPromise: () => tryPromise, tryTask: () => tryTask }); var FutureProto = { and(v) { return future(this.value.then((res) => res.andFuture(v).value)); }, andThen(f) { return future(this.value.then((res) => res.andThenFuture(f).value)); }, async err() { return this.value.then((res) => res.err()); }, async expect(msg) { return this.value.then((res) => res.expect(msg)); }, async expectErr(msg) { return this.value.then((res) => res.expectErr(msg)); }, flatten() { return this.andThen((v) => v); }, inspect(f) { return future(this.value.then((res) => res.inspect(f))); }, inspectErr(f) { return future(this.value.then((res) => res.inspectErr(f))); }, async isErr() { return this.value.then((res) => res.isErr()); }, async isErrAnd(f) { return this.value.then((res) => res.isErrAnd(f)); }, async isOk() { return this.value.then((res) => res.isOk()); }, async isOkAnd(f) { return this.value.then((res) => res.isOkAnd(f)); }, map(f) { return future(this.value.then((res) => res.map(f))); }, mapErr(f) { return future(this.value.then((res) => res.mapErr(f))); }, async mapOr(def, f) { return this.value.then((res) => res.mapOr(def, f)); }, async mapOrElse(def, f) { return this.value.then((res) => res.mapOrElse(def, f)); }, async ok() { return this.value.then((res) => res.ok()); }, or(v) { return future(this.value.then((res) => res.orFuture(v).value)); }, orElse(f) { return future(this.value.then((res) => res.orElseFuture(f).value)); }, async unwrap() { return this.value.then((res) => res.unwrap()); }, async unwrapErr() { return this.value.then((res) => res.unwrapErr()); }, async unwrapOr(def) { return this.value.then((res) => res.unwrapOr(def)); }, async unwrapOrElse(def) { return this.value.then((res) => res.unwrapOrElse(def)); }, andPromise(v) { return future(this.value.then((res) => res.andPromise(v).value)); }, andResult(v) { return this.and(future(v)); }, andThenResult(f) { return this.andThen((v) => future(f(v))); }, andThenPromise(f) { return future(this.value.then((res) => res.andThenPromise(f).value)); }, orPromise(v) { return future(this.value.then((res) => res.orPromise(v).value)); }, orResult(v) { return this.or(future(v)); }, orElseResult(f) { return this.orElse((e) => future(f(e))); }, orElsePromise(f) { return future(this.value.then((res) => res.orElsePromise(f).value)); }, sleep(delay) { return future(new Promise((res) => setTimeout(() => res(this.value), delay))); } }; var tryTask = (f) => { return future(f().then((v) => ok(v)).catch((r) => err(r))); }; var tryPromise = (v) => { return future(v.then((v2) => ok(v2)).catch((r) => err(r))); }; var future = (v) => { const a = Object.create(FutureProto); a.value = Promise.resolve(v); a.tag = "future"; return a; }; // src/Iterator.ts var Iterator_exports = {}; __export(Iterator_exports, { OnceIterator: () => OnceIterator }); var OnceIterator = class _OnceIterator { constructor(value) { this.value = value; } done = false; next(value) { if (this.done) { return { done: true, value }; } this.done = true; return { done: false, value: this.value }; } [Symbol.iterator]() { return new _OnceIterator(this.value); } }; // src/Option.ts var Option_exports = {}; __export(Option_exports, { OptionProto: () => OptionProto, bind: () => bind, fromNullable: () => fromNullable, fromResult: () => fromResult, isNone: () => isNone, isSome: () => isSome, none: () => none, some: () => some, tryCatch: () => tryCatch }); var isSome = (r) => r.tag === "some"; var isNone = (r) => r.tag === "none"; var OptionProto = () => ({ and(v) { return isSome(this) ? v : none(); }, andThen(f) { return isSome(this) ? f(this.value) : none(); }, expect(msg) { if (isSome(this)) { return this.value; } else { throw new Error(msg); } }, filter(f) { return isNone(this) ? none() : f(this.value) ? this : none(); }, flatten() { return isSome(this) ? this.value : none(); }, inspect(f) { if (isSome(this)) { f(this.value); return some(this.value); } else { return none(); } }, isNone() { return isNone(this); }, isNoneOr(f) { return isNone(this) || f(this.value); }, isSome() { return isSome(this); }, isSomeAnd(f) { return isNone(this) ? false : f(this.value); }, map(f) { return isSome(this) ? some(f(this.value)) : none(); }, mapOr(def, f) { return isSome(this) ? f(this.value) : def; }, mapOrElse(d, f) { return isSome(this) ? f(this.value) : d(); }, okOr(v) { return isSome(this) ? ok(this.value) : err(v); }, okOrElse(f) { return isSome(this) ? ok(this.value) : err(f()); }, or(v) { return isSome(this) ? this : v; }, orElse(f) { return isSome(this) ? this : f(); }, transpose() { return isNone(this) ? ok(none()) : isErr(this.value) ? err(this.value.value) : ok(some(this.value.value)); }, unwrap() { if (isSome(this)) { return this.value; } else { throw new Error("called `Option.unwrap()` on a `None` value"); } }, unwrapOr(def) { return isSome(this) ? this.value : def; }, unwrapOrElse(def) { return isSome(this) ? this.value : def(); }, xor(v) { return isSome(this) && isSome(v) ? this : isNone(this) && isSome(v) ? v : none(); }, [Symbol.iterator]() { return new OnceIterator(this); } }); var fromNullable = (v) => v == null ? none() : some(v); var fromResult = (v) => v.ok(); var tryCatch = (f) => { try { return some(f()); } catch (_) { return none(); } }; var some = (v) => { const a = Object.create(OptionProto()); a.value = v; a.tag = "some"; return a; }; var none = () => { const a = Object.create(OptionProto()); a.tag = "none"; return a; }; var bind = (fn) => { const iter = fn(); let result = iter.next(); while (true) { if (result.done) { return some(result.value); } if (isSome(result.value)) { result = iter.next(result.value.value); } else { return result.value; } } }; // src/Result.ts var isOk = (r) => r.tag === "ok"; var isErr = (r) => r.tag === "err"; var ResultProto = () => ({ and(f) { return isOk(this) ? ok(f(this.value)) : err(this.value); }, andThen(f) { return isOk(this) ? f(this.value) : err(this.value); }, err() { return isErr(this) ? some(this.value) : none(); }, expect(msg) { if (isOk(this)) { return this.value; } else { throw new Error(msg); } }, expectErr(msg) { if (isErr(this)) { return this.value; } else { throw new Error(msg); } }, flatten() { return this.andThen((v) => v); }, inspect(f) { if (isOk(this)) { f(this.value); return ok(this.value); } else { return err(this.value); } }, inspectErr(f) { if (isErr(this)) { f(this.value); return err(this.value); } else { return ok(this.value); } }, isErr() { return isErr(this); }, isErrAnd(f) { return isErr(this) && f(this.value); }, isOk() { return isOk(this); }, isOkAnd(f) { return isOk(this) && f(this.value); }, map(f) { return isOk(this) ? ok(f(this.value)) : err(this.value); }, mapErr(f) { return isErr(this) ? err(f(this.value)) : ok(this.value); }, mapOr(def, f) { return isOk(this) ? f(this.value) : def; }, mapOrElse(def, f) { return isOk(this) ? f(this.value) : def(this.value); }, ok() { return isOk(this) ? some(this.value) : none(); }, or(f) { return isErr(this) ? err(f(this.value)) : ok(this.value); }, orElse(f) { return isErr(this) ? f(this.value) : ok(this.value); }, transpose() { return isErr(this) ? some(err(this.value)) : isNone(this.value) ? none() : some(ok(this.value.value)); }, unwrap() { if (isOk(this)) { return this.value; } else { throw new Error(`called \`Result.unwrap()\` on an \`Err\` value: ${String(this.value)}`); } }, unwrapErr() { if (isErr(this)) { return this.value; } else { throw new Error(`called \`Result.unwrap()\` on an \`Ok\` value: ${String(this.value)}`); } }, unwrapOr(def) { return isOk(this) ? this.value : def; }, unwrapOrElse(def) { return isOk(this) ? this.value : def(this.value); }, andPromise(v) { return isOk(this) ? tryPromise(v) : future(this); }, andFuture(v) { return isOk(this) ? v : future(this); }, andThenPromise(f) { return isOk(this) ? tryPromise(f(this.value)) : future(this); }, andThenFuture(f) { return isOk(this) ? f(this.value) : future(this); }, orPromise(v) { return isErr(this) ? tryPromise(v) : future(this); }, orFuture(v) { return isErr(this) ? v : future(this); }, orElseFuture(f) { return isErr(this) ? f(this.value) : future(this); }, orElsePromise(f) { return isErr(this) ? tryPromise(f(this.value)) : future(this); }, [Symbol.iterator]() { return new OnceIterator(this); } }); var fromNullable2 = (v, f) => v == null ? err(f()) : ok(v); var fromOption = (v, f) => v.okOr(f()); var tryCatch2 = (f) => { try { return ok(f()); } catch (e) { return err(e); } }; var ok = (v) => { const a = Object.create(ResultProto()); a.value = v; a.tag = "ok"; return a; }; var err = (v) => { const a = Object.create(ResultProto()); a.value = v; a.tag = "err"; return a; }; var bind2 = (fn) => { const iter = fn(); let result = iter.next(); while (true) { if (result.done) { return ok(result.value); } if (isOk(result.value)) { result = iter.next(result.value.value); } else { return result.value; } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Future, Iterator, Option, Result });