| 1 |
import isPlainObject from 'is-plain-object'; |
| 2 |
|
| 3 |
import deepFilter from 'deep-filter'; |
| 4 |
|
| 5 |
// Format Date of Chart Data |
| 6 |
export const formatDate = ( data ) => { |
| 7 |
Object.keys( data['visualizer-series']).map( i => { |
| 8 |
if ( data['visualizer-series'][i].type !== undefined && 'date' === data['visualizer-series'][i].type ) { |
| 9 |
Object.keys( data['visualizer-data']).map( o => { |
| 10 |
return data['visualizer-data'][o][i] = new Date( data['visualizer-data'][o][i]); |
| 11 |
}); |
| 12 |
} |
| 13 |
}); |
| 14 |
return data; |
| 15 |
}; |
| 16 |
|
| 17 |
// A fork of deep-compact package as it had some issues |
| 18 |
const notEmpty = value => { |
| 19 |
let key; |
| 20 |
|
| 21 |
if ( Array.isArray( value ) ) { |
| 22 |
return 0 < value.length; |
| 23 |
} |
| 24 |
|
| 25 |
if ( isPlainObject( value ) ) { |
| 26 |
for ( key in value ) { |
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
if ( 'string' === typeof value ) { |
| 34 |
return 0 < value.length; |
| 35 |
} |
| 36 |
|
| 37 |
return null != value; |
| 38 |
}; |
| 39 |
|
| 40 |
export const compact = value => deepFilter( value, notEmpty ); |
| 41 |
|
| 42 |
// Remove chart size-related properies for Chart List |
| 43 |
export const filterCharts = value => { |
| 44 |
value.width = ''; |
| 45 |
value.height = ''; |
| 46 |
value.backgroundColor = {}; |
| 47 |
value.chartArea = {}; |
| 48 |
|
| 49 |
return compact( value, notEmpty ); |
| 50 |
}; |
| 51 |
|
| 52 |
// Check if JSON object is valid or not |
| 53 |
export const isValidJSON = obj => { |
| 54 |
try { |
| 55 |
JSON.parse( obj ); |
| 56 |
} catch ( e ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
return true; |
| 60 |
}; |
| 61 |
|
| 62 |
// Convert CSV data to Array |
| 63 |
// Source: https://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm |
| 64 |
export const CSVToArray = ( strData, strDelimiter ) => { |
| 65 |
strDelimiter = ( strDelimiter || ',' ); |
| 66 |
|
| 67 |
const objPattern = new RegExp( |
| 68 |
( '(\\' + strDelimiter + '|\\r?\\n|\\r|^)' + '(?:\'([^\']*(?:\'\'[^\']*)*)\'|' + '([^\'\\' + strDelimiter + '\\r\\n]*))' ), 'gi' ); |
| 69 |
|
| 70 |
const arrData = [ [] ]; |
| 71 |
|
| 72 |
let arrMatches = null; |
| 73 |
|
| 74 |
while ( arrMatches = objPattern.exec( strData ) ) { |
| 75 |
|
| 76 |
const strMatchedDelimiter = arrMatches[ 1 ]; |
| 77 |
|
| 78 |
if ( strMatchedDelimiter.length && strMatchedDelimiter !== strDelimiter ) { |
| 79 |
arrData.push([]); |
| 80 |
} |
| 81 |
|
| 82 |
let strMatchedValue; |
| 83 |
|
| 84 |
if ( arrMatches[ 2 ]) { |
| 85 |
strMatchedValue = arrMatches[ 2 ].replace( new RegExp( '\'\'', 'g' ), '\'' ); |
| 86 |
} else { |
| 87 |
strMatchedValue = arrMatches[ 3 ]; |
| 88 |
} |
| 89 |
|
| 90 |
arrData[ arrData.length - 1 ].push( strMatchedValue ); |
| 91 |
} |
| 92 |
|
| 93 |
return ( arrData ); |
| 94 |
}; |
| 95 |
|