Created
May 12, 2011 15:18
-
-
Save yavor-atanasov/968717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Usage: | |
* jQuery.csv()(csvtext) returns an array of arrays representing the CSV text. | |
* jQuery.csv("\t")(tsvtext) uses Tab as a delimiter (comma is the default) | |
* jQuery.csv("\t", "'")(tsvtext) uses a single quote as the quote character instead of double quotes | |
* jQuery.csv("\t", "'\"")(tsvtext) uses single & double quotes as the quote character | |
*/ | |
; | |
jQuery.extend({ | |
csv: function(delim, quote, linedelim) { | |
delim = typeof delim == "string" ? new RegExp( "[" + (delim || "," ) + "]" ) : typeof delim == "undefined" ? "," : delim; | |
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"' ) + "]" ) : typeof quote == "undefined" ? '"' : quote; | |
lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined; | |
function splitline (v) { | |
// Split the line using the delimitor | |
var arr = v.split(delim), | |
out = [], q; | |
for (var i=0, l=arr.length; i<l; i++) { | |
if (q = arr[i].match(quote)) { | |
for (j=i; j<l; j++) { | |
if (arr[j].charAt(arr[j].length-1) == q[0]) { break; } | |
} | |
var s = arr.slice(i,j+1).join(delim); | |
out.push(s.substr(1,s.length-2)); | |
i = j; | |
} | |
else { out.push(arr[i]); } | |
} | |
return out; | |
} | |
return function(text) { | |
var lines = text.split(lined); | |
for (var i=0, l=lines.length; i<l; i++) { | |
lines[i] = splitline(lines[i]); | |
} | |
return lines; | |
}; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
You wrap the plugin in a define() statement | |
that requires jquery-1 as a dependency | |
and you extend the jQuery object in the callback ($) | |
*/ | |
define(['jquery-1'], function($){ | |
$.extend({ | |
csv: function(delim, quote, linedelim) { | |
delim = typeof delim == "string" ? new RegExp( "[" + (delim || "," ) + "]" ) : typeof delim == "undefined" ? "," : delim; | |
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"' ) + "]" ) : typeof quote == "undefined" ? '"' : quote; | |
lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined; | |
function splitline (v) { | |
// Split the line using the delimitor | |
var arr = v.split(delim), | |
out = [], q; | |
for (var i=0, l=arr.length; i<l; i++) { | |
if (q = arr[i].match(quote)) { | |
for (j=i; j<l; j++) { | |
if (arr[j].charAt(arr[j].length-1) == q[0]) { break; } | |
} | |
var s = arr.slice(i,j+1).join(delim); | |
out.push(s.substr(1,s.length-2)); | |
i = j; | |
} | |
else { out.push(arr[i]); } | |
} | |
return out; | |
} | |
return function(text) { | |
var lines = text.split(lined); | |
for (var i=0, l=lines.length; i<l; i++) { | |
lines[i] = splitline(lines[i]); | |
} | |
return lines; | |
}; | |
} | |
}); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
And this is how you consume the module and use the plugin inside it. | |
NOTE: in your path to the cvs plugin file you do not include the '.js' | |
*/ | |
require(["jquery-1","/path/to/local/scripts/jquery.cvs",], function($) { | |
$(function() { | |
// $.csv() should be available | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment