Browse Source

Merge branch 'master' of github.com:mholt/papaparse

pull/370/head
Matthew Holt 8 years ago
parent
commit
b5dc67545b
  1. 1
      Gruntfile.js
  2. 2
      package.json
  3. 45
      papaparse.js
  4. 4
      papaparse.min.js
  5. 51
      tests/test-cases.js

1
Gruntfile.js

@ -15,4 +15,5 @@ module.exports = function(grunt) { @@ -15,4 +15,5 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('build', ['uglify']);
grunt.registerTask('default', ['uglify']);
}

2
package.json

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
{
"name": "papaparse",
"version": "4.1.2",
"version": "4.1.4",
"description": "Fast and powerful CSV parser for the browser that supports web workers and streaming large files. Converts CSV to JSON and JSON to CSV.",
"keywords": [
"csv",

45
papaparse.js

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*!
Papa Parse
v4.1.2
v4.1.4
https://github.com/mholt/PapaParse
*/
(function(root, factory)
@ -26,7 +26,20 @@ @@ -26,7 +26,20 @@
{
'use strict';
var global = Function('return this')();
var global = (function () {
// alternative method, similar to `Function('return this')()`
// but without using `eval` (which is disabled when
// using Content Security Policy).
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
// When running tests none of the above have been defined
return {};
})();
var IS_WORKER = !global.document && !!global.postMessage,
IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search),
LOADED_SYNC = false, AUTO_SCRIPT_PATH;
@ -234,14 +247,22 @@ @@ -234,14 +247,22 @@
/** whether to surround every datum with quotes */
var _quotes = false;
/** whether to write headers */
var _writeHeader = true;
/** delimiting character */
var _delimiter = ',';
/** newline character(s) */
var _newline = '\r\n';
/** quote character */
var _quoteChar = '"';
unpackConfig();
var quoteCharRegex = new RegExp(_quoteChar, 'g');
if (typeof _input === 'string')
_input = JSON.parse(_input);
@ -296,6 +317,12 @@ @@ -296,6 +317,12 @@
if (typeof _config.newline === 'string')
_newline = _config.newline;
if (typeof _config.quoteChar === 'string')
_quoteChar = _config.quoteChar;
if (typeof _config.header === 'boolean')
_writeHeader = _config.header;
}
@ -324,7 +351,7 @@ @@ -324,7 +351,7 @@
var dataKeyedByField = !(data[0] instanceof Array);
// If there a header row, write it first
if (hasHeader)
if (hasHeader && _writeHeader)
{
for (var i = 0; i < fields.length; i++)
{
@ -362,7 +389,7 @@ @@ -362,7 +389,7 @@
if (typeof str === 'undefined' || str === null)
return '';
str = str.toString().replace(/"/g, '""');
str = str.toString().replace(quoteCharRegex, _quoteChar+_quoteChar);
var needsQuotes = (typeof _quotes === 'boolean' && _quotes)
|| (_quotes instanceof Array && _quotes[col])
@ -371,7 +398,7 @@ @@ -371,7 +398,7 @@
|| str.charAt(0) === ' '
|| str.charAt(str.length - 1) === ' ';
return needsQuotes ? '"' + str + '"' : str;
return needsQuotes ? _quoteChar + str + _quoteChar : str;
}
function hasAny(str, substrings)
@ -594,6 +621,9 @@ @@ -594,6 +621,9 @@
function getFileSize(xhr)
{
var contentRange = xhr.getResponseHeader('Content-Range');
if (contentRange === null) { // no content range, then finish!
return -1;
}
return parseInt(contentRange.substr(contentRange.lastIndexOf('/') + 1));
}
}
@ -765,6 +795,11 @@ @@ -765,6 +795,11 @@
}
_results.meta.delimiter = _config.delimiter;
}
else if(typeof _config.delimiter === 'function')
{
_config.delimiter = _config.delimiter(input);
_results.meta.delimiter = _config.delimiter;
}
var parserConfig = copy(_config);
if (_config.preview && _config.header)

4
papaparse.min.js vendored

File diff suppressed because one or more lines are too long

51
tests/test-cases.js

@ -625,6 +625,15 @@ var PARSE_TESTS = [ @@ -625,6 +625,15 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Callback delimiter",
input: 'a$ b$ c',
config: { delimiter: function(input) { return input[1] + ' '; } },
expected: {
data: [['a', 'b', 'c']],
errors: []
}
},
{
description: "Dynamic typing converts numeric literals",
input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2',
@ -892,16 +901,26 @@ var PARSE_TESTS = [ @@ -892,16 +901,26 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Single quote as quote character",
notes: "Must parse correctly when single quote is specified as a quote character",
input: "a,b,'c,d'",
config: { quoteChar: "'"},
expected: {
data: [['a', 'b', 'c,d']],
errors: []
}
}
{
description: "Single quote as quote character",
notes: "Must parse correctly when single quote is specified as a quote character",
input: "a,b,'c,d'",
config: { quoteChar: "'"},
expected: {
data: [['a', 'b', 'c,d']],
errors: []
}
},
{
description: "Header row with preceding comment",
notes: "Must parse correctly headers if they are preceded by comments",
input: '#Comment\na,b\nc,d\n',
config: { header: true, comments: '#', skipEmptyLines: true, delimiter: ','},
expected: {
data: [{'a': 'c', 'b': 'd'}],
errors: []
}
}
];
describe('Parse Tests', function() {
@ -1152,6 +1171,18 @@ var UNPARSE_TESTS = [ @@ -1152,6 +1171,18 @@ var UNPARSE_TESTS = [
description: "JSON null is treated as empty value",
input: [{ "Col1": "a", "Col2": null, "Col3": "c" }],
expected: 'Col1,Col2,Col3\r\na,,c'
},
{
description: "Custom quote character (single quote)",
input: [['a,d','b','c']],
config: { quoteChar: "'"},
expected: "'a,d',b,c"
},
{
description: "Don't print header if header:false option specified",
input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Col3": "f" }],
config: { header: false },
expected: 'a,b,c\r\nd,e,f'
}
];

Loading…
Cancel
Save