Skip to content

Commit

Permalink
Merge branch 'master' into momentjs_upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed May 29, 2012
2 parents d6beffa + c4cb66e commit cd83b79
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 162 deletions.
17 changes: 13 additions & 4 deletions src/dataset.js
Expand Up @@ -149,13 +149,14 @@ Version 0.0.1.2
// implementation, pass it as an option
if (options.deferred) {
this.deferred = options.deferred;
} else {
this.deferred = new _.Deferred();
}

//build any columns present in the constructor
if ( options.columns ) {
this.addColumns(options.columns);
}

},

/**
Expand Down Expand Up @@ -186,7 +187,7 @@ Version 0.0.1.2
fetch : function(options) {
options = options || {};

var dfd = this.deferred || new _.Deferred();
var dfd = this.deferred;

if ( _.isNull(this.importer) ) {
throw "No importer defined";
Expand All @@ -195,7 +196,15 @@ Version 0.0.1.2
this.importer.fetch({
success: _.bind(function( data ) {

this._apply( data );
try {
this._apply( data );
} catch (e) {
if (options.error) {
options.error.call(this, e);
} else {
throw e;
}
}

// if a comparator was defined, sort the data
if (this.comparator) {
Expand All @@ -217,7 +226,7 @@ Version 0.0.1.2

error : _.bind(function(e) {
if (options.error) {
options.error.call(this);
options.error.call(this, e);
}

dfd.reject(e);
Expand Down
12 changes: 5 additions & 7 deletions src/importers/google_spreadsheet.js
Expand Up @@ -34,19 +34,16 @@

options.url = "https://spreadsheets.google.com/tq?key=" + options.key;

if (options.sheetName) {
options.url += "&sheet=" + options.sheetName;
} else {
options.url += "&gid=" + (options.worksheet || 1);
delete options.worksheet;
}
if (typeof options.sheetName === "undefined") {
options.sheetName = "Sheet1";
}

options.url += "&sheet=" + options.sheetName;
this.callback = "misodsgs" + new Date().getTime();
options.url += "&tqx=version:0.6;responseHandler:" + this.callback;
options.url += ";reqId:0;out:json&tq&_=1335871249558#";

delete options.sheetName;

} else {
options.url = "https://spreadsheets.google.com/feeds/cells/" +
options.key + "/" +
Expand All @@ -58,6 +55,7 @@
}
}


this.params = {
type : "GET",
url : options.url,
Expand Down
2 changes: 1 addition & 1 deletion src/importers/remote.js
Expand Up @@ -194,7 +194,7 @@

script.onerror = function(e) {
if (error) {
error.call(null);
error.call(null, e);
}
};

Expand Down
7 changes: 7 additions & 0 deletions src/parsers/delimited.js
Expand Up @@ -166,6 +166,13 @@
columnData[columns[columnIndex]].push(strMatchedValue);

} else {

// first check to see if there already is a column by this name
// if so, throw an error
if (columns.indexOf(strMatchedValue) !== -1) {
throw new Error("You have more than one column named " + strMatchedValue);
}

// we are building the column names here
columns.push(strMatchedValue);
columnData[strMatchedValue] = [];
Expand Down
40 changes: 33 additions & 7 deletions src/parsers/google_spreadsheet.js
Expand Up @@ -22,11 +22,32 @@
keyedData = {},
i;

// the fast importer API is not available
if (typeof data.status !== "undefined" && data.status === "error") {
throw new Error("You can't use the fast importer for this url. Disable the fast flag");
}

if (this.fast) {

// init column names
columns = _.pluck(data.table.cols, "label");

// check that the column names don't have duplicates
if (_.unique(columns).length < columns.length) {
var dup = "";

_.inject(columns, function(memo, val) {

memo[val] = (memo[val] + 1) || 1;
if (memo[val] > 1) {
dup = val;
}
return memo;
}, {});

throw new Error("You have more than one column named \"" + dup + "\"");
}

// save data
_.each(data.table.rows, function(row) {
row = row.c;
Expand Down Expand Up @@ -55,15 +76,20 @@
column = parts[1],
position = parseInt(parts[2], 10);

if (_.isUndefined(columnPositions[column])) {

// cache the column position
columnPositions[column] = columnData.length;
// this is the first row, thus column names.
if (position === 1) {

// we found a new column, so build a new column type.
columns[columnPositions[column]] = cell.content.$t;
columnData[columnPositions[column]] = [];
// if we've already seen this column name, throw an exception
if (columns.indexOf(cell.content.$t) !== -1) {
throw new Error("You have more than one column named \"" + cell.content.$t + "\"");
} else {
// cache the column position
columnPositions[column] = columnData.length;

// we found a new column, so build a new column type.
columns[columnPositions[column]] = cell.content.$t;
columnData[columnPositions[column]] = [];
}

} else {

Expand Down
8 changes: 6 additions & 2 deletions src/parsers/strict.js
Expand Up @@ -22,8 +22,12 @@
var columnData = {}, columnNames = [];

_.each(data.columns, function(column) {
columnNames.push( column.name );
columnData[ column.name ] = column.data;
if (columnNames.indexOf(column.name) !== -1) {
throw new Error("You have more than one column named \"" + column.name + "\"");
} else {
columnNames.push( column.name );
columnData[ column.name ] = column.data;
}
});

return {
Expand Down
5 changes: 5 additions & 0 deletions test/data/withzeros.csv
@@ -0,0 +1,5 @@
a,b,c
0,0,1
0,2,3
4,5,6
3,0,1
1 change: 1 addition & 0 deletions test/index.html
Expand Up @@ -56,6 +56,7 @@
<script src="unit/products.js"></script>
<script src="unit/speed.js"></script>
<script src="unit/derived.js"></script>
<script src="unit/bugs.js"></script>

</head>
<body>
Expand Down

0 comments on commit cd83b79

Please sign in to comment.