Browse Source

Supports parsing files; and multiple files from multiple inputs

pull/17/head
Matthew Holt 11 years ago
parent
commit
cb85561d08
  1. 58
      index.html
  2. 112
      jquery.parse.js
  3. 10
      parse.jquery.json
  4. 2
      tests.html

58
index.html

@ -70,25 +70,57 @@ Duke Rd & Walden Ave,Buffalo,NY,14225,Apple Store Walden Galleria,(716) 685-2762
       
<label><input type="checkbox" id="dyntype" checked> Dynamic typing</label> <label><input type="checkbox" id="dyntype" checked> Dynamic typing</label>
<br> <br>
<button id="btn">Parse</button> <button id="parseText">Parse Text</button>
<br><hr>
or
<br><hr>
<input type="file" id="fileinput1" multiple>
<input type="file" id="fileinput2" multiple>
<br><br>
<button id="parseFiles">Parse File(s)</button>
</div> </div>
<br><br> <br><br>
<code id="output"></code> <code id="output"></code>
</div> </div>
<script> <script>
$(function() $(function()
{
$('#parseText').click(function()
{
var results = $.parse($('#tb').val(), {
delimiter: $("#delim").val(),
header: $('#header').is(':checked'),
dynamicTyping: $('#dyntype').is(':checked')
});
render(results);
});
$('#parseFiles').click(function()
{
$('#fileinput1, #fileinput2').parse(
{ {
$('#btn').click(function() before: function(file, inputElem)
{ {
var results = $.parse($('#tb').val(), { console.log("BEFORE", file, inputElem);
delimiter: $("#delim").val(), },
header: $('#header').is(':checked'), error: function(err, file, elem)
dynamicTyping: $('#dyntype').is(':checked') {
}); console.log("ERROR", err, file, elem);
},
$('#output').text(JSON.stringify(results, undefined, 2)); complete: function(data, file, inputElem, event)
}); {
console.log("COMPLETE", data, file, inputElem, event);
render(data);
}
}); });
</script> });
function render(results)
{
$('#output').text(JSON.stringify(results, undefined, 2));
}
});
</script>
</body> </body>
</html> </html>

112
jquery.parse.js

@ -1,6 +1,6 @@
/* /*
jQuery Parse Plugin jQuery Parse Plugin
v0.6.1 v1.0.0
https://github.com/mholt/jquery.parse https://github.com/mholt/jquery.parse
*/ */
@ -8,11 +8,99 @@
{ {
"use strict"; "use strict";
var reader = new FileReader();
var defaults = { var defaults = {
delimiter: ",", delimiter: ",",
header: true, header: true,
dynamicTyping: false dynamicTyping: false
}; };
var config = {
delimiter: defaults.delimiter,
header: defaults.header,
dynamicTyping: defaults.dynamicTyping
};
$.fn.parse = function(options)
{
function error(name, elem, file)
{
if (isFunction(options.error))
options.error({name: name}, elem, file);
}
this.each(function(idx)
{
var supported = $(this).prop('tagName').toUpperCase() == "INPUT"
&& $(this).attr('type') == 'file'
&& window.FileReader;
if (!supported)
return true; // continue to next input element
// Config to be used only for this instance of parsing
var instanceConfig = {
delimiter: config.delimiter,
header: config.header,
dynamicTyping: config.dynamicTyping
};
if (!this.files || this.files.length == 0)
{
error("NoFileError", undefined, this);
return true; // continue to next input element
}
for (var i = 0; i < this.files.length; i++)
{
var file = this.files[i];
if (file.type.indexOf("text") < 0)
{
error("TypeMismatchError", file, this);
continue; // continue to next file in this input element
}
if (isFunction(options.before))
{
var returned = options.before(file, this);
if (typeof returned === 'object')
{
// update config for this file/instance only
if (isDef(returned.delimiter))
instanceConfig.delimiter = returned.delimiter;
if (isDef(returned.header))
instanceConfig.header = returned.header;
if (isDef(returned.dynamicTyping))
instanceConfig.dynamicTyping = returned.dynamicTyping;
}
else if (returned === "skip")
continue; // proceed to next file
else if (returned === false)
{
error("AbortError", file, this);
return false; // aborts the `.each()` loop
}
}
if (isFunction(options.error))
reader.onerror = function() { options.error(reader.error, file, this); };
var inputElem = this;
reader.onload = function(event)
{
var text = event.target.result;
var results = $.parse(text, instanceConfig);
if (isFunction(options.complete))
options.complete(results, file, inputElem, event);
};
reader.readAsText(file);
}
});
return this;
};
$.parse = function(input, options) $.parse = function(input, options)
{ {
@ -27,12 +115,8 @@
function verifyOptions(opt) function verifyOptions(opt)
{ {
opt.delimiter = opt.delimiter || defaults.delimiter; opt.delimiter = opt.delimiter || defaults.delimiter;
opt.header = typeof opt.header === 'undefined' opt.header = !isDef(opt.header) ? defaults.header : opt.header;
? defaults.header opt.dynamicTyping = !isDef(opt.dynamicTyping) ? defaults.dynamicTyping : opt.dynamicTyping;
: opt.header;
opt.dynamicTyping = typeof opt.dynamicTyping === 'undefined'
? defaults.dynamicTyping
: opt.dynamicTyping;
if (opt.delimiter == '"' || opt.delimiter == "\n") if (opt.delimiter == '"' || opt.delimiter == "\n")
opt.delimiter = defaults.delimiter; opt.delimiter = defaults.delimiter;
@ -43,6 +127,16 @@
return opt; return opt;
} }
function isFunction(func)
{
return typeof func === 'function';
}
function isDef(val)
{
return typeof val !== 'undefined'
}
function Parser(input, config) function Parser(input, config)
{ {
var self = this; var self = this;
@ -112,6 +206,10 @@
} }
_config = opt; _config = opt;
if (typeof opt.delimiter !== 'undefined')
self.setDelimiter(opt.delimiter);
console.log("DELIM:", _config);
} }
this.getInput = function() this.getInput = function()

10
parse.jquery.json

@ -1,8 +1,8 @@
{ {
"name": "parse", "name": "parse",
"version": "0.6.1", "version": "1.0.0",
"title": "jQuery Parse Plugin", "title": "jQuery Parse",
"description": "Parses CSV (or any delimited text) into a usable data structure. This efficient and robust parser gracefully handles malformed input. The parsing function can work without jQuery.", "description": "Efficiently parses CSV (character-separated, or delimited text) files or strings into arrays and objects. Gracefully handles errors. Supports multiple file inputs and multiple files per input element.",
"keywords": [ "keywords": [
"csv", "csv",
"parse", "parse",
@ -13,7 +13,9 @@
"data", "data",
"comma", "comma",
"tab", "tab",
"pipe" "pipe",
"file",
"filereader"
], ],
"author": { "author": {
"name": "Matthew Holt", "name": "Matthew Holt",

2
tests.html

@ -28,7 +28,7 @@
} }
#results td div { #results td div {
overflow-x: scroll; overflow-x: auto;
} }
.count { .count {

Loading…
Cancel
Save