initial commit
This commit is contained in:
33
lists/csv.js
Normal file
33
lists/csv.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Generates a CSV from all the rows in the view.
|
||||
*
|
||||
* Takes in a url encoded array of headers as an argument. You can
|
||||
* generate this by querying /_list/urlencode/headers. Pass it in
|
||||
* as the headers get parameter, e.g.: ?headers=%5B%22_id%22%2C%22_rev%5D
|
||||
*
|
||||
* @author Max Ogden
|
||||
*/
|
||||
function(head, req) {
|
||||
if ('headers' in req.query) {
|
||||
var headers = eval(unescape(req.query.headers.split(',')));
|
||||
var row, sep = '\n', headerSent = false, startedOutput = false;
|
||||
|
||||
start({"headers":{"Content-Type" : "text/x-csv"}});
|
||||
send(headers.join(',') + "\n");
|
||||
while (row = getRow()) {
|
||||
for (var header in headers) {
|
||||
if (row.value[headers[header]]) {
|
||||
if (startedOutput) send(",");
|
||||
send("\"" + row.value[headers[header]] + "\"");
|
||||
} else {
|
||||
if (startedOutput) send(",");
|
||||
}
|
||||
startedOutput = true;
|
||||
}
|
||||
startedOutput = false;
|
||||
send('\n');
|
||||
}
|
||||
} else {
|
||||
send("You must pass in the urlencoded headers you wish to build the CSV from. Query /_list/urlencode/headers");
|
||||
}
|
||||
};
|
||||
20
lists/urlencode.js
Normal file
20
lists/urlencode.js
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Returns the urlencoded version of the view value
|
||||
*
|
||||
* @author Max Ogden
|
||||
*/
|
||||
function(head, req) {
|
||||
// Send the same Content-Type as CouchDB would
|
||||
if (req.headers.Accept.indexOf('application/json')!=-1)
|
||||
start({"headers":{"Content-Type" : "application/json"}});
|
||||
else
|
||||
start({"headers":{"Content-Type" : "text/plain"}});
|
||||
|
||||
if ('callback' in req.query) send(req.query['callback'] + "(");
|
||||
|
||||
while (row = getRow()) {
|
||||
send(escape(JSON.stringify(row.value)));
|
||||
}
|
||||
|
||||
if ('callback' in req.query) send(")");
|
||||
};
|
||||
Reference in New Issue
Block a user