[backend/localcsv] Add support for defining the text delimiter on CSV files
This commit is contained in:
parent
de732fd27b
commit
7def51b573
@ -141,6 +141,13 @@
|
||||
<input type="text" name="separator" value="," class="spam1"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label">Text delimiter</label>
|
||||
<div class="controls">
|
||||
<input type="text" name="delimiter" value='"' />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label class="control-label">Encoding</label>
|
||||
<div class="controls">
|
||||
|
||||
@ -128,6 +128,7 @@ var ExplorerApp = Backbone.View.extend({
|
||||
var file = $file.files[0];
|
||||
var options = {
|
||||
separator : $form.find('input[name="separator"]').val(),
|
||||
delimiter : $form.find('input[name="delimiter"]').val(),
|
||||
encoding : $form.find('input[name="encoding"]').val()
|
||||
};
|
||||
recline.Backend.loadFromCSVFile(file, function(dataset) {
|
||||
|
||||
@ -58,7 +58,9 @@ this.recline.Backend = this.recline.Backend || {};
|
||||
var options = options || {};
|
||||
var trm = options.trim;
|
||||
var separator = options.separator || ',';
|
||||
|
||||
var delimiter = options.delimiter || '"';
|
||||
|
||||
|
||||
var cur = '', // The character we are currently processing.
|
||||
inQuote = false,
|
||||
fieldQuoted = false,
|
||||
@ -105,8 +107,8 @@ this.recline.Backend = this.recline.Backend || {};
|
||||
field = '';
|
||||
fieldQuoted = false;
|
||||
} else {
|
||||
// If it's not a ", add it to the field buffer
|
||||
if (cur !== '"') {
|
||||
// If it's not a delimiter, add it to the field buffer
|
||||
if (cur !== delimiter) {
|
||||
field += cur;
|
||||
} else {
|
||||
if (!inQuote) {
|
||||
@ -114,9 +116,9 @@ this.recline.Backend = this.recline.Backend || {};
|
||||
inQuote = true;
|
||||
fieldQuoted = true;
|
||||
} else {
|
||||
// Next char is ", this is an escaped "
|
||||
if (s.charAt(i + 1) === '"') {
|
||||
field += '"';
|
||||
// Next char is delimiter, this is an escaped delimiter
|
||||
if (s.charAt(i + 1) === delimiter) {
|
||||
field += delimiter;
|
||||
// Skip the next char
|
||||
i += 1;
|
||||
} else {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
(function ($) {
|
||||
module("Backend Local CSV");
|
||||
|
||||
test("parseCSV", function() {
|
||||
test("parseCSV", function() {
|
||||
var csv = '"Jones, Jay",10\n' +
|
||||
'"Xyz ""ABC"" O\'Brien",11:35\n' +
|
||||
'"Other, AN",12:35\n';
|
||||
@ -29,7 +29,7 @@ test("parseCSV", function() {
|
||||
equal(dataset.currentDocuments.length, 3);
|
||||
});
|
||||
|
||||
test("parseCSVsemicolon", function() {
|
||||
test("parseCSVsemicolon", function() {
|
||||
var csv = '"Jones; Jay";10\n' +
|
||||
'"Xyz ""ABC"" O\'Brien";11:35\n' +
|
||||
'"Other; AN";12:35\n';
|
||||
@ -44,4 +44,20 @@ test("parseCSVsemicolon", function() {
|
||||
|
||||
});
|
||||
|
||||
test("parseCSVdelimiter", function() {
|
||||
var csv = "'Jones, Jay',10\n" +
|
||||
"'Xyz \"ABC\" O''Brien',11:35\n" +
|
||||
"'Other; AN',12:35\n";
|
||||
|
||||
var array = recline.Backend.parseCSV(csv, {delimiter:"'"});
|
||||
var exp = [
|
||||
["Jones, Jay", 10],
|
||||
["Xyz \"ABC\" O'Brien", "11:35" ],
|
||||
["Other; AN", "12:35" ]
|
||||
];
|
||||
deepEqual(exp, array);
|
||||
|
||||
});
|
||||
|
||||
|
||||
})(this.jQuery);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user