2018-09-03 07:54:45 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
|
|
import { saveAs } from 'browser-filesaver';
|
|
|
|
|
|
|
|
function getCSVHeaders( headers ) {
|
|
|
|
return Array.isArray( headers ) ? headers.map( header => header.label ).join( ',' ) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCSVRows( rows ) {
|
|
|
|
return Array.isArray( rows )
|
2018-10-11 08:30:51 +00:00
|
|
|
? rows
|
|
|
|
.map( row =>
|
2019-02-22 02:02:38 +00:00
|
|
|
row.map( rowItem =>
|
|
|
|
rowItem.value !== undefined && rowItem.value !== null ? rowItem.value.toString().replace( /,/g, ''
|
|
|
|
) : '' ).join( ',' )
|
2018-10-11 08:30:51 +00:00
|
|
|
)
|
|
|
|
.join( '\n' )
|
2018-09-03 07:54:45 +00:00
|
|
|
: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a CSV string from table contents
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>} headers Object with table header information
|
|
|
|
* @param {Array.Array.<Object>} rows Object with table rows information
|
|
|
|
* @returns {String} Table contents in a CSV format
|
|
|
|
*/
|
|
|
|
export function generateCSVDataFromTable( headers, rows ) {
|
|
|
|
return [ getCSVHeaders( headers ), getCSVRows( rows ) ]
|
|
|
|
.filter( text => text.length )
|
|
|
|
.join( '\n' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a file name for CSV files based on the provided name, the current date
|
|
|
|
* and the provided params, which are all appended with hyphens.
|
|
|
|
*
|
|
|
|
* @param {String} [name=''] Name of the file
|
|
|
|
* @param {Object} [params={}] Object of key-values to append to the file name
|
|
|
|
* @returns {String} Formatted file name
|
|
|
|
*/
|
|
|
|
export function generateCSVFileName( name = '', params = {} ) {
|
|
|
|
const fileNameSections = [
|
2018-10-24 07:48:20 +00:00
|
|
|
name.toLowerCase().replace( ' ', '-' ),
|
2018-09-03 07:54:45 +00:00
|
|
|
moment().format( 'YYYY-MM-DD' ),
|
|
|
|
Object.keys( params )
|
|
|
|
.map( key => key + '-' + params[ key ] )
|
|
|
|
.join( '-' ),
|
|
|
|
].filter( text => text.length );
|
|
|
|
|
|
|
|
return fileNameSections.join( '-' ) + '.csv';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads a CSV file with the given file name and contents
|
|
|
|
*
|
|
|
|
* @param {String} fileName Name of the file to download
|
|
|
|
* @param {String} content Contents of the file to download
|
|
|
|
*/
|
|
|
|
export function downloadCSVFile( fileName, content ) {
|
|
|
|
const blob = new Blob( [ content ], { type: 'text/csv;charset=utf-8' } );
|
|
|
|
|
|
|
|
saveAs( blob, fileName );
|
|
|
|
}
|