Skip to content

Instantly share code, notes, and snippets.

@WomB0ComB0
Created August 10, 2024 11:44
Show Gist options
  • Save WomB0ComB0/b785e981f74203822f64c2642d1c3f57 to your computer and use it in GitHub Desktop.
Save WomB0ComB0/b785e981f74203822f64c2642d1c3f57 to your computer and use it in GitHub Desktop.
Generate kv pairs for O(1) lookup time from text files
import { fileURLToPath } from "node:url"
import { readFileSync, writeFileSync } from "node:fs"
import { dirname } from "node:path"
(() => {
const __dirname = dirname(fileURLToPath(import.meta.url))
console.log(`${__dirname}<path><file_name>.<extension>`)
const robots = readFileSync(`${__dirname}<path><file_name>.<extension>`, "utf-8")
const kv: Map<string, string> = new Map<string, string>()
robots.split(/\r?\n/).forEach((line) => {
const trimmedLine = line.trim()
if (trimmedLine) {
kv.set(trimmedLine, trimmedLine)
}
})
writeFileSync(`${__dirname}<path><file_name>.ts`, `export const robots: Readonly<Record<string, string>> = ${JSON.stringify(Object.fromEntries(kv), null, 2)}`)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment