Created
August 10, 2024 11:44
-
-
Save WomB0ComB0/b785e981f74203822f64c2642d1c3f57 to your computer and use it in GitHub Desktop.
Generate kv pairs for O(1) lookup time from text files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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