diff --git a/src/common/constants.ts b/src/common/constants.ts index e4d94da..b480d6c 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -1,3 +1,5 @@ +import { invert } from './helpers.js' + export const DANAE_VERSION = 1.440_000_057_220_459 // 1.44f export const LITTLE_ENDIAN = true @@ -13,7 +15,7 @@ export const MAP_DEPTH_IN_CELLS = 160 // source: https://mathiasbynens.be/notes/javascript-escapes // prettier-ignore -export const CHARS = [ +export const CHARS: readonly string[] = [ '\0', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\b', '\t', '\n', '\v', '\f', '\r', '\u000E', '\u000F', '\0x10', '\0x11', '\0x12', '\0x13', '\0x14', '\0x15', '\0x16', '\0x17', '\0x18', '\0x19', '\0x1a', '\0x1b', '\0x1c', '\0x1d', '\0x1e', '\0x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', @@ -32,11 +34,8 @@ export const CHARS = [ 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ' ] -export const CODES: Record = {} -CHARS.forEach((value, idx) => { - CODES[value] = idx -}) +export const CODES = /* @__PURE__ */ invert(CHARS as string[]) -export const BYTE_OF_AN_UNKNOWN_CHAR = CODES[' '] +export const BYTE_OF_AN_UNKNOWN_CHAR = /* @__PURE__ */ CHARS.indexOf(' ') export const CHAR_OF_AN_UNKNOWN_BYTE = ' ' diff --git a/src/common/helpers.ts b/src/common/helpers.ts index 8b5fa53..a27e558 100644 --- a/src/common/helpers.ts +++ b/src/common/helpers.ts @@ -31,6 +31,7 @@ export function times(fn: (index: number) => T, repetitions: number): T[] { export function repeat(value: T, repetitions: number): T[] { const values = [] + for (let i = 0; i < repetitions; i++) { values.push(value) } @@ -38,6 +39,16 @@ export function repeat(value: T, repetitions: number): T[] { return values } +export function invert(values: string[]): Record { + const obj: Record = {} + + values.forEach((value, index) => { + obj[value] = index + }) + + return obj +} + export function decodeText(bytes: number[]): string { const chars = bytes.map((byte) => { return CHARS[byte] ?? CHAR_OF_AN_UNKNOWN_BYTE