Advertisement
Guest User

parseCSV

a guest
Jan 19th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var Strings = {
  2.     /**
  3.      * Wrapped csv line parser
  4.      * @param s string delimited csv string
  5.      * @param sep separator
  6.      * @attribution : http://www.greywyvern.com/?post=258 (comments closed on blog :( )
  7.      */
  8.     parseCSV : function(s,sep) {
  9.         // http://stackoverflow.com/questions/1155678/javascript-string-newline-character
  10.         var universalNewline = /\r\n|\r|\n/g;
  11.         var a = s.split(universalNewline);
  12.         for(var i in a){
  13.             for (var f = a[i].split(sep = sep || ","), x = f.length - 1, tl; x >= 0; x--) {
  14.                 if (f[x].replace(/"\s+$/, '"').charAt(f[x].length - 1) == '"') {
  15.                 if ((tl = f[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
  16.                     f[x] = f[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
  17.                   } else if (x) {
  18.                 f.splice(x - 1, 2, [f[x - 1], f[x]].join(sep));
  19.               } else f = f.shift().split(sep).concat(f);
  20.             } else f[x].replace(/""/g, '"');
  21.           } a[i] = f;
  22.         }
  23.         return a;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement