| 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 |
// NOTE: This method is likely to create problems. |
| 19 |
// Problem Scenario #1: |
| 20 |
// - A table has 5 columns (series). Say the 1st column is Date and others are Numbers. |
| 21 |
// - If the 1st columns format (series.format) is provided, DataTable.js gets 6 (0-5) series. |
| 22 |
// - BUT if the 1st columns format (series.format) is empty, DataTable.js gets 5 (1-4) series. |
| 23 |
// That is why when sending options to DataTable.js, filterChart method has not been used. |
| 24 |
const notEmpty = value => { |
| 25 |
let key; |
| 26 |
|
| 27 |
if ( Array.isArray( value ) ) { |
| 28 |
return 0 < value.length; |
| 29 |
} |
| 30 |
|
| 31 |
if ( isPlainObject( value ) ) { |
| 32 |
for ( key in value ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
if ( 'string' === typeof value ) { |
| 40 |
return 0 < value.length; |
| 41 |
} |
| 42 |
|
| 43 |
return null != value; |
| 44 |
}; |
| 45 |
|
| 46 |
export const compact = value => deepFilter( value, notEmpty ); |
| 47 |
|
| 48 |
// Remove chart size-related properies for Chart List |
| 49 |
export const filterCharts = value => { |
| 50 |
value.width = ''; |
| 51 |
value.height = ''; |
| 52 |
value.backgroundColor = {}; |
| 53 |
value.chartArea = {}; |
| 54 |
|
| 55 |
return compact( value, notEmpty ); |
| 56 |
}; |
| 57 |
|
| 58 |
// Check if JSON object is valid or not |
| 59 |
export const isValidJSON = obj => { |
| 60 |
try { |
| 61 |
JSON.parse( obj ); |
| 62 |
} catch ( e ) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
return true; |
| 66 |
}; |
| 67 |
|
| 68 |
// Convert CSV data to Array |
| 69 |
// Source: https://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm |
| 70 |
export const CSVToArray = ( strData, strDelimiter ) => { |
| 71 |
strDelimiter = ( strDelimiter || ',' ); |
| 72 |
|
| 73 |
const objPattern = new RegExp( |
| 74 |
( '(\\' + strDelimiter + '|\\r?\\n|\\r|^)' + '(?:\'([^\']*(?:\'\'[^\']*)*)\'|' + '([^\'\\' + strDelimiter + '\\r\\n]*))' ), 'gi' ); |
| 75 |
|
| 76 |
const arrData = [ [] ]; |
| 77 |
|
| 78 |
let arrMatches = null; |
| 79 |
|
| 80 |
while ( arrMatches = objPattern.exec( strData ) ) { |
| 81 |
|
| 82 |
const strMatchedDelimiter = arrMatches[ 1 ]; |
| 83 |
|
| 84 |
if ( strMatchedDelimiter.length && strMatchedDelimiter !== strDelimiter ) { |
| 85 |
arrData.push([]); |
| 86 |
} |
| 87 |
|
| 88 |
let strMatchedValue; |
| 89 |
|
| 90 |
if ( arrMatches[ 2 ]) { |
| 91 |
strMatchedValue = arrMatches[ 2 ].replace( new RegExp( '\'\'', 'g' ), '\'' ); |
| 92 |
} else { |
| 93 |
strMatchedValue = arrMatches[ 3 ]; |
| 94 |
} |
| 95 |
|
| 96 |
arrData[ arrData.length - 1 ].push( strMatchedValue ); |
| 97 |
} |
| 98 |
|
| 99 |
return ( arrData ); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
export const isChecked = ( settings, param ) => { |
| 104 |
return true === settings[param] || 'true' === settings[param] || '1' === settings[param] || 1 === settings[param]; |
| 105 |
}; |
| 106 |
|