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

180 lines 6.8 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. * */ Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); describe("misc-utils", () => { test("_callIfTruthy(condition, fun)", () => { const contextObject = { foo: 1 }; let executed = false; const returnValue = (0, index_1._callIfTruthy)(contextObject, (it) => { expect(it).toBeDefined(); expect(it).toEqual({ foo: 1 }); executed = true; }); expect(returnValue).toBeTruthy(); expect(returnValue).toBe(contextObject); expect(executed).toBeTruthy(); }); test("_callIfFalsy(condition, fun)", () => { let executedIfNull = false; let executedIfUndefined = false; (0, index_1._callIfFalsy)(undefined, () => { executedIfUndefined = true; }); (0, index_1._callIfFalsy)(null, () => { executedIfNull = true; }); expect(executedIfUndefined).toBeTruthy(); expect(executedIfNull).toBeTruthy(); }); test("_repeat(count, fun)", () => { let count = 0; (0, index_1._repeat)(5, () => count++); expect(count).toEqual(5); }); test("_callIfTrue(condition, fun)", () => { let flag = false; const fun = () => { flag = true; }; (0, index_1._callIfTrue)(false, fun); expect(flag).toBeFalsy(); (0, index_1._callIfTrue)(true, fun); expect(flag).toBeTruthy(); }); test("_callIfTrue(condition, onTrueFun, onFalseFun)", () => { const onTrueFn = (flags) => flags.onTrueFlag = true; const onFalseFn = (flags) => flags.onFalseFlag = true; (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, it => { (0, index_1._callIfTrue)(false, onTrueFn.bind(undefined, it), onFalseFn.bind(undefined, it)); expect(it.onTrueFlag).toBeFalsy(); expect(it.onFalseFlag).toBeTruthy(); }); (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, it => { (0, index_1._callIfFalse)(false, onFalseFn.bind(undefined, it), onTrueFn.bind(undefined, it)); expect(it.onTrueFlag).toBeFalsy(); expect(it.onFalseFlag).toBeTruthy(); }); }); test("_callIfFalse(condition, onTrueFun, onFalseFun)", () => { let flag = true; const fun = () => { flag = false; }; (0, index_1._callIfFalse)(true, fun); expect(flag).toBeTruthy(); (0, index_1._callIfFalse)(false, fun); expect(flag).toBeFalsy(); }); test("_callIfTruthyWithReturn(condition, onTrueFunWithReturn, onFalseFunWithReturn)", () => { // Condition is truthy. (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, flags => { const returnValue = (0, index_1._callIfTruthyWithReturn)("foo", (it) => { expect(it).toEqual("foo"); flags.onTrueFlag = true; return "true"; }, () => { flags.onFalseFlag = false; return "false"; }); expect(returnValue).toEqual("true"); expect(flags.onTrueFlag).toBeTruthy(); expect(flags.onFalseFlag).toBeFalsy(); }); // Condition is falsy. (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, flags => { const returnValue = (0, index_1._callIfTruthyWithReturn)(undefined, (it) => { expect(it).toBeFalsy(); flags.onTrueFlag = false; return "true"; }, () => { flags.onFalseFlag = true; return "false"; }); expect(returnValue).toEqual("false"); expect(flags.onTrueFlag).toBeFalsy(); expect(flags.onFalseFlag).toBeTruthy(); }); }); test("_callIfTrueWithReturn(condition, onTrueFunWithReturn, onFalseFunWithReturn)", () => { // Condition is true. (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, flags => { const returnValue = (0, index_1._callIfTrueWithReturn)(true, () => { flags.onTrueFlag = true; return "true"; }, () => { flags.onFalseFlag = false; return "false"; }); expect(returnValue).toEqual("true"); expect(flags.onTrueFlag).toBeTruthy(); expect(flags.onFalseFlag).toBeFalsy(); }); // Condition is false. (0, index_1._also)({ onTrueFlag: false, onFalseFlag: false }, flags => { const returnValue = (0, index_1._callIfTrueWithReturn)(false, () => { flags.onTrueFlag = false; return "true"; }, () => { flags.onFalseFlag = true; return "false"; }); expect(returnValue).toEqual("false"); expect(flags.onTrueFlag).toBeFalsy(); expect(flags.onFalseFlag).toBeTruthy(); }); }); }); test("Data class works", () => { class MapData extends index_1.Data { name; type; map; array; constructor(name = "MapData contains properties: string, string, Map, Array", type = "string", map = new Map().set("foo", "1").set("bar", "2"), array = ["one", "two", "three"]) { super(); this.name = name; this.type = type; this.map = map; this.array = array; } } const mapData = new MapData(); const toString = mapData.toString(); const matchString = "{\n \"name\": \"MapData contains properties: string, string, Map, Array\",\n \"type\": \"string\",\n \"map\": {\n \"foo\": \"1\",\n \"bar\": \"2\"\n },\n \"array\": [\n \"one\",\n \"two\",\n \"three\"\n ]\n}"; expect(toString).toEqual(matchString); }); //# sourceMappingURL=misc-utils.test.js.map