vis.js is a dynamic, browser-based visualization library
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.

120 lines
3.6 KiB

10 years ago
  1. /**
  2. * Convert data in CSV (comma separated value) format to a javascript array.
  3. *
  4. * Values are separated by a comma, or by a custom one character delimeter.
  5. * Rows are separated by a new-line character.
  6. *
  7. * Leading and trailing spaces and tabs are ignored.
  8. * Values may optionally be enclosed by double quotes.
  9. * Values containing a special character (comma's, double-quotes, or new-lines)
  10. * must be enclosed by double-quotes.
  11. * Embedded double-quotes must be represented by a pair of consecutive
  12. * double-quotes.
  13. *
  14. * Example usage:
  15. * var csv = '"x", "y", "z"\n12.3, 2.3, 8.7\n4.5, 1.2, -5.6\n';
  16. * var array = csv2array(csv);
  17. *
  18. * Author: Jos de Jong, 2010
  19. *
  20. * @param {string} data The data in CSV format.
  21. * @param {string} delimeter [optional] a custom delimeter. Comma ',' by default
  22. * The Delimeter must be a single character.
  23. * @return {Array} array A two dimensional array containing the data
  24. * @throw {String} error The method throws an error when there is an
  25. * error in the provided data.
  26. */
  27. function csv2array(data, delimeter) {
  28. // Retrieve the delimeter
  29. if (delimeter == undefined)
  30. delimeter = ',';
  31. if (delimeter && delimeter.length > 1)
  32. delimeter = ',';
  33. // initialize variables
  34. var newline = '\n';
  35. var eof = '';
  36. var i = 0;
  37. var c = data.charAt(i);
  38. var row = 0;
  39. var col = 0;
  40. var array = new Array();
  41. while (c != eof) {
  42. // skip whitespaces
  43. while (c == ' ' || c == '\t' || c == '\r') {
  44. c = data.charAt(++i); // read next char
  45. }
  46. // get value
  47. var value = "";
  48. if (c == '\"') {
  49. // value enclosed by double-quotes
  50. c = data.charAt(++i);
  51. do {
  52. if (c != '\"') {
  53. // read a regular character and go to the next character
  54. value += c;
  55. c = data.charAt(++i);
  56. }
  57. if (c == '\"') {
  58. // check for escaped double-quote
  59. var cnext = data.charAt(i+1);
  60. if (cnext == '\"') {
  61. // this is an escaped double-quote.
  62. // Add a double-quote to the value, and move two characters ahead.
  63. value += '\"';
  64. i += 2;
  65. c = data.charAt(i);
  66. }
  67. }
  68. }
  69. while (c != eof && c != '\"');
  70. if (c == eof) {
  71. throw "Unexpected end of data, double-quote expected";
  72. }
  73. c = data.charAt(++i);
  74. }
  75. else {
  76. // value without quotes
  77. while (c != eof && c != delimeter && c!= newline && c != ' ' && c != '\t' && c != '\r') {
  78. value += c;
  79. c = data.charAt(++i);
  80. }
  81. }
  82. // add the value to the array
  83. if (array.length <= row)
  84. array.push(new Array());
  85. array[row].push(value);
  86. // skip whitespaces
  87. while (c == ' ' || c == '\t' || c == '\r') {
  88. c = data.charAt(++i);
  89. }
  90. // go to the next row or column
  91. if (c == delimeter) {
  92. // to the next column
  93. col++;
  94. }
  95. else if (c == newline) {
  96. // to the next row
  97. col = 0;
  98. row++;
  99. }
  100. else if (c != eof) {
  101. // unexpected character
  102. throw "Delimiter expected after character " + i;
  103. }
  104. // go to the next character
  105. c = data.charAt(++i);
  106. }
  107. return array;
  108. }