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
155 lines • 8.5 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");
const use_keyboard_internal_1 = require("../tui-node-keyboard/use-keyboard-internal");
test("KeyPress works", () => {
(0, index_1._also)((0, index_1.createMutableCopyOf)(undefined, undefined), it => expect(it.toString()).toEqual(""));
(0, index_1._also)((0, index_1.createMutableCopyOf)(undefined, "a"), it => {
expect(it.toString()).toEqual("a");
expect(it.input).toEqual("a");
expect(it.key).toEqual("");
expect(it.matches("a")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(undefined, "q"), it => {
expect(it.toString()).toEqual("q");
expect(it.input).toEqual("q");
expect(it.key).toEqual("");
expect(it.matches("q")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.ctrlKey, "q"), it => {
expect(it.toString()).toEqual("ctrl+q");
expect(it.input).toEqual("q");
expect(it.key).toEqual("ctrl");
expect(it.matches("ctrl+q")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.ctrlKey, "a"), it => {
expect(it.toString()).toEqual("ctrl+a");
expect(it.input).toEqual("a");
expect(it.key).toEqual("ctrl");
expect(it.matches("ctrl+a")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.escapeKey, undefined), it => {
expect(it.toString()).toEqual("escape");
expect(it.input).toEqual("");
expect(it.key).toEqual("escape");
expect(it.matches("escape")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.tabKey, undefined), it => {
const immutableCopy = it.setModifierKey("shift", true);
expect(immutableCopy.toString()).toEqual("shift+tab");
expect(immutableCopy.input).toEqual("");
expect(immutableCopy.key).toEqual("shift+tab");
expect(immutableCopy.matches("shift+tab")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.tabKey, undefined), it => {
const immutableCopy = it.setModifierKey("meta", true);
expect(immutableCopy.toString()).toEqual("meta+tab");
expect(immutableCopy.input).toEqual("");
expect(immutableCopy.key).toEqual("meta+tab");
expect(immutableCopy.matches("meta+tab")).toBeTruthy();
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.tabKey, undefined), it => {
const immutableCopy = it.setModifierKey("ctrl", true);
expect(immutableCopy.toString()).toEqual("ctrl+tab");
expect(immutableCopy.input).toEqual("");
expect(immutableCopy.key).toEqual("ctrl+tab");
expect(immutableCopy.matches("ctrl+tab")).toBeTruthy();
});
});
test("processKeyPress works", () => {
let fun1Flag = false;
let fun2State = "";
function fun1() {
fun1Flag = true;
}
function fun2(arg) {
fun2State = arg;
}
const shortcutsToActionMap = (0, index_1._also)((0, index_1.createNewShortcutToActionMap)(), map => map
.set("q", fun1)
.set("ctrl+q", fun1)
.set("!", fun2.bind(this, "1"))
.set("@", fun2.bind(this, "2"))
.set("#", fun2.bind(this, "3")));
(0, use_keyboard_internal_1.tryToRunActionForShortcut)(index_1.Option.some((0, index_1.createMutableCopyOf)(undefined, "q")), shortcutsToActionMap);
expect(fun1Flag).toBeTruthy();
fun1Flag = false;
(0, use_keyboard_internal_1.tryToRunActionForShortcut)(index_1.Option.some((0, index_1.createMutableCopyOf)(index_1.KeyCreator.ctrlKey, "q")), shortcutsToActionMap);
expect(fun1Flag).toBeTruthy();
(0, use_keyboard_internal_1.tryToRunActionForShortcut)(index_1.Option.some((0, index_1.createMutableCopyOf)(undefined, "!")), shortcutsToActionMap);
expect(fun2State).toEqual("1");
(0, use_keyboard_internal_1.tryToRunActionForShortcut)(index_1.Option.some((0, index_1.createMutableCopyOf)(undefined, "@")), shortcutsToActionMap);
expect(fun2State).toEqual("2");
(0, use_keyboard_internal_1.tryToRunActionForShortcut)(index_1.Option.some((0, index_1.createMutableCopyOf)(undefined, "#")), shortcutsToActionMap);
expect(fun2State).toEqual("3");
});
test("KeyPress isSpecialKey works (and validate KeyCreator constants)", () => {
// These are special keys:
// "upArrow", "downArrow", "leftArrow", "rightArrow",
// "pageDown", "pageUp",
// "return", "escape", "tab", "backspace", "delete"
(0, index_1._let)([
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.upKey, undefined), "uparrow"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.downKey, undefined), "downarrow"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.leftKey, undefined), "leftarrow"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.rightKey, undefined), "rightarrow"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.pageUpKey, undefined), "pageup"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.pageDownKey, undefined), "pagedown"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.returnKey, undefined), "return"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.escapeKey, undefined), "escape"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.tabKey, undefined), "tab"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.backspaceKey, undefined), "backspace"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.spaceKey, undefined), "space"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.deleteKey, undefined), "delete"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.homeKey, undefined), "home"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.endKey, undefined), "end"], /* Tuple */
], array => array.forEach(tuple => {
const [keyPress, shortcut] = tuple;
expect(keyPress.isSpecialKey()).toBeTruthy();
expect(keyPress.toString()).toEqual(shortcut);
}));
// These are not special keys: "ctrl", "meta", "shift".
(0, index_1._let)([
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.ctrlKey, undefined), "ctrl"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.ctrlKey, "a"), "ctrl+a"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.metaKey, undefined), "meta"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.shiftKey, undefined), "shift"],
[(0, index_1.createMutableCopyOf)(index_1.KeyCreator.shiftKey, "b"), "shift+b"], /* tuple */
], array => array.forEach(tuple => {
const [keyPress, shortcut] = tuple;
expect(keyPress.isSpecialKey()).toBeFalsy();
expect(keyPress.isModifierKey()).toBeTruthy();
expect(keyPress.toString()).toEqual(shortcut);
}));
});
test("KeyPress modifier keys work", () => {
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.rightKey, undefined), it => {
const immutableCopy = it.setModifierKey("shift", true);
expect(immutableCopy.toString()).toEqual("shift+rightarrow");
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.rightKey, undefined), it => {
const immutableCopy = it.setModifierKey("meta", true);
expect(immutableCopy.toString()).toEqual("meta+rightarrow");
});
(0, index_1._also)((0, index_1.createMutableCopyOf)(index_1.KeyCreator.rightKey, undefined), it => {
const immutableCopy = it.setModifierKey("ctrl", true);
expect(immutableCopy.toString()).toEqual("ctrl+rightarrow");
});
});
//# sourceMappingURL=use-keyboard-common.test.js.map
;