Add JSONtoCSV

Resolve #686
This commit is contained in:
Angelos Chalaris
2018-07-06 20:25:46 +03:00
parent bdf9ec0b74
commit 5e0b1e113b
5 changed files with 52 additions and 2 deletions

26
snippets/JSONtoCSV.md Normal file
View File

@ -0,0 +1,26 @@
### JSONtoCSV
Converts an array of objects to a comma-separated values (CSV) string that contains only the `columns` specified.
Use `Array.join(demiliter)` to combine all the names in `columns` to create the first row.
Use `Array.map()` and `Array.reduce()` to create a row for each object, substituting non-existent values with empty strings and only mapping values in `columns`.
Use `Array.join('\n')` to combine all rows into a string.
Omit the third argument, `delimiter`, to use a default delimiter of `,`.
```js
const JSONtoCSV = (arr, columns, delimiter = ',') =>
[
columns.join(delimiter),
...arr.map(obj =>
columns.reduce(
(acc, key) => `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`,
''
)
)
].join('\n');
```
```js
JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b']); // 'a,b\n"1","2"\n"3","4"\n"6",""\n"","7"'
JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b'], ';'); // 'a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"'
```

View File

@ -2,8 +2,8 @@
Converts a 2D array to a comma-separated values (CSV) string. Converts a 2D array to a comma-separated values (CSV) string.
Use `Array.map()` and `String.join(delimiter)` to combine individual 1D arrays (rows) into strings. Use `Array.map()` and `Array.join(delimiter)` to combine individual 1D arrays (rows) into strings.
Use `String.join('\n')` to combine all rows into a CSV string, separating each row with a newline. Use `Array.join('\n')` to combine all rows into a CSV string, separating each row with a newline.
Omit the second argument, `delimiter`, to use a default delimiter of `,`. Omit the second argument, `delimiter`, to use a default delimiter of `,`.
```js ```js

View File

@ -152,6 +152,7 @@ isUndefined:type
isUpperCase:string,utility isUpperCase:string,utility
isValidJSON:type,json isValidJSON:type,json
join:array join:array
JSONtoCSV:array,string,object,advanced
JSONToFile:node,json JSONToFile:node,json
last:array last:array
lcm:math,recursion lcm:math,recursion

View File

@ -0,0 +1,11 @@
const JSONtoCSV = (arr, columns, delimiter = ',') =>
[
columns.join(delimiter),
...arr.map(obj =>
columns.reduce(
(acc, key) => `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`,
''
)
)
].join('\n');
module.exports = JSONtoCSV;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const JSONtoCSV = require('./JSONtoCSV.js');
test('JSONtoCSV is a Function', () => {
expect(JSONtoCSV).toBeInstanceOf(Function);
});
test('JSONtoCSV works with default delimiter', () => {
expect(JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b'])).toBe('a,b\n"1","2"\n"3","4"\n"6",""\n"","7"');
});
test('JSONtoCSV works with custom delimiter', () => {
expect(JSONtoCSV([{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }], ['a', 'b'], ';')).toBe('a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"');
});