Repository: mholt/PapaParse (js)

Issue:
'columns' unparse config not working when first object doesn't have all properties
When using the `columns` unparse config option together with a data array where the first object does not have all properties that are listed in the `columns` array, then not all columns of the `columns` array are used even if later objects have these values.

E.g.
```javascript
const columns= ["First", "Second", "Third"];
const rows= [
  { First: "1-1"},
  {
    First: "2-1",
    Second: "2-2",
    "Third": "2-3",
  },
];
const result = Papa.unparse(
  { data: rows},
  {
    header: true,
    columns: columns,
  }
);
console.log(result)
```
The `result` is 
```
First
1-1
2-1
```

instead of the expected

```
First,Second,Third
1-1,,
2-1,2-2,2-3
```

Pull request:
fix: columns config works with input type object
Closes #918

Patch:
diff --git a/papaparse.js b/papaparse.js
index df7ba06..667a109 100755
--- a/papaparse.js
+++ b/papaparse.js
@@ -307,7 +307,7 @@ License: MIT
 			if (Array.isArray(_input.data))
 			{
 				if (!_input.fields)
-					_input.fields =  _input.meta && _input.meta.fields;
+					_input.fields = _input.meta && _input.meta.fields || _columns;
 
 				if (!_input.fields)
 					_input.fields =  Array.isArray(_input.data[0])


Test patch:
diff --git a/tests/test-cases.js b/tests/test-cases.js
index ab806ba..469c74c 100644
--- a/tests/test-cases.js
+++ b/tests/test-cases.js
@@ -1865,6 +1865,13 @@ var UNPARSE_TESTS = [
 		config: {columns: ['a', 'b', 'c']},
 		expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4'
 	},
+	{
+		description: "Column option used to manually specify keys with input type object",
+		notes: "Should not throw any error when attempting to serialize key not present in object. Columns are different than keys of the first object. When an object is missing a key then the serialized value should be an empty string.",
+		input: { data: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}] },
+		config: {columns: ['a', 'b', 'c']},
+		expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4'
+	},
 	{
 		description: "Use different escapeChar",
 		input: [{a: 'foo', b: '"quoted"'}],