ceseve
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -1,6 +0,6 @@ | ||
function csv(data) { | ||
function csv(data, separator = ',') { | ||
const items = Object.keys(data[0]); | ||
let result = ''; | ||
result += items.join(','); | ||
result += items.join(separator); | ||
result += '\n'; | ||
@@ -11,3 +11,3 @@ | ||
items.forEach((item) => { | ||
if (ctr > 0) result += ','; | ||
if (ctr > 0) result += separator; | ||
@@ -14,0 +14,0 @@ // If the field type is a String, should be to include "" |
{ | ||
"name": "ceseve", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Convert arrays of data to CSV files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,4 +17,4 @@ [![Build Status](https://travis-ci.org/carlosazaustre/ceseve.svg?branch=master)](https://travis-ci.org/carlosazaustre/ceseve) | ||
const data = [ | ||
{ foo: "bar", choo: "moo" }, | ||
{ foo: "ber", choo: "muu" } | ||
{ title: "foo", property: "bar" }, | ||
{ title: "foo_2", property: "bar_2" } | ||
]; | ||
@@ -24,7 +24,18 @@ | ||
// csv: | ||
// foo,moo | ||
// ber,muu | ||
// title,property | ||
// foo,bar | ||
// foo_2,bar_2 | ||
``` | ||
> You can specify separator character. Default is comma `,` | ||
```js | ||
const csvDocument = csv(data, ';'); | ||
// csv: | ||
// title;property | ||
// foo;bar | ||
// foo_2;bar_2 | ||
``` | ||
## License | ||
MIT © [Carlos Azaustre](https://carlosazaustre.es) |
const csv = require('../lib/Csv'); | ||
const mockData = [ | ||
{ foo: 'bar', choo: 'moo' }, | ||
{ foo: 'ber', choo: 'muu' }, | ||
]; | ||
test('Should return a csv format from an array of object', () => { | ||
const data = [ | ||
{ foo: 'bar', choo: 'moo' }, | ||
{ foo: 'ber', choo: 'muu' }, | ||
]; | ||
const result = 'foo,choo\nbar,moo\nber,muu\n'; | ||
expect(csv(mockData)).toBe(result); | ||
}); | ||
expect(csv(data)).toBe(result); | ||
test('Should return a csv splitted by semicolons', () => { | ||
const result = 'foo;choo\nbar;moo\nber;muu\n'; | ||
expect(csv(mockData, ';')).toBe(result); | ||
}); |
5081
53
40