Created
July 4, 2026 22:01
-
-
Save brandonhimpfen/aa2dfb326d7d083ac125ebb0ae2a2845 to your computer and use it in GitHub Desktop.
Profile CSV/JSONL column completeness while streaming in Node.js: count present, missing, empty, and completeness percentage with no dependencies.
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 | |
| /** | |
| * Profile CSV/JSONL column completeness while streaming. | |
| * | |
| * Usage: | |
| * node node-profile-column-completeness.js input.csv --format=csv | |
| * cat input.jsonl | node node-profile-column-completeness.js --format=jsonl --columns=id,name,email | |
| * | |
| * Notes: | |
| * - CSV mode uses the header row as columns unless --columns is provided. | |
| * - JSONL mode requires --columns. | |
| * - Supports nested JSONL paths like user.email. | |
| * - No dependencies. | |
| */ | |
| const fs = require("fs"); | |
| const readline = require("readline"); | |
| function usage() { | |
| console.error(`Usage: | |
| node node-profile-column-completeness.js input.csv --format=csv | |
| cat input.jsonl | node node-profile-column-completeness.js --format=jsonl --columns=id,name,user.email | |
| Options: | |
| --format=csv|jsonl | |
| --columns=col1,col2 | |
| --delimiter=, | |
| `); | |
| } | |
| function parseArgs(argv) { | |
| const args = { | |
| file: null, | |
| format: "jsonl", | |
| columns: null, | |
| delimiter: ",", | |
| }; | |
| for (const arg of argv.slice(2)) { | |
| if (!args.file && !arg.startsWith("--")) args.file = arg; | |
| else if (arg.startsWith("--format=")) args.format = arg.slice("--format=".length); | |
| else if (arg.startsWith("--columns=")) args.columns = arg.slice("--columns=".length).split(",").map(s => s.trim()).filter(Boolean); | |
| else if (arg.startsWith("--delimiter=")) args.delimiter = arg.slice("--delimiter=".length); | |
| else if (arg === "-h" || arg === "--help") args.help = true; | |
| else throw new Error(`Unknown argument: ${arg}`); | |
| } | |
| return args; | |
| } | |
| function parseCSVLine(line, delimiter = ",") { | |
| const out = []; | |
| let cur = ""; | |
| let inQuotes = false; | |
| for (let i = 0; i < line.length; i++) { | |
| const ch = line[i]; | |
| if (inQuotes) { | |
| if (ch === '"') { | |
| if (line[i + 1] === '"') { | |
| cur += '"'; | |
| i++; | |
| } else { | |
| inQuotes = false; | |
| } | |
| } else { | |
| cur += ch; | |
| } | |
| continue; | |
| } | |
| if (ch === '"') inQuotes = true; | |
| else if (ch === delimiter) { | |
| out.push(cur); | |
| cur = ""; | |
| } else { | |
| cur += ch; | |
| } | |
| } | |
| out.push(cur); | |
| return out; | |
| } | |
| function getByPath(obj, keyPath) { | |
| let cur = obj; | |
| for (const part of keyPath.split(".")) { | |
| if (cur == null || typeof cur !== "object" || !(part in cur)) { | |
| return undefined; | |
| } | |
| cur = cur[part]; | |
| } | |
| return cur; | |
| } | |
| function isEmpty(value) { | |
| return value === "" || value === null || value === undefined; | |
| } | |
| function createStats(columns) { | |
| return Object.fromEntries( | |
| columns.map(col => [ | |
| col, | |
| { | |
| present: 0, | |
| missing: 0, | |
| empty: 0, | |
| }, | |
| ]) | |
| ); | |
| } | |
| function updateStat(stats, col, value, exists) { | |
| if (!exists) { | |
| stats[col].missing += 1; | |
| return; | |
| } | |
| if (isEmpty(value)) { | |
| stats[col].empty += 1; | |
| } else { | |
| stats[col].present += 1; | |
| } | |
| } | |
| function printReport(stats, totalRows) { | |
| const report = Object.entries(stats).map(([column, s]) => { | |
| const complete = s.present; | |
| const pct = totalRows === 0 ? 0 : (complete / totalRows) * 100; | |
| return { | |
| column, | |
| total_rows: totalRows, | |
| present: s.present, | |
| empty: s.empty, | |
| missing: s.missing, | |
| completeness_pct: Number(pct.toFixed(2)), | |
| }; | |
| }); | |
| console.log(JSON.stringify(report, null, 2)); | |
| } | |
| async function runCSV(input, columnsArg, delimiter) { | |
| const rl = readline.createInterface({ input, crlfDelay: Infinity }); | |
| let headers = null; | |
| let columns = columnsArg; | |
| let stats = null; | |
| let totalRows = 0; | |
| let lineNum = 0; | |
| for await (const line of rl) { | |
| lineNum++; | |
| if (!line.trim()) continue; | |
| const fields = parseCSVLine(line, delimiter); | |
| if (!headers) { | |
| headers = fields; | |
| columns = columns || headers; | |
| stats = createStats(columns); | |
| continue; | |
| } | |
| totalRows++; | |
| for (const col of columns) { | |
| const idx = headers.indexOf(col); | |
| const exists = idx !== -1 && idx < fields.length; | |
| const value = exists ? fields[idx] : undefined; | |
| updateStat(stats, col, value, exists); | |
| } | |
| } | |
| printReport(stats || {}, totalRows); | |
| } | |
| async function runJSONL(input, columns) { | |
| if (!columns || columns.length === 0) { | |
| throw new Error("JSONL mode requires --columns=col1,col2"); | |
| } | |
| const rl = readline.createInterface({ input, crlfDelay: Infinity }); | |
| const stats = createStats(columns); | |
| let totalRows = 0; | |
| let lineNum = 0; | |
| for await (const line of rl) { | |
| lineNum++; | |
| if (!line.trim()) continue; | |
| let record; | |
| try { | |
| record = JSON.parse(line); | |
| } catch (err) { | |
| throw new Error(`Invalid JSON on line ${lineNum}: ${err.message}`); | |
| } | |
| totalRows++; | |
| for (const col of columns) { | |
| const value = getByPath(record, col); | |
| const exists = value !== undefined; | |
| updateStat(stats, col, value, exists); | |
| } | |
| } | |
| printReport(stats, totalRows); | |
| } | |
| async function main() { | |
| const args = parseArgs(process.argv); | |
| if (args.help || !["csv", "jsonl"].includes(args.format)) { | |
| usage(); | |
| process.exit(args.help ? 0 : 2); | |
| } | |
| const input = args.file | |
| ? fs.createReadStream(args.file, { encoding: "utf8" }) | |
| : process.stdin; | |
| if (args.format === "csv") { | |
| await runCSV(input, args.columns, args.delimiter); | |
| } else { | |
| await runJSONL(input, args.columns); | |
| } | |
| } | |
| main().catch((err) => { | |
| console.error("ERROR:", err && err.stack ? err.stack : err); | |
| process.exit(1); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment