@benodiwal/rusty-ts
Version:
A TypeScript library providing Rust-like Result and Option types for safer and more expressive error handling and optional values.
37 lines (36 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Options = void 0;
class Options {
static some(value) {
return { type: 'Some', value: value };
}
static none() {
return { type: 'None' };
}
static isSome(option) {
return option.type === 'Some';
}
static isNone(option) {
return option.type === 'None';
}
static unwrap(option) {
if (Options.isSome(option)) {
return option.value;
}
throw new Error('Called unwrap on a None value');
}
static unwrapOr(option, defaultValue) {
return Options.isSome(option) ? option.value : defaultValue;
}
static unwraporElse(option, fn) {
return Options.isSome(option) ? option.value : fn();
}
static map(option, fn) {
return Options.isSome(option) ? Options.some(fn(option.value)) : option;
}
static andThen(option, fn) {
return Options.isSome(option) ? fn(option.value) : option;
}
}
exports.Options = Options;