import { TemplateRef , ElementRef } from '@angular/core' ; import { coerceBooleanProperty as coerceBoolean , coerceCssPixelValue as coerceCssPixel , _isNumberValue } from '@angular/cdk/coercion' ; export function isUndefined ( value ) { return value === undefined ; } export function isNull ( value ) { return value === null ; } export function isUndefinedOrNull ( value ) { return isUndefined ( value ) || isNull ( value ) ; } export function isArray < T = any > ( value ) { return value && baseGetTag ( value ) === '[object Array]' ; } export function isEmpty ( value ? ) { return ! ( isArray ( value ) && value . length > 0 ) ; } export function isString ( value ? ) { return typeof value == 'string' || ( ! isArray ( value ) && isObjectLike ( value ) && baseGetTag ( value ) === '[object String]' ) ; } function isObjectLike ( value ) { return value !== null && typeof value === 'object' ; } function baseGetTag ( value ) { const objectProto = Object . prototype ; const hasOwnProperty = objectProto . hasOwnProperty ; const toString = objectProto . toString ; const symToStringTag = typeof Symbol !== 'undefined' ? Symbol . toStringTag : undefined ; if ( value == null ) { return value === undefined ? '[object Undefined]' : '[object Null]' ; } if ( ! ( symToStringTag && symToStringTag in Object ( value ) ) ) { return toString . call ( value ) ; } const isOwn = hasOwnProperty . call ( value , symToStringTag ) ; const tag = value [ symToStringTag ] ; let unmasked = false ; try { value [ symToStringTag ] = undefined ; unmasked = true ; } catch ( e ) { } const result = toString . call ( value ) ; if ( unmasked ) { if ( isOwn ) { value [ symToStringTag ] = tag ; } else { delete value [ symToStringTag ] ; } } return result ; } export function isNumber ( value ) { return typeof value === 'number' || ( isObjectLike ( value ) && baseGetTag ( value ) === '[object Number]' ) ; } export function isObject ( value ) { const type = typeof value ; return ! ! value && ( type === 'object' || type === 'function' ) ; } export function isFunction ( value ) { const type = typeof value ; return ! ! value && type === 'function' ; } export function isDate ( value ) { const type = typeof value ; return ! ! value && type === 'object' && ! ! value . getTime ; } export function coerceArray < T > ( value ) { return Array . isArray ( value ) ? value : [ value ] ; } export function get ( object , path , defaultValue ? ) { const paths = path . split ( '.' ) ; let result = object [ paths . shift ( ) ] ; while ( result && paths . length ) { result = result [ paths . shift ( ) ] ; } return result === undefined ? defaultValue : result ; } export function set ( object , path , value ) { if ( object == null ) { return object ; } const paths = path . split ( '.' ) ; let index = - 1 ; const length = paths . length ; const lastIndex = length - 1 ; let nested = object ; while ( nested !== null && ++ index < length ) { const key = paths [ index ] ; if ( isObject ( nested ) ) { if ( index === lastIndex ) { nested [ key ] = value ; nested = nested [ key ] ; break ; } else { if ( nested [ key ] == null ) { nested [ key ] = { } ; } } } nested = nested [ key ] ; } return object ; } export function isBoolean ( value ) { return value === true || value === false || ( isObjectLike ( value ) && baseGetTag ( value ) === '[object Boolean]' ) ; } export function htmlElementIsEmpty ( element ) { if ( element && element . childNodes ) { const nodes = element . childNodes ; for ( let i = 0 ; i < nodes . length ; i ++ ) { const node = nodes [ i ] ; if ( node . nodeType === Node . ELEMENT_NODE && ( node as HTMLElement ) . outerHTML . toString ( ) . trim ( ) . length !== 0 ) { return false ; } else if ( node . nodeType === Node . TEXT_NODE && node . textContent . toString ( ) . trim ( ) . length !== 0 ) { return false ; } else if ( node . nodeType !== Node . COMMENT_NODE ) { return false ; } } } return true ; } export function hexToRgb ( hexValue , alpha ? ) { const rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i ; const hex = hexValue . replace ( rgx , ( m , r , g , b ) => r + r + g + g + b + b ) ; const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i . exec ( hex ) ; const resultR = parseInt ( rgb [ 1 ] , 16 ) ; const resultG = parseInt ( rgb [ 2 ] , 16 ) ; const resultB = parseInt ( rgb [ 3 ] , 16 ) ; if ( alpha ) { return `rgba(${ resultR }, ${ resultG }, ${ resultB }, ${ alpha })` ; } else { return `rgb(${ resultR }, ${ resultG }, ${ resultB })` ; } } export function dateToUnixTimestamp ( date ) { if ( isNumber ( date ) ) { if ( date . toString ( ) . length === 10 ) { return date as number ; } else { return parseInt ( ( ( date as number ) / 1000 ) . toFixed ( 0 ) , 10 ) ; } } else { return parseInt ( ( ( date as Date ) . getTime ( ) / 1000 ) . toFixed ( 0 ) , 10 ) ; } } export function clamp ( value , min = 0 , max = 100 ) { return Math . max ( min , Math . min ( max , value ) ) ; } export function keyBy < T > ( array , key ) { const result = { } ; array . forEach ( item => { const keyValue = item [ key ] ; ( result as any ) [ keyValue ] = item ; } ) ; return result ; } export function indexKeyBy < T > ( array , key ) { const result = { } ; array . forEach ( ( item , index ) => { const keyValue = item [ key ] ; ( result as any ) [ keyValue ] = index ; } ) ; return result ; } function upperFirst ( string ) { return string . slice ( 0 , 1 ) . toUpperCase ( ) + string . slice ( 1 ) ; } export function camelCase ( values ) { if ( isArray ( values ) ) { return values . reduce ( ( result , word , index ) => { word = word . toLowerCase ( ) ; return result + ( index ? upperFirst ( word ) : word ) ; } , '' ) ; } else { return ; } } export function generateRandomStr ( ) { return Math . random ( ) . toString ( 36 ) . substring ( 2 ) ; } export function isTemplateRef < C = any > ( value ) { return value instanceof TemplateRef ; } export function isHTMLElement ( value ) { return value instanceof HTMLElement ; } export function isElementRef ( value ) { return value instanceof ElementRef ; } export function coerceBooleanProperty ( value ) { if ( value === '' || ( value && value !== 'false' ) ) { return true ; } else { return false ; } } export type FunctionProp < T > = ( ... args : any [ ] ) => T ; export function coerceNumberValue ( value ) ; export function coerceNumberValue < D > ( value , fallback ) ; export function coerceNumberValue ( value , fallbackValue = 0 ) { return _isNumberValue ( value ) ? Number ( value ) : fallbackValue ; } export function coerceCssPixelValue ( value ) { return coerceCssPixel ( value ) ; } export function valueFunctionProp < T > ( prop , ... args ) { return typeof prop === 'function' ? prop ( ... args ) : prop ; } export function shallowEqual ( objA ? , objB ? ) { if ( objA === objB ) { return true ; } if ( typeof objA !== 'object' || ! objA || typeof objB !== 'object' || ! objB ) { return false ; } const keysA = Object . keys ( objA ) ; const keysB = Object . keys ( objB ) ; if ( keysA . length !== keysB . length ) { return false ; } const bHasOwnProperty = Object . prototype . hasOwnProperty . bind ( objB ) ; for ( let idx = 0 ; idx < keysA . length ; idx ++ ) { const key = keysA [ idx ] ; if ( ! bHasOwnProperty ( key ) ) { return false ; } if ( objA [ key ] !== objB [ key ] ) { return false ; } } return true ; } export function concatArray < TItem > ( items , originalItems = [ ] ) { let _originalItems = [ ] ; if ( ! originalItems ) { _originalItems = [ ] ; } else { _originalItems = coerceArray ( originalItems ) ; } if ( items ) { if ( isArray ( items ) ) { return [ ... _originalItems , ... items ] ; } else { return [ ... _originalItems , items ] ; } } else { return _originalItems ; } }