Skip to content

Commit

Permalink
Fix #2: Csv: Add ability to auto-generate IDs sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
kfranqueiro committed Oct 13, 2013
1 parent 38679fc commit 60d0542
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ define([
// summary:
// Extension of dojo/store/Memory supporting an input string in CSV format.

// idProperty: String
// Indicates the field in CSV entries representing the unique
// identity of each item. Defaults to "id". If set to a falsy
// value (e.g. ""), IDs will be auto-generated based on their order
// in the CSV data (starting at 1).

// fieldNames: Array?
// If specified, indicates names of fields in the order they appear in
// CSV records. If unspecified, the first line of the CSV will be treated
Expand Down Expand Up @@ -56,9 +62,14 @@ define([
values = [], // records values in the current record
value = "",
prefix = "", // used to re-add delimiters and newlines to a spanning value
parts, part, numlines, numparts, match,
parts, part, numlines, numparts, match, item, index,
i, j, k;

if (!this.idProperty) {
// Set idProperty to field we will auto-generate sequentially
this.idProperty = "__id";
}

// Outer loop iterates over lines. It's labeled so that inner loop
// can jump out if an invalid value is encountered.
lineloop:
Expand Down Expand Up @@ -118,7 +129,10 @@ define([
// first row of data.
fieldNames = this.fieldNames = values;
} else {
data.push(arrays2hash(fieldNames, values));
index = data.push(item = arrays2hash(fieldNames, values));
if (this.idProperty === "__id") {
item.__id = index;
}
}
values = [];
} else {
Expand Down
31 changes: 31 additions & 0 deletions test/Csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ function(doh, request, all, Csv) {
data: data,
trim: true
});
stores.noQuoteNoHeaderAutoId = new Csv({
data: data,
fieldNames: ["id", "last", "first", "born", "died"],
trim: true,
idProperty: "" // Tell store to auto-generate IDs
});
stores.noQuoteAutoId = new Csv({
data: data,
trim: true,
idProperty: "" // Tell store to auto-generate IDs
});
}),
request(xhrBase + "/quote.csv").then(function(data) {
csvs.quote = data;
Expand Down Expand Up @@ -103,6 +114,26 @@ function(doh, request, all, Csv) {
t.t(/smiling,\r\n/.test(withHeader.get("2").quote),
"Multiline value should use same newline format as input.");
},
function autoId(t) {
var noQuoteAutoId = stores.noQuoteAutoId,
noQuoteNoHeaderAutoId = stores.noQuoteNoHeaderAutoId;
console.log(noQuoteAutoId.data, noQuoteNoHeaderAutoId.data);
t.is(noQuoteNoHeaderAutoId.get("1").last, "last",
"Item 1 should be the first line");
t.is(noQuoteNoHeaderAutoId.get("2").last, "Hawking",
"Item 2 should be the second line");
t.is(noQuoteNoHeaderAutoId.get("3").last, "Einstein",
"Item 3 should be the third line");
t.is(noQuoteNoHeaderAutoId.get("4").last, "Tesla",
"Item 4 should be the fourth line");

t.is(noQuoteAutoId.get("1").last, "Hawking",
"Item 1 should be the first non-header line");
t.is(noQuoteAutoId.get("2").last, "Einstein",
"Item 2 should be the second non-header line");
t.is(noQuoteAutoId.get("3").last, "Tesla",
"Item 3 should be the third non-header line");
},
function importExport(t) {
t.is(csvs.contributors, stores.contributors.toCsv(),
"toCsv() should generate data matching original if it is well-formed");
Expand Down

0 comments on commit 60d0542

Please sign in to comment.