import { saveAs } from 'file-saver' ; export { saveAs } ; export const getUnique = ( arr ) : Array < any > => [ ... new Set ( arr ) ] ; export const stringPad = { pad : ( val , len ) : string => { val = String ( val ) ; len = len || 2 ; while ( val . length < len ) val = '0' + val ; return val ; } , padEmpty : ( num , size ) : string => { const s = '   ' + num ; return s . substr ( s . length - size ) ; } , pad0 : ( str , max ) : string => ( str . length < max ? stringPad . pad0 ( '0' + str , max ) : str ) , } ; export const saveVariable = ( variable , filename ) : void => { try { filename = typeof filename == 'undefined' ? 'variable.txt' : filename ; variable = JSON . stringify ( variable ) ; const blob = new Blob ( [ variable ] , { type : 'text/plain;charset=utf-8' } ) ; if ( ! saveAs ) throw new Error ( 'saveAs is unavailable!' ) ; saveAs ( blob , filename ) ; } catch ( e ) { } } ; export const saveCsv = ( items , name ) : void => { try { const replacer = ( value ) => ( value === null ? '' : value ) ; const header = Object . keys ( items [ 0 ] ) ; let csv = items . map ( ( row ) => header . map ( ( fieldName ) => JSON . stringify ( replacer ( row [ fieldName ] ) ) ) . join ( ',' ) ) ; csv . unshift ( header . join ( ',' ) ) ; csv = csv . join ( '\r\n' ) ; const blob = new Blob ( [ csv ] , { type : 'text/plain;charset=utf-8' } ) ; if ( ! saveAs ) throw new Error ( 'saveAs is unavailable!' ) ; saveAs ( blob , `${ name }.csv` ) ; } catch ( error ) { } } ; type rgbaType = [ string | number , string | number , string | number , string | number ] ; export const parseRgba = ( str ) : [ number , number , number , number ] => { let [ r , g , b , a ] = < rgbaType > str . match ( /[\d\.]+/gu ) ; r = parseInt ( < string > r ) / 255 ; g = parseInt ( < string > g ) / 255 ; b = parseInt ( < string > b ) / 255 ; a = parseFloat ( < string > a ) ; if ( isNaN ( r ) || isNaN ( g ) || isNaN ( b ) || isNaN ( a ) ) { return [ 1 , 1 , 1 , 1 ] ; } else { return [ r , g , b , a ] ; } } ; export const hex2RgbA = ( hex ) : rgbaType => { if ( /^#([A-Fa-f0-9]{3}){1,2}$/u . test ( hex ) ) { let c = hex . substring ( 1 ) . split ( '' ) ; if ( c . length == 3 ) { c = [ c [ 0 ] , c [ 0 ] , c [ 1 ] , c [ 1 ] , c [ 2 ] , c [ 2 ] ] ; } c = '0x' + c . join ( '' ) ; const r = ( ( parseInt ( c ) >> 16 ) & 255 ) / 255 ; const g = ( ( parseInt ( c ) >> 8 ) & 255 ) / 255 ; const b = ( parseInt ( c ) & 255 ) / 255 ; return [ r , g , b , 1 ] ; } else { return [ 1 , 1 , 1 , 1 ] ; } } ; export const rgbCss = ( values ) : string => `rgba(${ values [ 0 ] * 255 },${ values [ 1 ] * 255 },${ values [ 2 ] * 255 },${ values [ 3 ] })` ; param { string } str param { number } num returns { string } export const truncateString = ( str , num ) : string => { if ( typeof str == 'undefined' ) return 'Unknown' ; if ( str . length <= num ) { return str ; } return str . slice ( 0 , num ) + '...' ; } ; ( < any > window ) . getUnique = getUnique ; ( < any > window ) . saveCsv = saveCsv ;