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 @@ -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>
<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>
<br><br>
<code id="output"></code>
</div>
<script>
$(function()
<script>
$(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(), {
delimiter: $("#delim").val(),
header: $('#header').is(':checked'),
dynamicTyping: $('#dyntype').is(':checked')
});
$('#output').text(JSON.stringify(results, undefined, 2));
});
console.log("BEFORE", file, inputElem);
},
error: function(err, file, elem)
{
console.log("ERROR", err, file, elem);
},
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>
</html>

112
jquery.parse.js

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*
jQuery Parse Plugin
v0.6.1
v1.0.0
https://github.com/mholt/jquery.parse
*/
@ -8,11 +8,99 @@ @@ -8,11 +8,99 @@
{
"use strict";
var reader = new FileReader();
var defaults = {
delimiter: ",",
header: true,
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)
{
@ -27,12 +115,8 @@ @@ -27,12 +115,8 @@
function verifyOptions(opt)
{
opt.delimiter = opt.delimiter || defaults.delimiter;
opt.header = typeof opt.header === 'undefined'
? defaults.header
: opt.header;
opt.dynamicTyping = typeof opt.dynamicTyping === 'undefined'
? defaults.dynamicTyping
: opt.dynamicTyping;
opt.header = !isDef(opt.header) ? defaults.header : opt.header;
opt.dynamicTyping = !isDef(opt.dynamicTyping) ? defaults.dynamicTyping : opt.dynamicTyping;
if (opt.delimiter == '"' || opt.delimiter == "\n")
opt.delimiter = defaults.delimiter;
@ -43,6 +127,16 @@ @@ -43,6 +127,16 @@
return opt;
}
function isFunction(func)
{
return typeof func === 'function';
}
function isDef(val)
{
return typeof val !== 'undefined'
}
function Parser(input, config)
{
var self = this;
@ -112,6 +206,10 @@ @@ -112,6 +206,10 @@
}
_config = opt;
if (typeof opt.delimiter !== 'undefined')
self.setDelimiter(opt.delimiter);
console.log("DELIM:", _config);
}
this.getInput = function()

10
parse.jquery.json

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
{
"name": "parse",
"version": "0.6.1",
"title": "jQuery Parse Plugin",
"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.",
"version": "1.0.0",
"title": "jQuery Parse",
"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": [
"csv",
"parse",
@ -13,7 +13,9 @@ @@ -13,7 +13,9 @@
"data",
"comma",
"tab",
"pipe"
"pipe",
"file",
"filereader"
],
"author": {
"name": "Matthew Holt",

2
tests.html

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

Loading…
Cancel
Save