Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d3-dsv.js 7.0KB

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