Skip to content
This repository has been archived by the owner on Jan 9, 2018. It is now read-only.

Commit

Permalink
Add option preventCast.
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltask committed Mar 21, 2012
1 parent 1a35458 commit 65c6dd8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.DS_Store
node_modules
npm-debug.log
1 change: 1 addition & 0 deletions .npmignore
@@ -1,2 +1,3 @@
test
node_modules
npm-debug.log
9 changes: 9 additions & 0 deletions lib/express-csv.js
Expand Up @@ -29,6 +29,12 @@ exports.version = package.version;

exports.separator = ',';

/**
* Prevent Excel's casting.
*/

exports.preventCast = false;

/**
* Escape CSV field
*
Expand All @@ -38,6 +44,9 @@ exports.separator = ',';
*/

function escape(field) {
if (exports.preventCast) {
return '"="' + field.toString().replace(/\"/g, '""') + '""';
}
return '"' + field.toString().replace(/\"/g, '""') + '"';
}

Expand Down
22 changes: 20 additions & 2 deletions test/index.js
Expand Up @@ -20,12 +20,18 @@ describe('express-csv', function() {
it('should expose .separator', function() {
csv.separator.should.be.a('string');
});

it('should expose .preventCast', function() {
csv.preventCast.should.be.a('boolean');
});

it('should extend http.ServerResponse.prototype.csv', function() {
require('http').ServerResponse.prototype.csv.should.be.a('function');
});
});

it('should return csv', function(done) {
describe('res.csv()', function() {
it('should response csv', function(done) {
request
.get('http://127.0.0.1:8383/test/1')
.end(function(res) {
Expand All @@ -34,7 +40,7 @@ describe('express-csv', function() {
});
});

it('should return tsv', function(done) {
it('should response tsv', function(done) {
var prevSeparator = csv.separator;
csv.separator = '\t';
request
Expand All @@ -45,4 +51,16 @@ describe('express-csv', function() {
done();
});
});

it('should response casted csv', function(done) {
var prevSetting = csv.preventCast;
csv.preventCast = true;
request
.get('http://127.0.0.1:8383/test/1')
.end(function(res) {
csv.preventCast = prevSetting;
res.text.should.equal('"="a"","="b"","="c""\r\n"="d"","="e"","="f""\r\n');
done();
});
});
});

0 comments on commit 65c6dd8

Please sign in to comment.