csvparser.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2009 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. /**
  28. * Creates a CSV lines parser.
  29. */
  30. export class CsvParser {
  31. /**
  32. * Converts \x00 and \u0000 escape sequences in the given string.
  33. *
  34. * @param {string} input field.
  35. **/
  36. escapeField(string) {
  37. let nextPos = string.indexOf("\\");
  38. if (nextPos === -1) return string;
  39. let result = [string.substring(0, nextPos)];
  40. // Escape sequences of the form \x00 and \u0000;
  41. let pos = 0;
  42. while (nextPos !== -1) {
  43. const escapeIdentifier = string[nextPos + 1];
  44. pos = nextPos + 2;
  45. if (escapeIdentifier === 'n') {
  46. result.push('\n');
  47. nextPos = pos;
  48. } else if (escapeIdentifier === '\\') {
  49. result.push('\\');
  50. nextPos = pos;
  51. } else {
  52. if (escapeIdentifier === 'x') {
  53. // \x00 ascii range escapes consume 2 chars.
  54. nextPos = pos + 2;
  55. } else {
  56. // \u0000 unicode range escapes consume 4 chars.
  57. nextPos = pos + 4;
  58. }
  59. // Convert the selected escape sequence to a single character.
  60. const escapeChars = string.substring(pos, nextPos);
  61. if (escapeChars === '2C') {
  62. result.push(',');
  63. } else {
  64. result.push(String.fromCharCode(parseInt(escapeChars, 16)));
  65. }
  66. }
  67. // Continue looking for the next escape sequence.
  68. pos = nextPos;
  69. nextPos = string.indexOf("\\", pos);
  70. // If there are no more escape sequences consume the rest of the string.
  71. if (nextPos === -1) {
  72. result.push(string.substr(pos));
  73. break;
  74. } else if (pos !== nextPos) {
  75. result.push(string.substring(pos, nextPos));
  76. }
  77. }
  78. return result.join('');
  79. }
  80. /**
  81. * Parses a line of CSV-encoded values. Returns an array of fields.
  82. *
  83. * @param {string} line Input line.
  84. */
  85. parseLine(line) {
  86. let pos = 0;
  87. const endPos = line.length;
  88. const fields = [];
  89. if (endPos == 0) return fields;
  90. let nextPos = 0;
  91. while(nextPos !== -1) {
  92. nextPos = line.indexOf(',', pos);
  93. let field;
  94. if (nextPos === -1) {
  95. field = line.substr(pos);
  96. } else {
  97. field = line.substring(pos, nextPos);
  98. }
  99. fields.push(this.escapeField(field));
  100. pos = nextPos + 1;
  101. };
  102. return fields
  103. }
  104. }