From 05a02765d0482d4d09a73fc9bb95fdb95efc2225 Mon Sep 17 00:00:00 2001 From: abdennour Date: Fri, 25 Nov 2016 16:48:16 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20still=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 ++ src/core.js | 53 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index c391b7c..8d3185e 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,9 @@ "babel-preset-es2015": "^6.18.0", "babel-preset-react": "^6.16.0", "babel-preset-stage-0": "^6.16.0", + "coveralls": "^2.11.15", "enzyme": "^2.6.0", + "mocha-lcov-reporter": "^1.2.0", "react": "^15.4.1" }, "dependencies": { diff --git a/src/core.js b/src/core.js index 74a3da9..77f6c8e 100644 --- a/src/core.js +++ b/src/core.js @@ -1,26 +1,47 @@ -const isJsons = (array) => array.every(row => (typeof row === 'object' && !(row instanceof Array))) -const isArrays = (array) => array.every(row => Array.isArray(row)) +export const isJsons = ((array) => array.every( + row => (typeof row === 'object' && !(row instanceof Array)) +)); -const jsonsHeaders = (array) => Array.from( +export const isArrays = ((array) => array.every( + row => Array.isArray(row) +)); + +export const jsonsHeaders = ((array) => Array.from( array.map(json => Object.keys(json)) .reduce((a, b) => new Set([...a, ...b]), []) -); -const jsons2arrays = (jsons, headers) => { +)); + +export const jsons2arrays = (jsons, headers) => { headers = headers || jsonsHeaders(jsons); return [headers, ...array.map((object) => headers.map((header) => object[header] ? object[header] : ''))] }; -const joiner = (data) => data.map((row, index) => row.join(',')).join(`\n`); - -const arrays2csv = (data, headers) => joiner(headers ? [headers, ...data] : data); -const jsons2csv = (data, headers) => joiner(jsons2arrays(data, headers)) -const toCSV = (data, headers) => { - if(isJsons(data)) return jsons2csv(data, headers); - if(isArrays(data)) return jsons2csv(data, headers); - throw new TypeError(`Data should be an array of arrays OR array of objects `); -}; -export const buildURI = (data, headers) => encodeURI( -`data:text/csv;charset=utf-8,${toCSV(data, headers)}` +export const joiner = ((data) => + data.map((row, index) => row.join(',')).join(`\n`) +); + +export const arrays2csv = ((data, headers) => + joiner(headers ? [headers, ...data] : data) +); + +export const jsons2csv = ((data, headers) => + joiner(jsons2arrays(data, headers)) +); + +export const string2csv = ((data, headers) => + (headers) ? `${headers.join(`,`)}\n${data}`: data +) + +export const toCSV = (data, headers) => { + if (isJsons(data)) return jsons2csv(data, headers); + if (isArrays(data)) return arrays2csv(data, headers); + if (typeof data ==='string') return string2csv(data, headers); + throw new TypeError(`Data should be a "String", "Array of arrays" OR "Array of objects" `); +}; + +export const buildURI = ((data, headers) => encodeURI( + `data:text/csv;charset=utf-8,${toCSV(data, headers)}` + ) );