UNPKG

csv-transpose

Version:

Transpose (rotate) data from rows to columns or vice verse in csv

281 lines (278 loc) 9.06 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __reExport = (target, module2, copyDefault, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); } return target; }; var __toESM = (module2, isNodeMode) => { return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); }; var __toCommonJS = /* @__PURE__ */ ((cache) => { return (module2, temp) => { return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); }; })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); // node_modules/.pnpm/@beenotung+tslib@18.0.0/node_modules/@beenotung/tslib/csv.js var require_csv = __commonJS({ "node_modules/.pnpm/@beenotung+tslib@18.0.0/node_modules/@beenotung/tslib/csv.js"(exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.csv_to_table_text = exports.json_to_csv = exports.json_to_csv_titles = exports.csv_to_json = exports.regular_csv = exports.to_csv = exports.from_csv = exports.COMMA = exports.DOUBLE_QUOTE = exports.SINGLE_QUOTE = void 0; exports.SINGLE_QUOTE = "'"; exports.DOUBLE_QUOTE = '"'; exports.COMMA = ","; function from_csv2(s, separator = exports.COMMA, delimiter = exports.DOUBLE_QUOTE) { s = s.trim(); const rows = []; let cols = []; let col = ""; let i = 0; const n = s.length; for (; i < n; ) { const c = s[i]; i++; switch (c) { case separator: cols.push(col); col = ""; break; case "\r": if (s[i] !== "\n") { col += c; } break; case "\n": cols.push(col); col = ""; rows.push(cols); cols = []; break; default: if (c === delimiter) { for (; ; ) { const c2 = s[i]; i++; if (c2 === delimiter) { if (s[i] === delimiter) { col += delimiter; i++; } else { break; } } else { col += c2; } } break; } col += c; } } if (s[n - 1] !== "\n") { if (col.length > 0) { cols.push(col); } if (cols.length > 0) { rows.push(cols); } } return rows; } exports.from_csv = from_csv2; function to_csv2(rows, separator = exports.COMMA, delimiter = exports.DOUBLE_QUOTE) { const res = []; for (const cols of rows) { let first = true; for (const col of cols) { if (first) { first = false; } else { res.push(separator); } if (col && col.indexOf(delimiter) === -1 && col.indexOf(separator) === -1) { res.push(col); } else { res.push(delimiter); for (const c of col || "") { if (c === delimiter) { res.push(delimiter, delimiter); } else { res.push(c); } } res.push(delimiter); } } res.push("\r\n"); } return res.join(""); } exports.to_csv = to_csv2; function regular_csv(rows) { let maxCol = 0; rows.forEach((cols) => maxCol = Math.max(maxCol, cols.length)); rows.forEach((cols) => { for (let i = cols.length; i < maxCol; i++) { cols.push(); } }); } exports.regular_csv = regular_csv; function csv_to_json(rows, titles) { if (!titles) { rows = rows.slice(); titles = rows.shift() || []; } const n = titles.length; return rows.map((cols) => { const res = {}; for (let i = 0; i < n; i++) { res[titles[i]] = cols[i]; } return res; }); } exports.csv_to_json = csv_to_json; function json_to_csv_titles(xs) { const titles = []; const titleCache = {}; for (const x of xs) { for (const title of Object.keys(x)) { if (!titleCache[title]) { titleCache[title] = true; titles.push(title); } } } return titles; } exports.json_to_csv_titles = json_to_csv_titles; function json_to_csv(xs, titles = json_to_csv_titles(xs)) { const rows = [titles]; const n = titles.length; for (const x of xs) { const cols = new Array(n).fill(""); rows.push(cols); for (let i = 0; i < n; i++) { let col = x[titles[i]]; if (col === null || col === void 0) { col = ""; } else { col = String(col); } cols[i] = col; } } return rows; } exports.json_to_csv = json_to_csv; function to_width(s, len, repeatChar) { const diff = len - s.length; if (diff > 0) { s += repeatChar.repeat(diff / repeatChar.length); s += repeatChar.slice(0, len - s.length); } return s; } function csv_to_table_text(rows, options) { if (rows.length === 0) { return ""; } let col_separator = " "; let header_line_char = "-"; let show_total = false; let line_edge_start = ""; let line_edge_end = ""; let min_col_width = 0; if (options) { if (options.col_separator) { col_separator = options.col_separator; } if (options.header_line_char) { header_line_char = options.header_line_char; } if (options.show_total) { show_total = options.show_total; } if (options.min_col_width) { min_col_width = options.min_col_width; } if (options.markdown) { col_separator = " | "; header_line_char = "-"; line_edge_start = "| "; line_edge_end = " |"; min_col_width = Math.max(min_col_width, 3); } } const n = rows[0].length; const lengths = new Array(n).fill(0); for (const cols of rows) { for (let i = 0; i < n; i++) { const col = cols[i]; if (col) { lengths[i] = Math.max(lengths[i], col.length, min_col_width); } } } const titles = rows[0]; const contents = rows.slice(1); let acc = ""; acc += line_edge_start; acc += titles.map((s, i) => to_width(s, lengths[i], " ")).join(col_separator); acc += line_edge_end; acc += "\n"; acc += line_edge_start; acc += titles.map((s, i) => to_width("", lengths[i], header_line_char)).join(col_separator); acc += line_edge_end; acc += "\n"; acc += contents.map((cols) => line_edge_start + cols.map((s, i) => to_width(s, lengths[i], " ")).join(col_separator) + line_edge_end).join("\n"); if (show_total) { acc += "\n"; acc += "\n"; acc += "total: " + contents.length; } return acc; } exports.csv_to_table_text = csv_to_table_text; } }); // lib/core.ts var core_exports = {}; __export(core_exports, { transpose: () => transpose }); var import_csv = __toESM(require_csv()); function transpose(input, separator) { let rows = (0, import_csv.from_csv)(input, separator); let maxCol = rows.reduce((acc, cols) => Math.max(acc, cols.length), 0); let results = []; for (let i = 0; i < maxCol; i++) { let result = []; rows.forEach((cols) => result.push(cols[i])); results.push(result); } let output = (0, import_csv.to_csv)(results, separator); return output; } module.exports = __toCommonJS(core_exports); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { transpose });