Last active
July 23, 2026 18:20
-
-
Save examosa/98933f5a3032b09c995f57a9440902fd to your computer and use it in GitHub Desktop.
JS Snippets
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 { spawn } from "node:child_process"; | |
| import { on, once } from "node:events"; | |
| async function spawnAsync(...args) { | |
| const subprocess = spawn(...args); | |
| /** @see https://github.com/sindresorhus/nano-spawn/blob/cc231e2c7/source/spawn.js#L24-L26 */ | |
| subprocess.once("error", () => {}); | |
| const until = ["spawn", "close"].reduce((out, event) => { | |
| out[event] = once(subprocess, event); | |
| return out; | |
| }, Object.create(null)); | |
| await until.spawn; | |
| const result = ["stdout", "stderr"].reduce((out, stdio) => { | |
| out[stdio] = ""; | |
| const stream = subprocess[stdio]; | |
| if (stream) { | |
| stream.setEncoding("utf8"); | |
| const task = Array.fromAsync( | |
| on(stream, "data", { close: ["close"] }), | |
| ).then((chunks) => { | |
| const output = chunks.join(""); | |
| out[stdio] = stdio === "stdout" ? output.trimEnd("\n") : output; | |
| }); | |
| out[stdio] = task; | |
| } | |
| return out; | |
| }, Object.create(null)); | |
| await Promise.all(Object.values(result)); | |
| await until.close; | |
| return result; | |
| } | |
| export default spawnAsync; |
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
| #!/usr/bin/env node | |
| import { basename } from "node:path"; | |
| import { writeFile } from "node:fs/promises"; | |
| const isStart = /^\p{ID_Start}$/u; | |
| const isPart = /^\p{ID_Continue}$/u; | |
| const lines = ["codepoint\tcharacter\ttype"]; | |
| for (let cp = 0; cp <= 0x10ffff; cp++) { | |
| if (cp >= 0xd800 && cp <= 0xdfff) continue; | |
| const char = String.fromCodePoint(cp); | |
| const start = isStart.test(char); | |
| const part = isPart.test(char); | |
| if (start || part) { | |
| const hex = `U+${cp.toString(16).toUpperCase().padStart(4, "0")}`; | |
| const type = start ? "Start" : "Continue"; | |
| lines.push(`${hex}\t${char}\t${type}`); | |
| } | |
| } | |
| const output = new URL("unicode_identifiers.tsv", import.meta.url); | |
| await writeFile(output, lines.join("\n"), "utf8"); | |
| console.log( | |
| `Done. ${lines.length - 1} characters written to ${basename(output.toString())}`, | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment