source: node_modules/d3-dsv/dist/d3-dsv.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 7.0 KB
Line 
1// https://d3js.org/d3-dsv/ v3.0.1 Copyright 2013-2021 Mike Bostock
2(function (global, factory) {
3typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4typeof define === 'function' && define.amd ? define(['exports'], factory) :
5(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
6}(this, (function (exports) { 'use strict';
7
8var EOL = {},
9 EOF = {},
10 QUOTE = 34,
11 NEWLINE = 10,
12 RETURN = 13;
13
14function objectConverter(columns) {
15 return new Function("d", "return {" + columns.map(function(name, i) {
16 return JSON.stringify(name) + ": d[" + i + "] || \"\"";
17 }).join(",") + "}");
18}
19
20function customConverter(columns, f) {
21 var object = objectConverter(columns);
22 return function(row, i) {
23 return f(object(row), i, columns);
24 };
25}
26
27// Compute unique columns in order of discovery.
28function inferColumns(rows) {
29 var columnSet = Object.create(null),
30 columns = [];
31
32 rows.forEach(function(row) {
33 for (var column in row) {
34 if (!(column in columnSet)) {
35 columns.push(columnSet[column] = column);
36 }
37 }
38 });
39
40 return columns;
41}
42
43function pad(value, width) {
44 var s = value + "", length = s.length;
45 return length < width ? new Array(width - length + 1).join(0) + s : s;
46}
47
48function formatYear(year) {
49 return year < 0 ? "-" + pad(-year, 6)
50 : year > 9999 ? "+" + pad(year, 6)
51 : pad(year, 4);
52}
53
54function formatDate(date) {
55 var hours = date.getUTCHours(),
56 minutes = date.getUTCMinutes(),
57 seconds = date.getUTCSeconds(),
58 milliseconds = date.getUTCMilliseconds();
59 return isNaN(date) ? "Invalid Date"
60 : formatYear(date.getUTCFullYear()) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2)
61 + (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z"
62 : seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z"
63 : minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z"
64 : "");
65}
66
67function dsv(delimiter) {
68 var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
69 DELIMITER = delimiter.charCodeAt(0);
70
71 function parse(text, f) {
72 var convert, columns, rows = parseRows(text, function(row, i) {
73 if (convert) return convert(row, i - 1);
74 columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
75 });
76 rows.columns = columns || [];
77 return rows;
78 }
79
80 function parseRows(text, f) {
81 var rows = [], // output rows
82 N = text.length,
83 I = 0, // current character index
84 n = 0, // current line number
85 t, // current token
86 eof = N <= 0, // current token followed by EOF?
87 eol = false; // current token followed by EOL?
88
89 // Strip the trailing newline.
90 if (text.charCodeAt(N - 1) === NEWLINE) --N;
91 if (text.charCodeAt(N - 1) === RETURN) --N;
92
93 function token() {
94 if (eof) return EOF;
95 if (eol) return eol = false, EOL;
96
97 // Unescape quotes.
98 var i, j = I, c;
99 if (text.charCodeAt(j) === QUOTE) {
100 while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
101 if ((i = I) >= N) eof = true;
102 else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
103 else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
104 return text.slice(j + 1, i - 1).replace(/""/g, "\"");
105 }
106
107 // Find next delimiter or newline.
108 while (I < N) {
109 if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
110 else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
111 else if (c !== DELIMITER) continue;
112 return text.slice(j, i);
113 }
114
115 // Return last token before EOF.
116 return eof = true, text.slice(j, N);
117 }
118
119 while ((t = token()) !== EOF) {
120 var row = [];
121 while (t !== EOL && t !== EOF) row.push(t), t = token();
122 if (f && (row = f(row, n++)) == null) continue;
123 rows.push(row);
124 }
125
126 return rows;
127 }
128
129 function preformatBody(rows, columns) {
130 return rows.map(function(row) {
131 return columns.map(function(column) {
132 return formatValue(row[column]);
133 }).join(delimiter);
134 });
135 }
136
137 function format(rows, columns) {
138 if (columns == null) columns = inferColumns(rows);
139 return [columns.map(formatValue).join(delimiter)].concat(preformatBody(rows, columns)).join("\n");
140 }
141
142 function formatBody(rows, columns) {
143 if (columns == null) columns = inferColumns(rows);
144 return preformatBody(rows, columns).join("\n");
145 }
146
147 function formatRows(rows) {
148 return rows.map(formatRow).join("\n");
149 }
150
151 function formatRow(row) {
152 return row.map(formatValue).join(delimiter);
153 }
154
155 function formatValue(value) {
156 return value == null ? ""
157 : value instanceof Date ? formatDate(value)
158 : reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\""
159 : value;
160 }
161
162 return {
163 parse: parse,
164 parseRows: parseRows,
165 format: format,
166 formatBody: formatBody,
167 formatRows: formatRows,
168 formatRow: formatRow,
169 formatValue: formatValue
170 };
171}
172
173var csv = dsv(",");
174
175var csvParse = csv.parse;
176var csvParseRows = csv.parseRows;
177var csvFormat = csv.format;
178var csvFormatBody = csv.formatBody;
179var csvFormatRows = csv.formatRows;
180var csvFormatRow = csv.formatRow;
181var csvFormatValue = csv.formatValue;
182
183var tsv = dsv("\t");
184
185var tsvParse = tsv.parse;
186var tsvParseRows = tsv.parseRows;
187var tsvFormat = tsv.format;
188var tsvFormatBody = tsv.formatBody;
189var tsvFormatRows = tsv.formatRows;
190var tsvFormatRow = tsv.formatRow;
191var tsvFormatValue = tsv.formatValue;
192
193function autoType(object) {
194 for (var key in object) {
195 var value = object[key].trim(), number, m;
196 if (!value) value = null;
197 else if (value === "true") value = true;
198 else if (value === "false") value = false;
199 else if (value === "NaN") value = NaN;
200 else if (!isNaN(number = +value)) value = number;
201 else if (m = value.match(/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/)) {
202 if (fixtz && !!m[4] && !m[7]) value = value.replace(/-/g, "/").replace(/T/, " ");
203 value = new Date(value);
204 }
205 else continue;
206 object[key] = value;
207 }
208 return object;
209}
210
211// https://github.com/d3/d3-dsv/issues/45
212const fixtz = new Date("2019-01-01T00:00").getHours() || new Date("2019-07-01T00:00").getHours();
213
214exports.autoType = autoType;
215exports.csvFormat = csvFormat;
216exports.csvFormatBody = csvFormatBody;
217exports.csvFormatRow = csvFormatRow;
218exports.csvFormatRows = csvFormatRows;
219exports.csvFormatValue = csvFormatValue;
220exports.csvParse = csvParse;
221exports.csvParseRows = csvParseRows;
222exports.dsvFormat = dsv;
223exports.tsvFormat = tsvFormat;
224exports.tsvFormatBody = tsvFormatBody;
225exports.tsvFormatRow = tsvFormatRow;
226exports.tsvFormatRows = tsvFormatRows;
227exports.tsvFormatValue = tsvFormatValue;
228exports.tsvParse = tsvParse;
229exports.tsvParseRows = tsvParseRows;
230
231Object.defineProperty(exports, '__esModule', { value: true });
232
233})));
Note: See TracBrowser for help on using the repository browser.