diff --git a/History.md b/History.md
index a7088cbd..5f5b100c 100644
--- a/History.md
+++ b/History.md
@@ -1,3 +1,7 @@
+# v2.3.0
+
+* Writing object ends up with function definitions [#158](https://github.com/C2FO/fast-csv/pull/158)
+
# v2.2.0
* Handle split CRLF [#156](https://github.com/C2FO/fast-csv/pull/156) - [@alexeits](https://github.com/alexeits)
diff --git a/docs/History.html b/docs/History.html
index 2b1a850e..cc2f2dd6 100644
--- a/docs/History.html
+++ b/docs/History.html
@@ -176,6 +176,10 @@
+
v2.3.0
+
+- Writing object ends up with function definitions #158
+
v2.2.0
- Handle split CRLF #156 - @alexeits
diff --git a/lib/extended.js b/lib/extended.js
index 6c304446..46d199f5 100644
--- a/lib/extended.js
+++ b/lib/extended.js
@@ -1,4 +1,5 @@
-var is = require("is-extended");
+var is = require("is-extended"),
+ hasOwn = Object.prototype.hasOwnProperty;
module.exports = require("extended")()
.register(is)
.register(require("object-extended"))
@@ -51,8 +52,8 @@ module.exports = require("extended")()
})
.register("keys", function (obj) {
var ret = [];
- if (is.isObject(obj)) {
- for (var i in obj) {
+ for (var i in obj) {
+ if (hasOwn.call(obj, i)) {
ret.push(i);
}
}
diff --git a/package.json b/package.json
index aae99674..edcbfaf8 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "fast-csv",
- "version": "2.2.0",
+ "version": "2.3.0",
"description": "CSV parser and writer",
"main": "index.js",
"scripts": {
diff --git a/test/issues.test.js b/test/issues.test.js
index 91f28217..6fe5394a 100644
--- a/test/issues.test.js
+++ b/test/issues.test.js
@@ -17,7 +17,7 @@ it.describe("github issues", function (it) {
it.should("handle bubble up parse errors properly", function (next) {
var d = domain.create(), called = false;
d.on("error", function (err) {
- if(!called) {
+ if (!called) {
called = true;
assert.equal(/^Parse Error/.test(err.message), true);
next();
@@ -40,11 +40,11 @@ it.describe("github issues", function (it) {
it.should("handle bubble up data errors properly", function (next) {
var d = domain.create(), called = false;
d.on("error", function (err) {
- if(!called) {
+ if (!called) {
called = true;
assert.equal(err.message, "Data error");
next();
- }else{
+ } else {
throw err;
}
});
@@ -148,11 +148,11 @@ it.describe("github issues", function (it) {
it.should("handle bubble up errors thrown in end properly", function (next) {
var d = domain.create(), called = false;
d.on("error", function (err) {
- if(!called) {
+ if (!called) {
called = true;
assert.equal(err.message, "End error");
next();
- }else{
+ } else {
throw err;
}
});
@@ -160,10 +160,11 @@ it.describe("github issues", function (it) {
var actual = [];
csv
.fromPath(path.resolve(__dirname, "./assets/issue93.csv"), {headers: true, delimiter: "\t"})
- .on("error", function(){
+ .on("error", function () {
next(new Error("Should not get here!"));
})
- .on("data", function (data) {})
+ .on("data", function (data) {
+ })
.on("end", function () {
throw new Error("End error");
});
@@ -173,42 +174,42 @@ it.describe("github issues", function (it) {
it.describe("#111", function (it) {
- it.should("parse a block of CSV text with a trailing delimiter", function () {
- var data = "first_name,last_name,email_address,empty\nFirst1,Last1,email1@email.com,\n";
- var myParser = parser({delimiter: ","});
- assert.deepEqual(myParser(data, false), {
- "line": "", "rows": [
- ["first_name", "last_name", "email_address", "empty"],
- ["First1", "Last1", "email1@email.com", ""]
- ]
- });
+ it.should("parse a block of CSV text with a trailing delimiter", function () {
+ var data = "first_name,last_name,email_address,empty\nFirst1,Last1,email1@email.com,\n";
+ var myParser = parser({delimiter: ","});
+ assert.deepEqual(myParser(data, false), {
+ "line": "", "rows": [
+ ["first_name", "last_name", "email_address", "empty"],
+ ["First1", "Last1", "email1@email.com", ""]
+ ]
+ });
});
- it.should("parse a block of CSV text with a delimiter at file end", function () {
- var data = "first_name,last_name,email_address,empty\nFirst1,Last1,email1@email.com,";
- var myParser = parser({delimiter: ","});
- assert.deepEqual(myParser(data, false), {
- "line": "", "rows": [
- ["first_name", "last_name", "email_address", "empty"],
- ["First1", "Last1", "email1@email.com", ""]
- ]
- });
+ it.should("parse a block of CSV text with a delimiter at file end", function () {
+ var data = "first_name,last_name,email_address,empty\nFirst1,Last1,email1@email.com,";
+ var myParser = parser({delimiter: ","});
+ assert.deepEqual(myParser(data, false), {
+ "line": "", "rows": [
+ ["first_name", "last_name", "email_address", "empty"],
+ ["First1", "Last1", "email1@email.com", ""]
+ ]
+ });
});
- it.should("parse a block of CSV text with two delimiters at file end", function () {
- var data = "first_name,last_name,email_address,empty1,empty2\nFirst1,Last1,email1@email.com,,";
- var myParser = parser({delimiter: ","});
- assert.deepEqual(myParser(data, false), {
- "line": "", "rows": [
- ["first_name", "last_name", "email_address", "empty1", "empty2"],
- ["First1", "Last1", "email1@email.com", "", ""]
- ]
- });
+ it.should("parse a block of CSV text with two delimiters at file end", function () {
+ var data = "first_name,last_name,email_address,empty1,empty2\nFirst1,Last1,email1@email.com,,";
+ var myParser = parser({delimiter: ","});
+ assert.deepEqual(myParser(data, false), {
+ "line": "", "rows": [
+ ["first_name", "last_name", "email_address", "empty1", "empty2"],
+ ["First1", "Last1", "email1@email.com", "", ""]
+ ]
+ });
});
- it.should("parse a block of CSV text with a trailing delimiter followed by a space", function() {
+ it.should("parse a block of CSV text with a trailing delimiter followed by a space", function () {
var data = "first_name,last_name,email_address,empty\nFirst1,Last1,email1@email.com, \n";
- var myParser = parser({ delimiter: "," });
+ var myParser = parser({delimiter: ","});
assert.deepEqual(myParser(data, false), {
"line": "", "rows": [
["first_name", "last_name", "email_address", "empty"],
@@ -217,20 +218,20 @@ it.describe("github issues", function (it) {
});
});
- it.should("parse a block of Space Separated Value text with a trailing delimiter", function() {
+ it.should("parse a block of Space Separated Value text with a trailing delimiter", function () {
var data = "first_name last_name email_address empty\nFirst1 Last1 email1@email.com \n";
- var myParser = parser({ delimiter: " " });
+ var myParser = parser({delimiter: " "});
assert.deepEqual(myParser(data, false), {
"line": "", "rows": [
["first_name", "last_name", "email_address", "empty"],
["First1", "Last1", "email1@email.com", ""]
]
});
- });
+ });
- it.should("parse a block of Space Separated Values with two delimiters at file end", function() {
+ it.should("parse a block of Space Separated Values with two delimiters at file end", function () {
var data = "first_name last_name email_address empty empty2\nFirst1 Last1 email1@email.com \n";
- var myParser = parser({ delimiter: " " });
+ var myParser = parser({delimiter: " "});
assert.deepEqual(myParser(data, false), {
"line": "", "rows": [
["first_name", "last_name", "email_address", "empty", "empty2"],
@@ -251,4 +252,35 @@ it.describe("github issues", function (it) {
});
});
});
-});
+
+ it.describe("#158", function (it) {
+ function Place(id, name) {
+ this.id = id;
+ this.name = name;
+ this.calculatedValue = 0;
+ }
+
+ Place.prototype.calculateSomething = function () {
+ this.calculatedValue = this.id * 2;
+ return this;
+ };
+
+ it.should("not write prototype methods in csv", function (next) {
+ var ws = new stream.Writable(), written = [];
+ ws._write = function (data, enc, cb) {
+ written.push(data + "");
+ cb();
+ };
+ ws.on("finish", function () {
+ assert.deepEqual(written.join(""), "id,name,calculatedValue\n1,a,2\n2,b,4\n3,c,6");
+ next();
+ });
+ csv.writeToStream(ws, [
+ new Place(1, "a").calculateSomething(),
+ new Place(2, "b").calculateSomething(),
+ new Place(3, "c").calculateSomething()
+ ], {headers: true}).on("error", next);
+ });
+
+ });
+});
\ No newline at end of file