UNPKG

rustlike-ts

Version:

A Rust-like functional utility library for safe and expressive error handling in TypeScript.

315 lines (310 loc) 5.81 kB
"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 index_exports = {}; __export(index_exports, { Err: () => Err, None: () => None, Ok: () => Ok, OptionError: () => OptionError, ResultError: () => ResultError, Some: () => Some, filterMapOption: () => filterMapOption, filterMapResult: () => filterMapResult, mapOption: () => mapOption, mapResult: () => mapResult, matchOption: () => matchOption, matchResult: () => matchResult }); module.exports = __toCommonJS(index_exports); // src/option.ts var OptionError = class extends Error { name = "OptionError"; }; var _Some = class { #data; constructor(data) { this.#data = data; } isSome() { return true; } isNone() { return false; } unwrap() { return this.#data; } expect(_) { return this.#data; } unwrapOr(_) { return this.#data; } unwrapOrElse(_) { return this.#data; } map(fn) { this.#data = fn(this.#data); return this; } mapOr(_, fn) { return fn(this.#data); } mapOrElse(_, someFn) { return someFn(this.#data); } and(option) { return option.isSome() ? option : new _None(); } or(_or) { return this; } okOr(_) { return Ok(this.#data); } okOrElse(_) { return Ok(this.#data); } andThen(fn) { return fn(this.#data); } filter(fn) { return fn(this.#data) ? this : new _None(); } }; var _None = class { isSome() { return false; } isNone() { return true; } unwrap() { throw new OptionError("Attempted to unwrap a None value!"); } expect(message) { throw new OptionError(message); } unwrapOr(or) { return or; } unwrapOrElse(fn) { return fn(); } map(_) { return this; } mapOr(fallback, _) { return fallback; } mapOrElse(noneFn, _) { return noneFn(); } and(_) { return this; } or(or) { return or; } okOr(result) { return Err(result); } okOrElse(errFn) { return Err(errFn()); } andThen(_) { return this; } filter(_) { return this; } }; function Some(data) { return new _Some(data); } var None = new _None(); // src/result.ts var ResultError = class extends Error { name = "ResultError"; }; var _Ok = class { #data; constructor(data) { this.#data = data; } unwrap() { return this.#data; } unwrapErr() { throw new ResultError( `Called unwrapErr() on an Ok value: ${JSON.stringify(this.#data)}` ); } unwrapOr(_) { return this.#data; } unwrapOrElse(_) { return this.#data; } expect(_) { return this.#data; } expectErr(message) { throw new ResultError(message); } isOk() { return true; } isErr() { return false; } ok() { return Some(this.#data); } err() { return None; } map(fn) { this.#data = fn(this.#data); return this; } mapErr(_fn) { return this; } and(result) { return result; } andThen(fn) { return fn(this.#data); } or(_result) { return this; } orElse(_) { return this; } }; var _Err = class { #error; constructor(error) { this.#error = error; } unwrap() { throw new ResultError( `Called unwrap() on an Err value: ${JSON.stringify(this.#error)}` ); } unwrapErr() { return this.#error; } unwrapOr(or) { return or; } unwrapOrElse(fn) { return fn(this.#error); } expect(message) { throw new ResultError(message); } expectErr(_) { return this.#error; } isOk() { return false; } isErr() { return true; } ok() { return None; } err() { return Some(this.#error); } map(_fn) { return this; } mapErr(fn) { this.#error = fn(this.#error); return this; } and(_result) { return this; } andThen(_fn) { return this; } or(result) { return result; } orElse(fn) { return fn(this.#error); } }; function Ok(data) { return new _Ok(data); } function Err(error) { return new _Err(error); } // src/utils.ts function matchResult(result, op) { return result.isOk() ? op.Ok(result.unwrap()) : op.Err(result.unwrapErr()); } function matchOption(result, op) { return result.isSome() ? op.Some(result.unwrap()) : op.None(); } function mapOption(data, fn) { return data.map((d) => { if (d.isSome()) { return Some(fn(d.unwrap())); } return None; }); } function mapResult(data, fn) { return data.map((d) => { if (d.isOk()) { return Ok(fn(d.unwrap())); } return Err(d.unwrapErr()); }); } function filterMapOption(data, fn) { return data.map((d) => d.isSome() ? fn(d.unwrap()) : None).filter((d) => d.isSome()); } function filterMapResult(data, fn) { return data.map((d) => d.isOk() ? fn(d.unwrap()) : Err(d.unwrapErr())).filter((d) => d.isOk()); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Err, None, Ok, OptionError, ResultError, Some, filterMapOption, filterMapResult, mapOption, mapResult, matchOption, matchResult });