param date import { Router } from "@angular/router" ; export function isValidDate ( date ) { return ( date && Object . prototype . toString . call ( date ) === "[object Date]" && ! Number . isNaN ( date . getTime ( ) ) ) ; } export function getUrlWithoutParams ( router ) { const urlTree = router . parseUrl ( router . url ) ; urlTree . queryParams = { } ; return urlTree . toString ( ) ; } export function getParentUrl ( router ) { const url = getUrlWithoutParams ( router ) ; return url . substr ( 0 , url . lastIndexOf ( "/" ) ) ; } param array param propertyToGroupBy export function groupBy < T > ( array , propertyToGroupBy ) { return array . reduce ( ( entryMap , element ) => entryMap . set ( element [ propertyToGroupBy ] , [ ... ( entryMap . get ( element [ propertyToGroupBy ] ) || [ ] ) , element , ] ) , new Map ( ) ) ; } export function calculateAge ( dateOfBirth ) { const now = new Date ( ) ; let age = now . getFullYear ( ) - dateOfBirth . getFullYear ( ) ; const m = now . getMonth ( ) - dateOfBirth . getMonth ( ) ; if ( m < 0 || ( m === 0 && now . getDate ( ) < dateOfBirth . getDate ( ) ) ) { age -- ; } return age ; } export function sortByAttribute < OBJECT > ( attribute , order = "asc" ) { return ( e1 , e2 ) => { const value1 = e1 [ attribute ] ; const value2 = e2 [ attribute ] ; if ( value1 === value2 ) { return 0 ; } if ( value1 === undefined ) { return order === "asc" ? 1 : - 1 ; } if ( value2 === undefined ) { return order === "asc" ? - 1 : 1 ; } if ( value1 < value2 ) { return order === "asc" ? - 1 : 1 ; } else { return order === "asc" ? 1 : - 1 ; } } ; } export function readFile ( file ) { return new Promise ( ( resolve ) => { const fileReader = new FileReader ( ) ; fileReader . addEventListener ( "load" , ( ) => resolve ( fileReader . result as string ) ) ; fileReader . readAsText ( file ) ; } ) ; }