From 7443e9270f7e5eafb949a0e6edc7ec6148a2b669 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 6 Jul 2018 20:25:46 +0300 Subject: [PATCH] Add JSONtoCSV Resolve #686 --- snippets/JSONtoCSV.md | 26 ++++++++++++++++++++++++++ snippets/arrayToCSV.md | 4 ++-- tag_database | 1 + test/JSONtoCSV/JSONtoCSV.js | 11 +++++++++++ test/JSONtoCSV/JSONtoCSV.test.js | 12 ++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 snippets/JSONtoCSV.md create mode 100644 test/JSONtoCSV/JSONtoCSV.js create mode 100644 test/JSONtoCSV/JSONtoCSV.test.js diff --git a/snippets/JSONtoCSV.md b/snippets/JSONtoCSV.md new file mode 100644 index 000000000..fac5f6946 --- /dev/null +++ b/snippets/JSONtoCSV.md @@ -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"' +``` diff --git a/snippets/arrayToCSV.md b/snippets/arrayToCSV.md index eecce9d99..1d65c7adf 100644 --- a/snippets/arrayToCSV.md +++ b/snippets/arrayToCSV.md @@ -2,8 +2,8 @@ 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 `String.join('\n')` to combine all rows into a CSV string, separating each row with a newline. +Use `Array.map()` and `Array.join(delimiter)` to combine individual 1D arrays (rows) into strings. +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 `,`. ```js diff --git a/tag_database b/tag_database index 7663809dc..a2b57d852 100644 --- a/tag_database +++ b/tag_database @@ -152,6 +152,7 @@ isUndefined:type isUpperCase:string,utility isValidJSON:type,json join:array +JSONtoCSV:array,string,object,advanced JSONToFile:node,json last:array lcm:math,recursion diff --git a/test/JSONtoCSV/JSONtoCSV.js b/test/JSONtoCSV/JSONtoCSV.js new file mode 100644 index 000000000..912f55338 --- /dev/null +++ b/test/JSONtoCSV/JSONtoCSV.js @@ -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; \ No newline at end of file diff --git a/test/JSONtoCSV/JSONtoCSV.test.js b/test/JSONtoCSV/JSONtoCSV.test.js new file mode 100644 index 000000000..36ec9ec2d --- /dev/null +++ b/test/JSONtoCSV/JSONtoCSV.test.js @@ -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"'); +});