From b3177cbd482f57eb4621b9c7a441870ee34a60e0 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 11 Jan 2024 11:23:28 -0500 Subject: [PATCH] Mess with function imports --- src/common/all_test.ts | 74 ++++++++++++++++++++++++++---------------- src/common/row.ts | 12 +++---- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/common/all_test.ts b/src/common/all_test.ts index 541302b..8b0c3a9 100644 --- a/src/common/all_test.ts +++ b/src/common/all_test.ts @@ -223,24 +223,28 @@ testSuite({ }, 'fns': { 'arrayInsert() strings': () => { + const { arrayInsert } = Fn; + const a = ['😺', '😸', '😹']; - const b = Fn.arrayInsert(a, 1, 'x'); + const b = arrayInsert(a, 1, 'x'); const c = ['😺', 'x', '😸', '😹']; assertEquals(b, c); - const d = Fn.arrayInsert(c, 17, 'y'); + const d = arrayInsert(c, 17, 'y'); const e = ['😺', 'x', '😸', '😹', 'y']; assertEquals(d, e); - assertEquals(Fn.arrayInsert([], 0, 'foo'), ['foo']); + assertEquals(arrayInsert([], 0, 'foo'), ['foo']); }, 'arrayInsert() numbers': () => { + const { arrayInsert } = Fn; + const a = [1, 3, 5]; const b = [1, 3, 4, 5]; - assertEquals(Fn.arrayInsert(a, 2, 4), b); + assertEquals(arrayInsert(a, 2, 4), b); const c = [1, 2, 3, 4, 5]; - assertEquals(Fn.arrayInsert(b, 1, 2), c); + assertEquals(arrayInsert(b, 1, 2), c); }, 'noop fn': () => { assertExists(Fn.noop); @@ -259,54 +263,68 @@ testSuite({ assertEquals(Fn.maxAdd(25, 74, 101), 99); }, 'ord()': () => { + const { ord } = Fn; + // Invalid output - assertEquals(Fn.ord(''), 256); + assertEquals(ord(''), 256); // Valid output - assertEquals(Fn.ord('a'), 97); + assertEquals(ord('a'), 97); }, 'strChars() properly splits strings into unicode characters': () => { - assertEquals(Fn.strChars('😺😸😹'), ['😺', '😸', '😹']); + const { strChars } = Fn; + + assertEquals(strChars('😺😸😹'), ['😺', '😸', '😹']); }, 'ctrlKey()': () => { - const ctrl_a = Fn.ctrlKey('a'); - assertTrue(Fn.isControl(ctrl_a)); + const { ctrlKey, isControl } = Fn; + + const ctrl_a = ctrlKey('a'); + assertTrue(isControl(ctrl_a)); assertEquals(ctrl_a, String.fromCodePoint(0x01)); - const invalid = Fn.ctrlKey('😺'); - assertFalse(Fn.isControl(invalid)); + const invalid = ctrlKey('😺'); + assertFalse(isControl(invalid)); assertEquals(invalid, '😺'); }, 'isAscii()': () => { - assertTrue(Fn.isAscii('asjyverkjhsdf1928374')); - assertFalse(Fn.isAscii('😺acalskjsdf')); - assertFalse(Fn.isAscii('ab😺ac')); + const { isAscii } = Fn; + + assertTrue(isAscii('asjyverkjhsdf1928374')); + assertFalse(isAscii('😺acalskjsdf')); + assertFalse(isAscii('ab😺ac')); }, 'isControl()': () => { - assertFalse(Fn.isControl('abc')); - assertTrue(Fn.isControl(String.fromCodePoint(0x01))); - assertFalse(Fn.isControl('😺')); + const { isControl } = Fn; + + assertFalse(isControl('abc')); + assertTrue(isControl(String.fromCodePoint(0x01))); + assertFalse(isControl('😺')); }, 'strlen()': () => { + const { strlen } = Fn; + // Ascii length - assertEquals(Fn.strlen('abc'), 'abc'.length); + assertEquals(strlen('abc'), 'abc'.length); // Get number of visible unicode characters - assertEquals(Fn.strlen('😺😸😹'), 3); - assertNotEquals('😺😸😹'.length, Fn.strlen('😺😸😹')); + assertEquals(strlen('😺😸😹'), 3); + assertNotEquals('😺😸😹'.length, strlen('😺😸😹')); // Skin tone modifier + base character - assertEquals(Fn.strlen('🀰🏼'), 2); - assertNotEquals('🀰🏼'.length, Fn.strlen('🀰🏼')); + assertEquals(strlen('🀰🏼'), 2); + assertNotEquals('🀰🏼'.length, strlen('🀰🏼')); // This has 4 sub-characters, and 3 zero-width-joiners - assertEquals(Fn.strlen('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦'), 7); - assertNotEquals('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦'.length, Fn.strlen('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦')); + assertEquals(strlen('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦'), 7); + assertNotEquals('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦'.length, strlen('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦')); }, 'truncate()': () => { - assertEquals(Fn.truncate('😺😸😹', 1), '😺'); - assertEquals(Fn.truncate('😺😸😹', 5), '😺😸😹'); - assertEquals(Fn.truncate('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦', 5), 'πŸ‘¨β€πŸ‘©β€πŸ‘§'); + const { truncate } = Fn; + + assertEquals(truncate('😺😸😹', 1), '😺'); + assertEquals(truncate('😺😸😹', 5), '😺😸😹'); + assertEquals(truncate('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦', 5), 'πŸ‘¨β€πŸ‘©β€πŸ‘§'); }, }, 'readKey()': { diff --git a/src/common/row.ts b/src/common/row.ts index ab86b30..322ef9b 100644 --- a/src/common/row.ts +++ b/src/common/row.ts @@ -1,5 +1,5 @@ import { SCROLL_TAB_SIZE } from './config.ts'; -import * as Util from './fns.ts'; +import { arrayInsert, strChars } from './fns.ts'; /** * One row of text in the current document @@ -17,7 +17,7 @@ export class Row { render: string[] = []; private constructor(s: string | string[] = '') { - this.chars = Array.isArray(s) ? s : Util.strChars(s); + this.chars = Array.isArray(s) ? s : strChars(s); this.render = []; } @@ -46,16 +46,16 @@ export class Row { } public append(s: string): void { - this.chars = this.chars.concat(Util.strChars(s)); + this.chars = this.chars.concat(strChars(s)); this.updateRender(); } public insertChar(at: number, c: string): void { - const newSlice = Util.strChars(c); + const newSlice = strChars(c); if (at >= this.size) { this.chars = this.chars.concat(newSlice); } else { - this.chars = Util.arrayInsert(this.chars, at + 1, newSlice); + this.chars = arrayInsert(this.chars, at + 1, newSlice); } } @@ -162,7 +162,7 @@ export class Row { ' '.repeat(SCROLL_TAB_SIZE), ); - this.render = Util.strChars(newString); + this.render = strChars(newString); } }