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
157 lines • 6.26 kB
JavaScript
/*
* 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");
it("_then() takes a contextObject, passes it to the ReceiverFn[], and returns the contextObject", () => {
const contextObject = "_then";
const flags = [false, false];
const fun1 = (it) => {
expect(it).toEqual(contextObject);
flags[0] = true;
};
const fun2 = (it) => {
expect(it).toEqual(contextObject);
flags[1] = true;
};
const returnValue = (0, index_1._then)(contextObject, fun1, fun2);
expect(returnValue).toEqual(contextObject);
expect(flags[0]).toBeTruthy();
expect(flags[1]).toBeTruthy();
});
it("_also() takes a contextObject, passes it to the ReceiverFn, and returns the contextObject", () => {
const contextObject = "_also";
let flag = false;
const returnValue = (0, index_1._also)(contextObject, (it) => {
expect(it).toBe(contextObject);
flag = true;
});
expect(returnValue).toEqual(contextObject);
expect(flag).toBeTruthy();
});
it("_alsoSafe() takes a contextObject, passes a deep copy of it to the ReceiverFn, and returns the copy", () => {
const contextObject = { foo: 1 };
let flag = false;
const fun = (it) => {
expect(it).not.toBe(contextObject);
flag = true;
};
const returnValue = (0, index_1._alsoSafe)(contextObject, fun);
expect(returnValue).not.toBe(contextObject);
expect(returnValue).toEqual(contextObject);
expect(flag).toBeTruthy();
});
it("_alsoAsync() takes a contextObject, passes it to the ReceiverFnAsync, and returns it and" +
" promise from ReceiverFnAsync", async () => {
const contextObject = "_alsoAsync";
let flag = false;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
const fun1 = async (it) => new Promise((resolveFn) => {
const _fun = () => {
expect(it).toEqual(contextObject);
flag = true;
resolveFn(true);
};
// Delay execution of _fun to next iteration of event loop.
// https://nodejs.dev/learn/understanding-setimmediate
setImmediate(_fun);
});
const { contextObject: obj, promiseFromReceiverFn: promise } = (0, index_1._alsoAsync)(contextObject, fun1);
expect(obj).toEqual(contextObject);
expect(flag).toBeFalsy();
const value = await promise;
expect(value).toBeTruthy();
expect(flag).toBeTruthy();
});
it("_alsoSafeAsync() takes a contextObject, passes a deep copy of it to the ReceiverFnAsync," +
" and returns the deep copy and the promise from ReceiverFnAsync", async () => {
const contextObject = { foo: 1 };
let flag = false;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
const fun1 = async (it) => new Promise((resolveFn) => {
const _fun = () => {
expect(it).toEqual(contextObject);
expect(it).not.toBe(contextObject);
flag = true;
resolveFn(true);
};
// Delay execution of _fun to next iteration of event loop.
// https://nodejs.dev/learn/understanding-setimmediate
setImmediate(_fun);
});
const { contextObjectDeepCopy: obj, promiseFromReceiverFn: promise } = (0, index_1._alsoSafeAsync)(contextObject, fun1);
expect(obj).toEqual(contextObject);
expect(flag).toBeFalsy();
const value = await promise;
expect(value).toBeTruthy();
expect(flag).toBeTruthy();
});
it("_let() takes a contextObject, passes it to the ReceiverFnWithReturn, and returns its return value", () => {
const contextObject = "_let";
let flag = false;
const returnValue = (0, index_1._let)(contextObject, (it) => {
expect(contextObject).toBe(it);
flag = true;
return "foo";
});
expect(returnValue).toEqual("foo");
expect(flag).toBeTruthy();
});
it("_letSafe() takes a contextObject, passes a deep copy of it to the ReceiverFnWithReturn, and" +
" returns its return value", () => {
const contextObject = { foo: 1 };
let flag = false;
const returnValue = (0, index_1._letSafe)(contextObject, (it) => {
expect(it).toEqual(contextObject);
expect(it).not.toBe(contextObject);
flag = true;
return "foo";
});
expect(returnValue).toEqual("foo");
expect(flag).toBeTruthy();
});
it("_apply() takes a contextObject, binds it to ImplicitReceiverObject's this, calls it, then" +
" returns the contextObject", () => {
const contextObject = "_apply";
let flag = false;
const myImplicitReceiverObject = {
fnWithReboundThis: function () {
expect(this).toEqual(contextObject);
flag = true;
return contextObject;
},
};
const returnValue = (0, index_1._apply)(contextObject, myImplicitReceiverObject);
expect(returnValue).toEqual(contextObject);
expect(flag).toBeTruthy();
});
it("_with() takes a contextObject, binds it to ImplicitReceiverObjectWithReturn's this, calls it," +
" then returns the its return value", () => {
const contextObject = "_with";
let flag = false;
const myImplicitReceiverObjectWithReturn = {
fnWithReboundThis: function () {
expect(this).toEqual(contextObject);
flag = true;
return "foo";
},
};
const returnValue = (0, index_1._with)(contextObject, myImplicitReceiverObjectWithReturn);
expect(returnValue).toEqual("foo");
expect(flag).toBeTruthy();
});
//# sourceMappingURL=kotlin-lang-utils.test.js.map
;