| 1 | var EOL = {},
|
|---|
| 2 | EOF = {},
|
|---|
| 3 | QUOTE = 34,
|
|---|
| 4 | NEWLINE = 10,
|
|---|
| 5 | RETURN = 13;
|
|---|
| 6 |
|
|---|
| 7 | function objectConverter(columns) {
|
|---|
| 8 | return new Function("d", "return {" + columns.map(function(name, i) {
|
|---|
| 9 | return JSON.stringify(name) + ": d[" + i + "] || \"\"";
|
|---|
| 10 | }).join(",") + "}");
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function customConverter(columns, f) {
|
|---|
| 14 | var object = objectConverter(columns);
|
|---|
| 15 | return function(row, i) {
|
|---|
| 16 | return f(object(row), i, columns);
|
|---|
| 17 | };
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | // Compute unique columns in order of discovery.
|
|---|
| 21 | function inferColumns(rows) {
|
|---|
| 22 | var columnSet = Object.create(null),
|
|---|
| 23 | columns = [];
|
|---|
| 24 |
|
|---|
| 25 | rows.forEach(function(row) {
|
|---|
| 26 | for (var column in row) {
|
|---|
| 27 | if (!(column in columnSet)) {
|
|---|
| 28 | columns.push(columnSet[column] = column);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | return columns;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | function pad(value, width) {
|
|---|
| 37 | var s = value + "", length = s.length;
|
|---|
| 38 | return length < width ? new Array(width - length + 1).join(0) + s : s;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | function formatYear(year) {
|
|---|
| 42 | return year < 0 ? "-" + pad(-year, 6)
|
|---|
| 43 | : year > 9999 ? "+" + pad(year, 6)
|
|---|
| 44 | : pad(year, 4);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | function formatDate(date) {
|
|---|
| 48 | var hours = date.getUTCHours(),
|
|---|
| 49 | minutes = date.getUTCMinutes(),
|
|---|
| 50 | seconds = date.getUTCSeconds(),
|
|---|
| 51 | milliseconds = date.getUTCMilliseconds();
|
|---|
| 52 | return isNaN(date) ? "Invalid Date"
|
|---|
| 53 | : formatYear(date.getUTCFullYear(), 4) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
|
|---|
| 54 | + (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z"
|
|---|
| 55 | : seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z"
|
|---|
| 56 | : minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z"
|
|---|
| 57 | : "");
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | export default function(delimiter) {
|
|---|
| 61 | var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
|
|---|
| 62 | DELIMITER = delimiter.charCodeAt(0);
|
|---|
| 63 |
|
|---|
| 64 | function parse(text, f) {
|
|---|
| 65 | var convert, columns, rows = parseRows(text, function(row, i) {
|
|---|
| 66 | if (convert) return convert(row, i - 1);
|
|---|
| 67 | columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
|
|---|
| 68 | });
|
|---|
| 69 | rows.columns = columns || [];
|
|---|
| 70 | return rows;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | function parseRows(text, f) {
|
|---|
| 74 | var rows = [], // output rows
|
|---|
| 75 | N = text.length,
|
|---|
| 76 | I = 0, // current character index
|
|---|
| 77 | n = 0, // current line number
|
|---|
| 78 | t, // current token
|
|---|
| 79 | eof = N <= 0, // current token followed by EOF?
|
|---|
| 80 | eol = false; // current token followed by EOL?
|
|---|
| 81 |
|
|---|
| 82 | // Strip the trailing newline.
|
|---|
| 83 | if (text.charCodeAt(N - 1) === NEWLINE) --N;
|
|---|
| 84 | if (text.charCodeAt(N - 1) === RETURN) --N;
|
|---|
| 85 |
|
|---|
| 86 | function token() {
|
|---|
| 87 | if (eof) return EOF;
|
|---|
| 88 | if (eol) return eol = false, EOL;
|
|---|
| 89 |
|
|---|
| 90 | // Unescape quotes.
|
|---|
| 91 | var i, j = I, c;
|
|---|
| 92 | if (text.charCodeAt(j) === QUOTE) {
|
|---|
| 93 | while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
|
|---|
| 94 | if ((i = I) >= N) eof = true;
|
|---|
| 95 | else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
|
|---|
| 96 | else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
|
|---|
| 97 | return text.slice(j + 1, i - 1).replace(/""/g, "\"");
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | // Find next delimiter or newline.
|
|---|
| 101 | while (I < N) {
|
|---|
| 102 | if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
|
|---|
| 103 | else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
|
|---|
| 104 | else if (c !== DELIMITER) continue;
|
|---|
| 105 | return text.slice(j, i);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | // Return last token before EOF.
|
|---|
| 109 | return eof = true, text.slice(j, N);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | while ((t = token()) !== EOF) {
|
|---|
| 113 | var row = [];
|
|---|
| 114 | while (t !== EOL && t !== EOF) row.push(t), t = token();
|
|---|
| 115 | if (f && (row = f(row, n++)) == null) continue;
|
|---|
| 116 | rows.push(row);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | return rows;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | function preformatBody(rows, columns) {
|
|---|
| 123 | return rows.map(function(row) {
|
|---|
| 124 | return columns.map(function(column) {
|
|---|
| 125 | return formatValue(row[column]);
|
|---|
| 126 | }).join(delimiter);
|
|---|
| 127 | });
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | function format(rows, columns) {
|
|---|
| 131 | if (columns == null) columns = inferColumns(rows);
|
|---|
| 132 | return [columns.map(formatValue).join(delimiter)].concat(preformatBody(rows, columns)).join("\n");
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | function formatBody(rows, columns) {
|
|---|
| 136 | if (columns == null) columns = inferColumns(rows);
|
|---|
| 137 | return preformatBody(rows, columns).join("\n");
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | function formatRows(rows) {
|
|---|
| 141 | return rows.map(formatRow).join("\n");
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | function formatRow(row) {
|
|---|
| 145 | return row.map(formatValue).join(delimiter);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | function formatValue(value) {
|
|---|
| 149 | return value == null ? ""
|
|---|
| 150 | : value instanceof Date ? formatDate(value)
|
|---|
| 151 | : reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\""
|
|---|
| 152 | : value;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | return {
|
|---|
| 156 | parse: parse,
|
|---|
| 157 | parseRows: parseRows,
|
|---|
| 158 | format: format,
|
|---|
| 159 | formatBody: formatBody,
|
|---|
| 160 | formatRows: formatRows,
|
|---|
| 161 | formatRow: formatRow,
|
|---|
| 162 | formatValue: formatValue
|
|---|
| 163 | };
|
|---|
| 164 | }
|
|---|