Skip to content

Commit

Permalink
refactor(constants): annotate constants with pure flag
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Dec 19, 2024
1 parent 45cc496 commit 29ccb98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/',
Expand All @@ -32,11 +34,8 @@ export const CHARS = [
'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ'
]

export const CODES: Record<string, number> = {}
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 = ' '
11 changes: 11 additions & 0 deletions src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ export function times<T>(fn: (index: number) => T, repetitions: number): T[] {

export function repeat<T>(value: T, repetitions: number): T[] {
const values = []

for (let i = 0; i < repetitions; i++) {
values.push(value)
}

return values
}

export function invert(values: string[]): Record<string, number> {
const obj: Record<string, number> = {}

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
Expand Down

0 comments on commit 29ccb98

Please sign in to comment.