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

113 lines 4.44 kB
"use strict"; /* * Copyright (c) 2022 R3BL LLC. All rights reserved. * * 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 * * http://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. * */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports._with = exports._apply = exports._letSafe = exports._let = exports._alsoSafeAsync = exports._alsoAsync = exports._alsoSafe = exports._also = exports._then = void 0; const lodash_1 = __importDefault(require("lodash")); /** * @param contextObject value of `it` * @param receiverFns array of lambdas that accepts `it` * @return contextObject that is passed */ const _then = (contextObject, ...receiverFns) => { receiverFns.forEach((fun) => fun(contextObject)); return contextObject; }; exports._then = _then; /** * @param contextObject value of `it` * @param receiverFn lambda that accepts `it` * @return contextObject that is passed */ const _also = (contextObject, receiverFn) => { receiverFn(contextObject); return contextObject; }; exports._also = _also; /** * @param contextObject value of `it` * @param receiverFn lambda that accepts deep copy of `it` * @return deep copy of `it` */ const _alsoSafe = (contextObject, receiverFn) => { const ctxCopy = lodash_1.default.cloneDeep(contextObject); receiverFn(ctxCopy); return ctxCopy; }; exports._alsoSafe = _alsoSafe; /** * @param contextObject value of `it` * @param receiverFn lambda that accepts `it` * @return {contextObject that is passed, promise returned by receiverFn} */ const _alsoAsync = (contextObject, receiverFn) => { const promiseFromReceiverFn = receiverFn(contextObject); return { contextObject, promiseFromReceiverFn }; }; exports._alsoAsync = _alsoAsync; /** * @param contextObject value of `it` * @param receiverFn lambda that accepts deep copy of `it` * @return {contextObjectDeepCopy deep copy of `it`, promise returned by receiverFn} */ const _alsoSafeAsync = (contextObject, receiverFn) => { const ctxCopy = lodash_1.default.cloneDeep(contextObject); const promiseFromReceiverFn = receiverFn(ctxCopy); return { contextObjectDeepCopy: ctxCopy, promiseFromReceiverFn }; }; exports._alsoSafeAsync = _alsoSafeAsync; /** * @param contextObject value of `it` * @param receiverFnWithReturn lambda that accepts `it` * @return result of the receiverFnWithReturn (lambda) */ const _let = (contextObject, receiverFnWithReturn) => { return receiverFnWithReturn(contextObject); }; exports._let = _let; /** * @param contextObject value of `it` * @param receiverFnWithReturn lambda that accepts deep copy of `it` * @return result of the receiverFnWithReturn (lambda) */ const _letSafe = (contextObject, receiverFnWithReturn) => { return receiverFnWithReturn(lodash_1.default.cloneDeep(contextObject)); }; exports._letSafe = _letSafe; /** * @param contextObject value of `this` (in the `blockWithReboundThis` function) * @param objectContainingFnWithReboundThis object containing function `blockWithReboundThis` * which accepts contextObject (aka `this`) * @return contextObject that is passed */ const _apply = (contextObject, objectContainingFnWithReboundThis) => { objectContainingFnWithReboundThis.fnWithReboundThis.bind(contextObject).call(contextObject); return contextObject; }; exports._apply = _apply; /** * @param contextObject value of `this` (in the `blockWithReboundThis` function) * @param objectContainingFnWithReboundThis object containing function `blockWithReboundThis` * which accepts contextObject (aka `this`) * @return result of the `blockWithReboundThis` function */ const _with = (contextObject, objectContainingFnWithReboundThis) => objectContainingFnWithReboundThis.fnWithReboundThis.bind(contextObject).call(contextObject); exports._with = _with; //# sourceMappingURL=kotlin-lang-utils.js.map