diff --git a/index.html b/index.html index 276b1c9..91f4d4e 100644 --- a/index.html +++ b/index.html @@ -70,25 +70,57 @@ Duke Rd & Walden Ave,Buffalo,NY,14225,Apple Store Walden Galleria,(716) 685-2762    
- + +

+ or +

+ + +

+

- + }); + + function render(results) + { + $('#output').text(JSON.stringify(results, undefined, 2)); + } +}); + \ No newline at end of file diff --git a/jquery.parse.js b/jquery.parse.js index d659af9..2874646 100644 --- a/jquery.parse.js +++ b/jquery.parse.js @@ -1,6 +1,6 @@ /* jQuery Parse Plugin - v0.6.1 + v1.0.0 https://github.com/mholt/jquery.parse */ @@ -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 @@ 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 @@ 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 @@ } _config = opt; + + if (typeof opt.delimiter !== 'undefined') + self.setDelimiter(opt.delimiter); + console.log("DELIM:", _config); } this.getInput = function() diff --git a/parse.jquery.json b/parse.jquery.json index ada194b..145c1a2 100644 --- a/parse.jquery.json +++ b/parse.jquery.json @@ -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 @@ "data", "comma", "tab", - "pipe" + "pipe", + "file", + "filereader" ], "author": { "name": "Matthew Holt", diff --git a/tests.html b/tests.html index 23df8eb..d9de57d 100644 --- a/tests.html +++ b/tests.html @@ -28,7 +28,7 @@ } #results td div { - overflow-x: scroll; + overflow-x: auto; } .count {