import { HttpParams } from '@angular/common/http' ; import * as moment from 'moment' ; import * as timezone from 'moment-timezone' ; import { distinctUntilChanged } from 'rxjs/operators' ; export function toParams ( query ) { let params = new HttpParams ( ) ; Object . keys ( query ) . forEach ( ( key ) => { if ( isJsObject ( query [ key ] ) ) { params = toSubParams ( params , key , query [ key ] ) ; } else { params = params . append ( key . toString ( ) , query [ key ] ) ; } } ) ; return params ; } function isJsObject ( object ) { return ( object !== null && object !== undefined && typeof object === 'object' ) ; } param item returns { boolean } export function isNotEmpty ( item ) { return ! isEmpty ( item ) ; } param item returns { boolean } export function isEmpty ( item ) { if ( item instanceof Array ) { item = item . filter ( ( val ) => ! isEmpty ( val ) ) ; return item . length === 0 ; } else if ( item && typeof item === 'object' ) { for ( var key in item ) { if ( item [ key ] === null || item [ key ] === undefined || item [ key ] === '' ) { delete item [ key ] ; } } return Object . keys ( item ) . length === 0 ; } else { return ( ! item || ( item + '' ) . toLocaleLowerCase ( ) === 'null' || ( item + '' ) . toLocaleLowerCase ( ) === 'undefined' ) ; } } function toSubParams ( params , key , object ) { Object . keys ( object ) . forEach ( ( childKey ) => { if ( isJsObject ( object [ childKey ] ) ) { params = toSubParams ( params , `${ key }[${ childKey }]` , object [ childKey ] ) ; } else { params = params . append ( `${ key }[${ childKey }]` , object [ childKey ] ) ; } } ) ; return params ; } export function toFormData ( obj , form ? , namespace ? ) { const fd = form || new FormData ( ) ; let formKey ; for ( const property in obj ) { if ( obj . hasOwnProperty ( property ) && obj [ property ] ) { if ( namespace ) { formKey = namespace + '[' + property + ']' ; } else { formKey = property ; } if ( obj [ property ] instanceof Date ) { fd . append ( formKey , obj [ property ] . toISOString ( ) ) ; } else if ( typeof obj [ property ] === 'object' && ! ( obj [ property ] instanceof File ) ) { toFormData ( obj [ property ] , fd , formKey ) ; } else { fd . append ( formKey , obj [ property ] ) ; } } } return fd ; } export function progressStatus ( value ) { if ( value <= 25 ) { return 'danger' ; } else if ( value <= 50 ) { return 'warning' ; } else if ( value <= 75 ) { return 'info' ; } else { return 'success' ; } } export function getContrastColor ( hex ) { const threshold = 130 ; const hexToRGB = ( h ) => { const hexValue = h . charAt ( 0 ) === '#' ? h . substring ( 1 , 7 ) : h ; return { red : parseInt ( hexValue . substring ( 0 , 2 ) , 16 ) , blue : parseInt ( hexValue . substring ( 2 , 4 ) , 16 ) , green : parseInt ( hexValue . substring ( 4 , 6 ) , 16 ) } ; } ; const { red , green , blue } = hexToRGB ( hex ) ; const cBrightness = ( red * 299 + green * 587 + blue * 114 ) / 1000 ; return cBrightness > threshold ? '#000000' : '#ffffff' ; } /**
 * The precision for a decimal (exact numeric applies only for decimal column), which is the maximum
 * number of digits that are stored.
 */ export function convertPrecisionFloatDigit ( val , digit = 6 ) { return parseFloat ( parseFloat ( val . toString ( ) ) . toFixed ( digit ) ) ; } export function retrieveNameFromEmail ( email ) { if ( email ) { return ucFirst ( email . substring ( 0 , email . lastIndexOf ( '@' ) ) , true ) ; } return ; } export function convertLocalToTimezone ( localDt , localDtFormat , timeZone , format = 'YYYY-MM-DD hh:mm:ss' ) { return timezone ( localDt , localDtFormat ) . tz ( timeZone ) . format ( format ) ; } export function ucFirst ( str , force ) { str = force ? str . toLowerCase ( ) : str ; return str . replace ( /(\b)([a-zA-Z])/ , function ( firstLetter ) { return firstLetter . toUpperCase ( ) ; } ) ; } export function toLocal ( data ) { return moment . utc ( data ) . local ( ) ; } export function toUTC ( data ) { return moment ( data ) . utc ( ) ; } export function distinctUntilChange < T > ( ) { try { return distinctUntilChanged < T > ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) ) ; } catch ( error ) { console . log ( { error } ) ; } } export const compareDate = ( date1 , date2 ) => { const d1 = new Date ( date1 ) ; const d2 = new Date ( date2 ) ; const same = d1 . getTime ( ) === d2 . getTime ( ) ; if ( same ) { return false ; } return d1 > d2 ; } ; export function splitCamelCase ( word ) { let output ; if ( typeof word !== 'string' ) { throw new Error ( 'The "word" parameter must be a string.' ) ; } output = [ ] ; const split = word . split ( '_' ) ; split . forEach ( ( text ) => { output . push ( ucFirst ( text , true ) ) ; } ) ; return output . join ( ' ' ) ; }