1(function () { 2 /** 3 * 4 * Node: 5 * const csv2json = require('./csv2json.js'); 6 * csv2json(csv, options) 7 * 8 * Browser: 9 * CSVJSON.csv2json(csv, options) 10 * 11 * Converts CSV to JSON. Returns an object. Use JSON.stringify to convert to a string. 12 * 13 * Available options: 14 * - separator: Optional. Character which acts as separator. If omitted, 15 * will attempt to detect comma (,), semi-colon (;) or tab (\t). 16 * - parseNumbers: Optional. Will attempt to convert a value to a number, if possible. 17 * - parseJSON: Optional. Will attempt to convert a value to a valid JSON value if possible. 18 * Detects numbers, null, false, true, [] and {}. 19 * - transpose: Optional. Will pivot the table. Default is false. 20 * - hash: Optional. Will use the first column as a key and return a hash instead of 21 * an array of objects. Default is false. 22 * 23 * Copyright (c) 2014-2019 Martin Drapeau 24 * 25 */ 26 27 var errorDetectingSeparator = "We could not detect the separator.", 28 errorNotWellFormed = "CSV is not well formed", 29 errorEmpty = "Empty CSV. Please provide something.", 30 errorEmptyHeader = 31 "Could not detect header. Ensure first row contains your column headers.", 32 separators = [",", ";", "\t"], 33 pegjsSeparatorNames = { 34 ",": "comma", 35 ";": "semicolon", 36 "\t": "tab", 37 }; 38 39 // Picks the separator we find the most. 40 function detectSeparator(csv) { 41 var counts = {}, 42 sepMax; 43 separators.forEach(function (sep, i) { 44 var re = new RegExp(sep, "g"); 45 counts[sep] = (csv.match(re) || []).length; 46 sepMax = !sepMax || counts[sep] > counts[sepMax] ? sep : sepMax; 47 }); 48 return sepMax; 49 } 50 51 // Source: https://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function 52 function zip() { 53 var args = [].slice.call(arguments); 54 var longest = args.reduce(function (a, b) { 55 return a.length > b.length ? a : b; 56 }, []); 57 58 return longest.map(function (_, i) { 59 return args.map(function (array) { 60 return array[i]; 61 }); 62 }); 63 } 64 65 function uniquify(keys) { 66 var counts = {}; 67 for (var i = 0; i < keys.length; i++) { 68 var key = keys[i]; 69 if (counts[key] === undefined) { 70 counts[key] = 0; 71 } else { 72 counts[key]++; 73 } 74 } 75 76 var result = []; 77 for (var i = keys.length - 1; i >= 0; i--) { 78 var key = keys[i]; 79 if (counts[key] > 0) key = key + "__" + counts[key]--; 80 result.unshift(key); 81 } 82 83 return result; 84 } 85 86 function convert(csv, options) { 87 options || (options = {}); 88 if (csv.length == 0) throw errorEmpty; 89 90 var separator = options.separator || detectSeparator(csv); 91 if (!separator) throw errorDetectingSeparator; 92 93 var a = []; 94 try { 95 var a = csvParser.parse(csv, pegjsSeparatorNames[separator]); 96 } catch (error) { 97 var start = csv.lastIndexOf("\n", error.offset), 98 end = csv.indexOf("\n", error.offset), 99 line = csv.substring( 100 start >= -1 ? start : 0, 101 end > -1 ? end : csv.length, 102 ); 103 throw ( 104 error.message + 105 " On line " + 106 error.line + 107 " and column " + 108 error.column + 109 ".\n" + 110 line 111 ); 112 } 113 114 if (options.transpose) a = zip.apply(this, a); 115 116 var keys = a.shift(); 117 if (keys.length == 0) throw errorEmptyHeader; 118 keys = keys.map(function (key) { 119 return key.trim().replace(/(^")|("$)/g, ""); 120 }); 121 122 keys = uniquify(keys); 123 124 var json = options.hash ? {} : []; 125 for (var l = 0; l < a.length; l++) { 126 var row = {}, 127 hashKey; 128 for (var i = 0; i < keys.length; i++) { 129 var value = (a[l][i] || "").trim().replace(/(^")|("$)/g, ""); 130 var number = value === "" ? NaN : value - 0; 131 if (options.hash && i == 0) { 132 hashKey = value; 133 } else { 134 if (options.parseJSON || (options.parseNumbers && !isNaN(number))) { 135 try { 136 row[keys[i]] = JSON.parse(value); 137 } catch (error) { 138 row[keys[i]] = value; 139 } 140 } else { 141 row[keys[i]] = value; 142 } 143 } 144 } 145 if (options.hash) json[hashKey] = row; 146 else json.push(row); 147 } 148 149 return json; 150 } 151 152 var csvParser = (function () { 153 /* 154 * Generated by PEG.js 0.7.0. 155 * 156 * http://pegjs.majda.cz/ 157 * 158 * source: https://gist.github.com/trevordixon/3362830 159 * Martin 2018-04-2: Added parse_semicolon function. 160 * 161 */ 162 163 function quote(s) { 164 /* 165 * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a 166 * string literal except for the closing quote character, backslash, 167 * carriage return, line separator, paragraph separator, and line feed. 168 * Any character may appear in the form of an escape sequence. 169 * 170 * For portability, we also escape escape all control and non-ASCII 171 * characters. Note that "\0" and "\v" escape sequences are not used 172 * because JSHint does not like the first and IE the second. 173 */ 174 return ( 175 '"' + 176 s 177 .replace(/\\/g, "\\\\") // backslash 178 .replace(/"/g, '\\"') // closing quote character 179 .replace(/\x08/g, "\\b") // backspace 180 .replace(/\t/g, "\\t") // horizontal tab 181 .replace(/\n/g, "\\n") // line feed 182 .replace(/\f/g, "\\f") // form feed 183 .replace(/\r/g, "\\r") // carriage return 184 .replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape) + 185 '"' 186 ); 187 } 188 189 var result = { 190 /* 191 * Parses the input with a generated parser. If the parsing is successfull, 192 * returns a value explicitly or implicitly specified by the grammar from 193 * which the parser was generated (see |PEG.buildParser|). If the parsing is 194 * unsuccessful, throws |PEG.parser.SyntaxError| describing the error. 195 */ 196 parse: function (input, startRule) { 197 var parseFunctions = { 198 comma: parse_comma, 199 semicolon: parse_semicolon, 200 tab: parse_tab, 201 sv: parse_sv, 202 line: parse_line, 203 field: parse_field, 204 char: parse_char, 205 }; 206 207 if (startRule !== undefined) { 208 if (parseFunctions[startRule] === undefined) { 209 throw new Error("Invalid rule name: " + quote(startRule) + "."); 210 } 211 } else { 212 startRule = "comma"; 213 } 214 215 var pos = 0; 216 var reportFailures = 0; 217 var rightmostFailuresPos = 0; 218 var rightmostFailuresExpected = []; 219 220 function padLeft(input, padding, length) { 221 var result = input; 222 223 var padLength = length - input.length; 224 for (var i = 0; i < padLength; i++) { 225 result = padding + result; 226 } 227 228 return result; 229 } 230 231 function escape(ch) {
232 var charCode = ch.charCodeAt(0); 233 var escapeChar; 234 var length; 235 236 if (charCode <= 0xff) { 237 escapeChar = "x"; 238 length = 2; 239 } else { 240 escapeChar = "u"; 241 length = 4; 242 } 243 244 return ( 245 "\\" + 246 escapeChar + 247 padLeft(charCode.toString(16).toUpperCase(), "0", length) 248 ); 249 } 250 251 function matchFailed(failure) { 252 if (pos < rightmostFailuresPos) { 253 return; 254 } 255 256 if (pos > rightmostFailuresPos) { 257 rightmostFailuresPos = pos; 258 rightmostFailuresExpected = []; 259 } 260 261 rightmostFailuresExpected.push(failure); 262 } 263 264 function parse_comma() { 265 var result0, result1; 266 var pos0, pos1; 267 268 pos0 = pos; 269 pos1 = pos; 270 result0 = (function (offset) { 271 return (separator = ","); 272 })(pos) 273 ? "" 274 : null; 275 if (result0 !== null) { 276 result1 = parse_sv(); 277 if (result1 !== null) { 278 result0 = [result0, result1]; 279 } else { 280 result0 = null; 281 pos = pos1; 282 } 283 } else { 284 result0 = null; 285 pos = pos1; 286 } 287 if (result0 !== null) { 288 result0 = (function (offset, sv) { 289 return sv; 290 })(pos0, result0[1]); 291 } 292 if (result0 === null) { 293 pos = pos0; 294 } 295 return result0; 296 } 297 298 function parse_semicolon() { 299 var result0, result1; 300 var pos0, pos1; 301 302 pos0 = pos; 303 pos1 = pos; 304 result0 = (function (offset) { 305 return (separator = ";"); 306 })(pos) 307 ? "" 308 : null; 309 if (result0 !== null) { 310 result1 = parse_sv(); 311 if (result1 !== null) { 312 result0 = [result0, result1]; 313 } else { 314 result0 = null; 315 pos = pos1; 316 } 317 } else { 318 result0 = null; 319 pos = pos1; 320 } 321 if (result0 !== null) { 322 result0 = (function (offset, sv) { 323 return sv; 324 })(pos0, result0[1]); 325 } 326 if (result0 === null) { 327 pos = pos0; 328 } 329 return result0; 330 } 331 332 function parse_tab() { 333 var result0, result1; 334 var pos0, pos1; 335 336 pos0 = pos; 337 pos1 = pos; 338 result0 = (function (offset) { 339 return (separator = "\t"); 340 })(pos) 341 ? "" 342 : null; 343 if (result0 !== null) { 344 result1 = parse_sv(); 345 if (result1 !== null) { 346 result0 = [result0, result1]; 347 } else { 348 result0 = null; 349 pos = pos1; 350 } 351 } else { 352 result0 = null; 353 pos = pos1; 354 } 355 if (result0 !== null) { 356 result0 = (function (offset, sv) { 357 return sv; 358 })(pos0, result0[1]); 359 } 360 if (result0 === null) { 361 pos = pos0; 362 } 363 return result0; 364 } 365 366 function parse_sv() { 367 var result0, result1, result2, result3, result4; 368 var pos0, pos1, pos2, pos3; 369 370 pos0 = pos; 371 pos1 = pos; 372 result0 = []; 373 if (/^[\n\r]/.test(input.charAt(pos))) { 374 result1 = input.charAt(pos); 375 pos++; 376 } else { 377 result1 = null; 378 if (reportFailures === 0) { 379 matchFailed("[\\n\\r]"); 380 } 381 } 382 while (result1 !== null) { 383 result0.push(result1); 384 if (/^[\n\r]/.test(input.charAt(pos))) { 385 result1 = input.charAt(pos); 386 pos++; 387 } else { 388 result1 = null; 389 if (reportFailures === 0) { 390 matchFailed("[\\n\\r]"); 391 } 392 } 393 } 394 if (result0 !== null) { 395 result1 = parse_line(); 396 if (result1 !== null) { 397 result2 = []; 398 pos2 = pos; 399 pos3 = pos; 400 if (/^[\n\r]/.test(input.charAt(pos))) { 401 result4 = input.charAt(pos); 402 pos++; 403 } else { 404 result4 = null; 405 if (reportFailures === 0) { 406 matchFailed("[\\n\\r]"); 407 } 408 } 409 if (result4 !== null) { 410 result3 = []; 411 while (result4 !== null) { 412 result3.push(result4); 413 if (/^[\n\r]/.test(input.charAt(pos))) { 414 result4 = input.charAt(pos); 415 pos++; 416 } else { 417 result4 = null; 418 if (reportFailures === 0) { 419 matchFailed("[\\n\\r]"); 420 } 421 } 422 } 423 } else { 424 result3 = null; 425 } 426 if (result3 !== null) { 427 result4 = parse_line(); 428 if (result4 !== null) { 429 result3 = [result3, result4]; 430 } else { 431 result3 = null; 432 pos = pos3; 433 } 434 } else { 435 result3 = null; 436 pos = pos3; 437 } 438 if (result3 !== null) { 439 result3 = (function (offset, data) { 440 return data; 441 })(pos2, result3[1]); 442 } 443 if (result3 === null) { 444 pos = pos2; 445 } 446 while (result3 !== null) { 447 result2.push(result3); 448 pos2 = pos; 449 pos3 = pos; 450 if (/^[\n\r]/.test(input.charAt(pos))) { 451 result4 = input.charAt(pos); 452 pos++; 453 } else { 454 result4 = null; 455 if (reportFailures === 0) { 456 matchFailed("[\\n\\r]"); 457 } 458 } 459 if (result4 !== null) { 460 result3 = []; 461 while (result4 !== null) { 462 result3.push(result4); 463 if (/^[\n\r]/.test(input.charAt(pos))) { 464 result4 = input.charAt(pos); 465 pos++; 466 } else { 467 result4 = null; 468 if (reportFailures === 0) { 469 matchFailed("[\\n\\r]"); 470 } 471 } 472 } 473 } else { 474 result3 = null; 475 } 476 if (result3 !== null) { 477 result4 = parse_line(); 478 if (result4 !== null) { 479 result3 = [result3, result4]; 480 } else { 481 result3 = null; 482 pos = pos3; 483 } 484 } else { 485 result3 = null; 486 pos = pos3; 487 } 488 if (result3 !== null) { 489 result3 = (function (offset, data) { 490 return data; 491 })(pos2, result3[1]); 492 } 493 if (result3 === null) { 494 pos = pos2; 495 } 496 } 497 if (result2 !== null) { 498 result3 = []; 499 if (/^[\n\r]/.test(input.charAt(pos))) { 500 result4 = input.charAt(pos); 501 pos++; 502 } else { 503 result4 = null; 504 if (reportFailures === 0) { 505 matchFailed("[\\n\\r]"); 506 } 507 } 508 while (result4 !== null) { 509 result3.push(result4); 510 if (/^[\n\r]/.test(input.charAt(pos))) { 511 result4 = input.charAt(pos); 512 pos++; 513 } else { 514 result4 = null; 515 if (reportFailures === 0) { 516 matchFailed("[\\n\\r]"); 517 } 518 } 519 } 520 if (result3 !== null) { 521 result0 = [result0, result1, result2, result3]; 522 } else { 523 result0 = null; 524 pos = pos1; 525 } 526 } else { 527 result0 = null; 528 pos = pos1; 529 } 530 } else { 531 result0 = null; 532 pos = pos1; 533 } 534 } else { 535 result0 = null; 536 pos = pos1; 537 } 538 if (result0 !== null) { 539 result0 = (function (offset, first, rest) { 540 rest.unshift(first); 541 return rest; 542 })(pos0, result0[1], result0[2]); 543 } 544 if (result0 === null) { 545 pos = pos0; 546 } 547 return result0; 548 } 549 550 function parse_line() { 551 var result0, result1, result2, result3, result4; 552 var pos0, pos1, pos2, pos3; 553 554 pos0 = pos; 555 pos1 = pos; 556 result0 = parse_field(); 557 if (result0 !== null) { 558 result1 = []; 559 pos2 = pos; 560 pos3 = pos; 561 if (input.length > pos) { 562 result2 = input.charAt(pos); 563 pos++; 564 } else { 565 result2 = null; 566 if (reportFailures === 0) { 567 matchFailed("any character"); 568 } 569 } 570 if (result2 !== null) { 571 result3 = (function (offset, char) { 572 return char == separator; 573 })(pos, result2) 574 ? "" 575 : null; 576 if (result3 !== null) { 577 result4 = parse_field(); 578 if (result4 !== null) { 579 result2 = [result2, result3, result4]; 580 } else { 581 result2 = null; 582 pos = pos3; 583 } 584 } else { 585 result2 = null; 586 pos = pos3; 587 } 588 } else { 589 result2 = null; 590 pos = pos3; 591 } 592 if (result2 !== null) { 593 result2 = (function (offset, char, text) { 594 return text; 595 })(pos2, result2[0], result2[2]); 596 } 597 if (result2 === null) { 598 pos = pos2; 599 } 600 while (result2 !== null) { 601 result1.push(result2); 602 pos2 = pos; 603 pos3 = pos; 604 if (input.length > pos) { 605 result2 = input.charAt(pos); 606 pos++; 607 } else { 608 result2 = null; 609 if (reportFailures === 0) { 610 matchFailed("any character"); 611 } 612 } 613 if (result2 !== null) { 614 result3 = (function (offset, char) { 615 return char == separator; 616 })(pos, result2) 617 ? "" 618 : null; 619 if (result3 !== null) { 620 result4 = parse_field(); 621 if (result4 !== null) { 622 result2 = [result2, result3, result4]; 623 } else { 624 result2 = null; 625 pos = pos3; 626 } 627 } else { 628 result2 = null; 629 pos = pos3; 630 } 631 } else { 632 result2 = null; 633 pos = pos3; 634 } 635 if (result2 !== null) { 636 result2 = (function (offset, char, text) { 637 return text; 638 })(pos2, result2[0], result2[2]); 639 } 640 if (result2 === null) { 641 pos = pos2; 642 } 643 } 644 if (result1 !== null) { 645 result2 = (function (offset, first, rest) { 646 return !!first || rest.length; 647 })(pos, result0, result1) 648 ? "" 649 : null; 650 if (result2 !== null) { 651 result0 = [result0, result1, result2]; 652 } else { 653 result0 = null; 654 pos = pos1; 655 } 656 } else { 657 result0 = null; 658 pos = pos1; 659 } 660 } else { 661 result0 = null; 662 pos = pos1; 663 } 664 if (result0 !== null) { 665 result0 = (function (offset, first, rest) { 666 rest.unshift(first); 667 return rest; 668 })(pos0, result0[0], result0[1]); 669 } 670 if (result0 === null) { 671 pos = pos0; 672 } 673 return result0; 674 } 675 676 function parse_field() { 677 var result0, result1, result2; 678 var pos0, pos1, pos2; 679 680 pos0 = pos; 681 pos1 = pos; 682 if (input.charCodeAt(pos) === 34) { 683 result0 = '"'; 684 pos++; 685 } else { 686 result0 = null; 687 if (reportFailures === 0) { 688 matchFailed('"\\""'); 689 } 690 } 691 if (result0 !== null) { 692 result1 = []; 693 result2 = parse_char(); 694 while (result2 !== null) { 695 result1.push(result2); 696 result2 = parse_char(); 697 } 698 if (result1 !== null) { 699 if (input.charCodeAt(pos) === 34) { 700 result2 = '"'; 701 pos++; 702 } else { 703 result2 = null; 704 if (reportFailures === 0) { 705 matchFailed('"\\""'); 706 } 707 } 708 if (result2 !== null) { 709 result0 = [result0, result1, result2]; 710 } else { 711 result0 = null; 712 pos = pos1; 713 } 714 } else { 715 result0 = null; 716 pos = pos1; 717 } 718 } else { 719 result0 = null; 720 pos = pos1; 721 } 722 if (result0 !== null) { 723 result0 = (function (offset, text) { 724 return text.join(""); 725 })(pos0, result0[1]); 726 } 727 if (result0 === null) { 728 pos = pos0; 729 } 730 if (result0 === null) { 731 pos0 = pos; 732 result0 = []; 733 pos1 = pos; 734 pos2 = pos; 735 if (/^[^\n\r]/.test(input.charAt(pos))) { 736 result1 = input.charAt(pos); 737 pos++; 738 } else { 739 result1 = null; 740 if (reportFailures === 0) { 741 matchFailed("[^\\n\\r]"); 742 } 743 } 744 if (result1 !== null) { 745 result2 = (function (offset, char) { 746 return char != separator; 747 })(pos, result1) 748 ? "" 749 : null; 750 if (result2 !== null) { 751 result1 = [result1, result2]; 752 } else { 753 result1 = null; 754 pos = pos2; 755 } 756 } else { 757 result1 = null; 758 pos = pos2; 759 } 760 if (result1 !== null) { 761 result1 = (function (offset, char) { 762 return char; 763 })(pos1, result1[0]); 764 } 765 if (result1 === null) { 766 pos = pos1; 767 } 768 while (result1 !== null) { 769 result0.push(result1); 770 pos1 = pos; 771 pos2 = pos; 772 if (/^[^\n\r]/.test(input.charAt(pos))) { 773 result1 = input.charAt(pos); 774 pos++; 775 } else { 776 result1 = null; 777 if (reportFailures === 0) { 778 matchFailed("[^\\n\\r]"); 779 } 780 } 781 if (result1 !== null) { 782 result2 = (function (offset, char) { 783 return char != separator; 784 })(pos, result1) 785 ? "" 786 : null; 787 if (
787result2 !== null) { 788 result1 = [result1, result2]; 789 } else { 790 result1 = null; 791 pos = pos2; 792 } 793 } else { 794 result1 = null; 795 pos = pos2; 796 } 797 if (result1 !== null) { 798 result1 = (function (offset, char) { 799 return char; 800 })(pos1, result1[0]); 801 } 802 if (result1 === null) { 803 pos = pos1; 804 } 805 } 806 if (result0 !== null) { 807 result0 = (function (offset, text) { 808 return text.join(""); 809 })(pos0, result0); 810 } 811 if (result0 === null) { 812 pos = pos0; 813 } 814 } 815 return result0; 816 } 817 818 function parse_char() { 819 var result0, result1; 820 var pos0, pos1; 821 822 pos0 = pos; 823 pos1 = pos; 824 if (input.charCodeAt(pos) === 34) { 825 result0 = '"'; 826 pos++; 827 } else { 828 result0 = null; 829 if (reportFailures === 0) { 830 matchFailed('"\\""'); 831 } 832 } 833 if (result0 !== null) { 834 if (input.charCodeAt(pos) === 34) { 835 result1 = '"'; 836 pos++; 837 } else { 838 result1 = null; 839 if (reportFailures === 0) { 840 matchFailed('"\\""'); 841 } 842 } 843 if (result1 !== null) { 844 result0 = [result0, result1]; 845 } else { 846 result0 = null; 847 pos = pos1; 848 } 849 } else { 850 result0 = null; 851 pos = pos1; 852 } 853 if (result0 !== null) { 854 result0 = (function (offset) { 855 return '"'; 856 })(pos0); 857 } 858 if (result0 === null) { 859 pos = pos0; 860 } 861 if (result0 === null) { 862 if (/^[^"]/.test(input.charAt(pos))) { 863 result0 = input.charAt(pos); 864 pos++; 865 } else { 866 result0 = null; 867 if (reportFailures === 0) { 868 matchFailed('[^"]'); 869 } 870 } 871 } 872 return result0; 873 } 874 875 function cleanupExpected(expected) { 876 expected.sort(); 877 878 var lastExpected = null; 879 var cleanExpected = []; 880 for (var i = 0; i < expected.length; i++) { 881 if (expected[i] !== lastExpected) { 882 cleanExpected.push(expected[i]); 883 lastExpected = expected[i]; 884 } 885 } 886 return cleanExpected; 887 } 888 889 function computeErrorPosition() { 890 /* 891 * The first idea was to use |String.split| to break the input up to the 892 * error position along newlines and derive the line and column from 893 * there. However IE's |split| implementation is so broken that it was 894 * enough to prevent it. 895 */ 896 897 var line = 1; 898 var column = 1; 899 var seenCR = false; 900 901 for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) { 902 var ch = input.charAt(i); 903 if (ch === "\n") { 904 if (!seenCR) { 905 line++; 906 } 907 column = 1; 908 seenCR = false; 909 } else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") { 910 line++; 911 column = 1; 912 seenCR = true; 913 } else { 914 column++; 915 seenCR = false; 916 } 917 } 918 919 return { line: line, column: column }; 920 } 921 922 var separator = ","; 923 924 var result = parseFunctions[startRule](); 925 926 /* 927 * The parser is now in one of the following three states: 928 * 929 * 1. The parser successfully parsed the whole input. 930 * 931 * - |result !== null| 932 * - |pos === input.length| 933 * - |rightmostFailuresExpected| may or may not contain something 934 * 935 * 2. The parser successfully parsed only a part of the input. 936 * 937 * - |result !== null| 938 * - |pos < input.length| 939 * - |rightmostFailuresExpected| may or may not contain something 940 * 941 * 3. The parser did not successfully parse any part of the input. 942 * 943 * - |result === null| 944 * - |pos === 0| 945 * - |rightmostFailuresExpected| contains at least one failure 946 * 947 * All code following this comment (including called functions) must 948 * handle these states. 949 */ 950 if (result === null || pos !== input.length) { 951 var offset = Math.max(pos, rightmostFailuresPos); 952 var found = offset < input.length ? input.charAt(offset) : null; 953 var errorPosition = computeErrorPosition(); 954 955 throw new this.SyntaxError( 956 cleanupExpected(rightmostFailuresExpected), 957 found, 958 offset, 959 errorPosition.line, 960 errorPosition.column, 961 ); 962 } 963 964 return result; 965 }, 966 967 /* Returns the parser source code. */ 968 toSource: function () { 969 return this._source; 970 }, 971 }; 972 973 /* Thrown when a parser encounters a syntax error. */ 974 975 result.SyntaxError = function (expected, found, offset, line, column) { 976 function buildMessage(expected, found) { 977 var expectedHumanized, foundHumanized; 978 979 switch (expected.length) { 980 case 0: 981 expectedHumanized = "end of input"; 982 break; 983 case 1: 984 expectedHumanized = expected[0]; 985 break; 986 default: 987 expectedHumanized = 988 expected.slice(0, expected.length - 1).join(", ") + 989 " or " + 990 expected[expected.length - 1]; 991 } 992 993 foundHumanized = found ? quote(found) : "end of input"; 994 995 return ( 996 "Expected " + expectedHumanized + " but " + foundHumanized + " found." 997 ); 998 } 999 1000 this.name = "SyntaxError"; 1001 this.expected = expected; 1002 this.found = found; 1003 this.message = buildMessage(expected, found); 1004 this.offset = offset; 1005 this.line = line; 1006 this.column = column; 1007 }; 1008 1009 result.SyntaxError.prototype = Error.prototype; 1010 1011 return result; 1012 })(); 1013 1014 // CommonJS or Browser 1015 if (typeof exports !== "undefined") { 1016 if (typeof module !== "undefined" && module.exports) { 1017 exports = module.exports = convert; 1018 } 1019 exports.csv2json = convert; 1020 } else { 1021 this.CSVJSON || (this.CSVJSON = {}); 1022 this.CSVJSON.csv2json = convert; 1023 } 1024}.call(this));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.