UNPKG

r3bl-ts-utils

Version:

The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu

90 lines (87 loc) 3 kB
"use strict"; /* Copyright 2022 R3BL LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.debug = exports.DebugStyle = exports._callIfNone = exports._callIfSome = exports.Some = exports.None = exports.Option = void 0; const style_builder_1 = require("../tui-colors/style-builder"); const data_class_1 = require("./data-class"); // Option is based on Rust's Option enum and it is intended to be used in place of null and // undefined. Expressing !undefined && !null in TS: https://stackoverflow.com/a/63046469/2085356 class Option { _isSome; constructor(value) { if (value === undefined || value === null) { this._isSome = false; } else { this._isSome = true; } } // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates isSome() { return this._isSome; } // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates isNone() { return !this._isSome; } static none = () => Object.freeze(new None()); static some = (arg) => Object.freeze(new Some(arg)); static create = (arg) => arg === undefined || arg === null ? Option.none() : Option.some(arg); } exports.Option = Option; class None extends Option { constructor() { super(undefined); } toString() { return `None`; } } exports.None = None; class Some extends Option { _value; constructor(value) { super(value); this._value = value; } toString() { return `Some(${(0, data_class_1.anyToString)(this.value)})`; } get value() { return this._value; } } exports.Some = Some; // Expressions that work w/ `Option`. const _callIfSome = (context, receiver) => { if (context instanceof Some) receiver(context.value); }; exports._callIfSome = _callIfSome; const _callIfNone = (context, receiver) => { if (context instanceof None) receiver(); }; exports._callIfNone = _callIfNone; // Similar to `debug!` macro in Rust. exports.DebugStyle = Object.freeze({ msgStyle: style_builder_1.TextColor.builder.cyan.build(), argStyle: style_builder_1.TextColor.builder.green.build(), }); function debug(msg, obj) { console.log(exports.DebugStyle.msgStyle(msg), exports.DebugStyle.argStyle((0, data_class_1.anyToString)(obj))); return obj; } exports.debug = debug; //# sourceMappingURL=rust-lang-utils.js.map