ts-results-es
Version:
A TypeScript implementation of Rust's Result and Option objects.
252 lines • 8.15 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { AsyncOption } from './asyncoption.js';
import { toString } from './utils.js';
import { Ok, Err } from './result.js';
/**
* Contains the None value
*/
var NoneImpl = /** @class */ (function () {
function NoneImpl() {
}
NoneImpl.prototype.isSome = function () {
return false;
};
NoneImpl.prototype.isNone = function () {
return true;
};
NoneImpl.prototype[Symbol.iterator] = function () {
return {
next: function () {
return { done: true, value: undefined };
},
};
};
NoneImpl.prototype.unwrapOr = function (val) {
return val;
};
NoneImpl.prototype.unwrapOrElse = function (f) {
return f();
};
NoneImpl.prototype.expect = function (msg) {
throw new Error("".concat(msg));
};
NoneImpl.prototype.unwrap = function () {
throw new Error("Tried to unwrap None");
};
NoneImpl.prototype.map = function (_mapper) {
return this;
};
NoneImpl.prototype.mapOr = function (default_, _mapper) {
return default_;
};
NoneImpl.prototype.mapOrElse = function (default_, _mapper) {
return default_();
};
NoneImpl.prototype.or = function (other) {
return other;
};
NoneImpl.prototype.orElse = function (other) {
return other();
};
NoneImpl.prototype.andThen = function (op) {
return this;
};
NoneImpl.prototype.toResult = function (error) {
return Err(error);
};
NoneImpl.prototype.toString = function () {
return 'None';
};
NoneImpl.prototype.toAsyncOption = function () {
return new AsyncOption(None);
};
return NoneImpl;
}());
// Export None as a singleton, then freeze it so it can't be modified
export var None = new NoneImpl();
Object.freeze(None);
/**
* Contains the success value
*/
var SomeImpl = /** @class */ (function () {
function SomeImpl(val) {
if (!(this instanceof SomeImpl)) {
return new SomeImpl(val);
}
this.value = val;
}
SomeImpl.prototype.isSome = function () {
return true;
};
SomeImpl.prototype.isNone = function () {
return false;
};
SomeImpl.prototype[Symbol.iterator] = function () {
return [this.value][Symbol.iterator]();
};
SomeImpl.prototype.unwrapOr = function (_val) {
return this.value;
};
SomeImpl.prototype.unwrapOrElse = function (_f) {
return this.value;
};
SomeImpl.prototype.expect = function (_msg) {
return this.value;
};
SomeImpl.prototype.unwrap = function () {
return this.value;
};
SomeImpl.prototype.map = function (mapper) {
return Some(mapper(this.value));
};
SomeImpl.prototype.mapOr = function (_default_, mapper) {
return mapper(this.value);
};
SomeImpl.prototype.mapOrElse = function (_default_, mapper) {
return mapper(this.value);
};
SomeImpl.prototype.or = function (_other) {
return this;
};
SomeImpl.prototype.orElse = function (_other) {
return this;
};
SomeImpl.prototype.andThen = function (mapper) {
return mapper(this.value);
};
SomeImpl.prototype.toResult = function (error) {
return Ok(this.value);
};
SomeImpl.prototype.toAsyncOption = function () {
return new AsyncOption(this);
};
SomeImpl.prototype.toString = function () {
return "Some(".concat(toString(this.value), ")");
};
/**
* An empty Some
*
* @example
* ```typescript
* const x: Option<void> = Some.EMPTY
* ```
*/
SomeImpl.EMPTY = new SomeImpl(undefined);
return SomeImpl;
}());
// This allows Some to be callable - possible because of the es5 compilation target
export var Some = SomeImpl;
export var Option;
(function (Option) {
function all(first) {
var rest = [];
for (var _i = 1; _i < arguments.length; _i++) {
rest[_i - 1] = arguments[_i];
}
var options = first === undefined ? [] : Array.isArray(first) ? first : __spreadArray([first], rest, true);
var someOption = [];
for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
var option = options_1[_a];
if (option.isSome()) {
someOption.push(option.value);
}
else {
return option;
}
}
return Some(someOption);
}
Option.all = all;
function any(first) {
var rest = [];
for (var _i = 1; _i < arguments.length; _i++) {
rest[_i - 1] = arguments[_i];
}
var options = first === undefined ? [] : Array.isArray(first) ? first : __spreadArray([first], rest, true);
// short-circuits
for (var _a = 0, options_2 = options; _a < options_2.length; _a++) {
var option = options_2[_a];
if (option.isSome()) {
return option;
}
else {
continue;
}
}
// it must be None
return None;
}
Option.any = any;
function isOption(value) {
return value instanceof Some || value === None;
}
Option.isOption = isOption;
/**
* Converts a nullable value to an {@link Option}.
* Returns {@link None} if the value is `null`, otherwise returns {@link Some} containing the value.
*
* See also {@link fromOptional} for `T | undefined` and {@link fromNullish} for `T | null | undefined`.
*
* @example
* ```typescript
* const value: string | null = 'hello';
* Option.fromNullable(value); // Some('hello'), type: Option<string>
*
* const missing: string | null = null;
* Option.fromNullable(missing); // None, type: Option<string>
* ```
*/
function fromNullable(value) {
return (value === null ? None : Some(value));
}
Option.fromNullable = fromNullable;
/**
* Converts an optional value to an {@link Option}.
* Returns {@link None} if the value is `undefined`, otherwise returns {@link Some} containing the value.
*
* See also {@link fromNullable} for `T | null` and {@link fromNullish} for `T | null | undefined`.
*
* @example
* ```typescript
* const value: string | undefined = 'hello';
* Option.fromOptional(value); // Some('hello'), type: Option<string>
*
* const missing: string | undefined = undefined;
* Option.fromOptional(missing); // None, type: Option<string>
* ```
*/
function fromOptional(value) {
return (value === undefined ? None : Some(value));
}
Option.fromOptional = fromOptional;
/**
* Converts a nullish value to an {@link Option}.
* Returns {@link None} if the value is `null` or `undefined`, otherwise returns {@link Some} containing the value.
*
* Prefer {@link fromNullable} for `T | null` or {@link fromOptional} for `T | undefined`.
* Use this method only when the value is already both nullable and optional and you genuinely
* want `null` and `undefined` to be treated the same.
*
* @example
* ```typescript
* const value: string | null | undefined = 'hello';
* Option.fromNullish(value); // Some('hello'), type: Option<string>
*
* const missing: string | null | undefined = null;
* Option.fromNullish(missing); // None, type: Option<string>
* ```
*/
function fromNullish(value) {
return value === null || value === undefined ? None : Some(value);
}
Option.fromNullish = fromNullish;
})(Option || (Option = {}));
//# sourceMappingURL=option.js.map