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.

162 lines
4.4 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // https://d3js.org/d3-dsv/ v1.0.10 Copyright 2018 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. (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 dsv(delimiter) {
  37. var reFormat = new RegExp("[\"" + delimiter + "\n\r]"),
  38. DELIMITER = delimiter.charCodeAt(0);
  39. function parse(text, f) {
  40. var convert, columns, rows = parseRows(text, function(row, i) {
  41. if (convert) return convert(row, i - 1);
  42. columns = row, convert = f ? customConverter(row, f) : objectConverter(row);
  43. });
  44. rows.columns = columns || [];
  45. return rows;
  46. }
  47. function parseRows(text, f) {
  48. var rows = [], // output rows
  49. N = text.length,
  50. I = 0, // current character index
  51. n = 0, // current line number
  52. t, // current token
  53. eof = N <= 0, // current token followed by EOF?
  54. eol = false; // current token followed by EOL?
  55. // Strip the trailing newline.
  56. if (text.charCodeAt(N - 1) === NEWLINE) --N;
  57. if (text.charCodeAt(N - 1) === RETURN) --N;
  58. function token() {
  59. if (eof) return EOF;
  60. if (eol) return eol = false, EOL;
  61. // Unescape quotes.
  62. var i, j = I, c;
  63. if (text.charCodeAt(j) === QUOTE) {
  64. while (I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE);
  65. if ((i = I) >= N) eof = true;
  66. else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true;
  67. else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
  68. return text.slice(j + 1, i - 1).replace(/""/g, "\"");
  69. }
  70. // Find next delimiter or newline.
  71. while (I < N) {
  72. if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true;
  73. else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; }
  74. else if (c !== DELIMITER) continue;
  75. return text.slice(j, i);
  76. }
  77. // Return last token before EOF.
  78. return eof = true, text.slice(j, N);
  79. }
  80. while ((t = token()) !== EOF) {
  81. var row = [];
  82. while (t !== EOL && t !== EOF) row.push(t), t = token();
  83. if (f && (row = f(row, n++)) == null) continue;
  84. rows.push(row);
  85. }
  86. return rows;
  87. }
  88. function format(rows, columns) {
  89. if (columns == null) columns = inferColumns(rows);
  90. return [columns.map(formatValue).join(delimiter)].concat(rows.map(function(row) {
  91. return columns.map(function(column) {
  92. return formatValue(row[column]);
  93. }).join(delimiter);
  94. })).join("\n");
  95. }
  96. function formatRows(rows) {
  97. return rows.map(formatRow).join("\n");
  98. }
  99. function formatRow(row) {
  100. return row.map(formatValue).join(delimiter);
  101. }
  102. function formatValue(text) {
  103. return text == null ? ""
  104. : reFormat.test(text += "") ? "\"" + text.replace(/"/g, "\"\"") + "\""
  105. : text;
  106. }
  107. return {
  108. parse: parse,
  109. parseRows: parseRows,
  110. format: format,
  111. formatRows: formatRows
  112. };
  113. }
  114. var csv = dsv(",");
  115. var csvParse = csv.parse;
  116. var csvParseRows = csv.parseRows;
  117. var csvFormat = csv.format;
  118. var csvFormatRows = csv.formatRows;
  119. var tsv = dsv("\t");
  120. var tsvParse = tsv.parse;
  121. var tsvParseRows = tsv.parseRows;
  122. var tsvFormat = tsv.format;
  123. var tsvFormatRows = tsv.formatRows;
  124. exports.dsvFormat = dsv;
  125. exports.csvParse = csvParse;
  126. exports.csvParseRows = csvParseRows;
  127. exports.csvFormat = csvFormat;
  128. exports.csvFormatRows = csvFormatRows;
  129. exports.tsvParse = tsvParse;
  130. exports.tsvParseRows = tsvParseRows;
  131. exports.tsvFormat = tsvFormat;
  132. exports.tsvFormatRows = tsvFormatRows;
  133. Object.defineProperty(exports, '__esModule', { value: true });
  134. })));