Mess with function imports
timw4mail/scroll/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2024-01-11 11:23:28 -05:00
parent f19fc1d2e0
commit b3177cbd48
2 changed files with 52 additions and 34 deletions

View File

@ -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()': {

View File

@ -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);
}
}