'use strict'; const OptionKindSymbol = Symbol("Option kind"); const ResultKindSymbol = Symbol("Result kind"); function isOption(o) { return o != null && typeof o === "object" && OptionKindSymbol in o; } function isResult(r) { return r != null && typeof r === "object" && ResultKindSymbol in r; } function Some(value) { const some = { [Symbol.toStringTag]: "Option", [OptionKindSymbol]: "Some", isSome() { return true; }, isNone() { return false; }, isSomeAnd(predicate) { return predicate(value); }, isSomeAndAsync(predicate) { return predicate(value); }, expect(_msg) { return value; }, unwrap() { return value; }, unwrapOr(_defaultValue) { return value; }, unwrapOrElse(_fn) { return value; }, unwrapOrElseAsync(_fn) { return Promise.resolve(value); }, okOr(_error) { return Ok(value); }, okOrElse(_err) { return Ok(value); }, transpose() { const r = value; assertResult(r); return r.isOk() ? Ok(Some(r.unwrap())) : Err(r.unwrapErr()); }, filter(predicate) { return predicate(value) ? some : None; }, flatten() { const o = value; assertOption(o); return o; }, map(fn) { return Some(fn(value)); }, mapOr(_defaultValue, fn) { return fn(value); }, mapOrElse(_defaultFn, fn) { return fn(value); }, zip(other) { assertOption(other); return other.isSome() ? Some([value, other.unwrap()]) : None; }, zipWith(other, fn) { assertOption(other); return other.isSome() ? Some(fn(value, other.unwrap())) : None; }, unzip() { const tuple = value; if (!Array.isArray(tuple) || tuple.length !== 2) { throw new TypeError("Unzip format is incorrect."); } const [a, b] = tuple; return [Some(a), Some(b)]; }, and(other) { assertOption(other); return other; }, andThen(fn) { return fn(value); }, andThenAsync(fn) { return fn(value); }, or(_other) { return some; }, orElse(_fn) { return some; }, orElseAsync(_fn) { return Promise.resolve(some); }, xor(other) { assertOption(other); return other.isSome() ? None : some; }, inspect(fn) { fn(value); return some; }, eq(other) { assertOption(other); return other.isSome() && other.unwrap() === value; }, toString() { return `Some(${value})`; } }; return some; } const None = Object.freeze({ [Symbol.toStringTag]: "Option", [OptionKindSymbol]: "None", isSome() { return false; }, isNone() { return true; }, isSomeAnd(_predicate) { return false; }, isSomeAndAsync(_predicate) { return Promise.resolve(false); }, expect(msg) { throw new TypeError(msg); }, unwrap() { throw new TypeError("Called `Option::unwrap()` on a `None` value"); }, unwrapOr(defaultValue) { return defaultValue; }, unwrapOrElse(fn) { return fn(); }, unwrapOrElseAsync(fn) { return fn(); }, okOr(error) { return Err(error); }, okOrElse(err) { return Err(err()); }, transpose() { return Ok(None); }, filter(_predicate) { return None; }, flatten() { return None; }, map(_fn) { return None; }, mapOr(defaultValue, _fn) { return defaultValue; }, mapOrElse(defaultFn, _fn) { return defaultFn(); }, zip(_other) { return None; }, zipWith(_other, _fn) { return None; }, unzip() { return [None, None]; }, and(_other) { return None; }, andThen(_fn) { return None; }, andThenAsync(_fn) { return Promise.resolve(None); }, or(other) { assertOption(other); return other; }, orElse(fn) { return fn(); }, orElseAsync(fn) { return fn(); }, xor(other) { assertOption(other); return other.isSome() ? other : None; }, inspect(_fn) { return None; }, eq(other) { assertOption(other); return other === None; }, toString() { return "None"; } }); function Ok(value) { const ok = { [Symbol.toStringTag]: "Result", [ResultKindSymbol]: "Ok", isOk() { return true; }, isErr() { return false; }, isOkAnd(predicate) { return predicate(value); }, isOkAndAsync(predicate) { return predicate(value); }, isErrAnd(_predicate) { return false; }, isErrAndAsync(_predicate) { return Promise.resolve(false); }, expect(_msg) { return value; }, unwrap() { return value; }, unwrapOr(_defaultValue) { return value; }, unwrapOrElse(_fn) { return value; }, unwrapOrElseAsync(_fn) { return Promise.resolve(value); }, expectErr(msg) { throw new TypeError(`${msg}: ${value}`); }, unwrapErr() { throw new TypeError("Called `Result::unwrapErr()` on an `Ok` value"); }, ok() { return Some(value); }, err() { return None; }, transpose() { const o = value; assertOption(o); return o.isSome() ? Some(Ok(o.unwrap())) : None; }, map(fn) { return Ok(fn(value)); }, mapErr(_fn) { return Ok(value); }, mapOr(_defaultValue, fn) { return fn(value); }, mapOrElse(_defaultFn, fn) { return fn(value); }, flatten() { const r = value; assertResult(r); return r; }, and(other) { assertResult(other); return other; }, or(_other) { return ok; }, andThen(fn) { return fn(value); }, andThenAsync(fn) { return fn(value); }, orElse(_fn) { return ok; }, orElseAsync(_fn) { return Promise.resolve(ok); }, inspect(fn) { fn(value); return ok; }, inspectErr(_fn) { return ok; }, eq(other) { assertResult(other); return other.isOk() && other.unwrap() === value; }, asOk() { return ok; }, asErr() { throw new TypeError("Called `Result::asErr()` on an `Ok` value"); }, toString() { return `Ok(${value})`; } }; return ok; } function Err(error) { const err = { [Symbol.toStringTag]: "Result", [ResultKindSymbol]: "Err", isOk() { return false; }, isErr() { return true; }, isOkAnd(_predicate) { return false; }, isOkAndAsync(_predicate) { return Promise.resolve(false); }, isErrAnd(predicate) { return predicate(error); }, isErrAndAsync(predicate) { return predicate(error); }, expect(msg) { throw new TypeError(`${msg}: ${error}`); }, unwrap() { throw new TypeError("Called `Result::unwrap()` on an `Err` value"); }, unwrapOr(defaultValue) { return defaultValue; }, unwrapOrElse(fn) { return fn(error); }, unwrapOrElseAsync(fn) { return fn(error); }, expectErr(_msg) { return error; }, unwrapErr() { return error; }, ok() { return None; }, err() { return Some(error); }, transpose() { return Some(err); }, map(_fn) { return Err(error); }, mapErr(fn) { return Err(fn(error)); }, mapOr(defaultValue, _fn) { return defaultValue; }, mapOrElse(defaultFn, _fn) { return defaultFn(error); }, flatten() { return err; }, and(_other) { return err; }, or(other) { assertResult(other); return other; }, andThen(_fn) { return err; }, andThenAsync(_fn) { return Promise.resolve(err); }, orElse(fn) { return fn(error); }, orElseAsync(fn) { return fn(error); }, inspect(_fn) { return err; }, inspectErr(fn) { fn(error); return err; }, eq(other) { assertResult(other); return other.isErr() && other.unwrapErr() === error; }, asOk() { throw new TypeError("Called `Result::asOk()` on an `Err` value"); }, asErr() { return err; }, toString() { return `Err(${error})`; } }; return err; } function assertOption(o) { if (!isOption(o)) { throw new TypeError(`This(${o}) is not an Option`); } } function assertResult(r) { if (!isResult(r)) { throw new TypeError(`This(${r}) is not a Result`); } } const RESULT_TRUE = Ok(true); const RESULT_FALSE = Ok(false); const RESULT_ZERO = Ok(0); const RESULT_VOID = Ok(); function promiseToAsyncResult(p) { return p.then((x) => { return Ok(x); }).catch((err) => { return Err(err); }); } exports.Err = Err; exports.None = None; exports.Ok = Ok; exports.RESULT_FALSE = RESULT_FALSE; exports.RESULT_TRUE = RESULT_TRUE; exports.RESULT_VOID = RESULT_VOID; exports.RESULT_ZERO = RESULT_ZERO; exports.Some = Some; exports.isOption = isOption; exports.isResult = isResult; exports.promiseToAsyncResult = promiseToAsyncResult; //# sourceMappingURL=main.cjs.map