/**
 * -------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the MIT License.
 * See License in the project root for license information.
 * -------------------------------------------------------------------------------------------
 */ export function getRelativeDisplayDate ( date ) { const now = new Date ( ) ; const today = new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) ) ; if ( date >= today ) { return date . toLocaleString ( 'default' , { hour : 'numeric' , minute : 'numeric' } ) ; } const sunday = new Date ( today ) ; sunday . setDate ( now . getDate ( ) - now . getDay ( ) ) ; if ( date >= sunday ) { return date . toLocaleString ( 'default' , { hour : 'numeric' , minute : 'numeric' , weekday : 'short' } ) ; } const lastTwoWeeks = new Date ( sunday ) ; lastTwoWeeks . setDate ( sunday . getDate ( ) - 7 ) ; if ( date >= lastTwoWeeks ) { return date . toLocaleString ( 'default' , { day : 'numeric' , month : 'numeric' , weekday : 'short' } ) ; } return date . toLocaleString ( 'default' , { day : 'numeric' , month : 'numeric' , year : 'numeric' } ) ; } param time export function delay ( ms ) { return new Promise ( resolve => { setTimeout ( resolve , ms ) ; } ) ; } export param { Date } date returns export function getShortDateString ( date ) { const month = date . getMonth ( ) ; const day = date . getDate ( ) ; return `${ getMonthString ( month ) } ${ day }` ; } export param { number } month returns { string } export function getMonthString ( month ) { switch ( month ) { case 0 : return 'January' ; case 1 : return 'February' ; case 2 : return 'March' ; case 3 : return 'April' ; case 4 : return 'May' ; case 5 : return 'June' ; case 6 : return 'July' ; case 7 : return 'August' ; case 8 : return 'September' ; case 9 : return 'October' ; case 10 : return 'November' ; case 11 : return 'December' ; default : return 'Month' ; } } export param { number } day returns { string } export function getDayOfWeekString ( day ) { switch ( day ) { case 0 : return 'Sunday' ; case 1 : return 'Monday' ; case 2 : return 'Tuesday' ; case 3 : return 'Wednesday' ; case 4 : return 'Thursday' ; case 5 : return 'Friday' ; case 6 : return 'Saturday' ; default : return 'Day' ; } } export param { number } monthNum returns { number } export function getDaysInMonth ( monthNum ) { switch ( monthNum ) { case 1 : return 28 ; case 3 : case 5 : case 8 : case 10 : default : return 30 ; case 0 : case 2 : case 4 : case 6 : case 7 : case 9 : case 11 : return 31 ; } } export param { number } month param { number } year returns export function getDateFromMonthYear ( month , year ) { const yearStr = year + '' ; let monthStr = month + '' ; if ( monthStr . length < 2 ) { monthStr = '0' + monthStr ; } return new Date ( `${ yearStr }-${ monthStr }-1T12:00:00-${ new Date ( ) . getTimezoneOffset ( ) / 60 }` ) ; } export param { * } func param { * } time returns export function debounce ( func , time ) { let timeout ; return function ( ) { const functionCall = ( ) => func . apply ( this , arguments ) ; clearTimeout ( timeout ) ; timeout = setTimeout ( functionCall , time ) ; } ; } param { Blob } blob returns { Promise < string > } export function blobToBase64 ( blob ) { return new Promise ( ( resolve , reject ) => { const reader = new FileReader ( ) ; reader . onerror = reject ; reader . onload = _ => { resolve ( reader . result as string ) ; } ; reader . readAsDataURL ( blob ) ; } ) ; } param { string } emailString returns { string } export function extractEmailAddress ( emailString ) { if ( emailString . startsWith ( '[' ) ) { return emailString . match ( /([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi ) . toString ( ) ; } else return emailString ; } param { string } emailString returns { boolean } export function isValidEmail ( emailString ) { const emailRx = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/ ; return emailRx . test ( emailString ) ; } param bytes param decimals returns export function formatBytes ( bytes , decimals = 2 ) { if ( bytes === 0 ) return '0 Bytes' ; const k = 1024 ; const dm = decimals < 0 ? 0 : decimals ; const sizes = [ 'Bytes' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] ; const i = Math . floor ( Math . log ( bytes ) / Math . log ( k ) ) ; return parseFloat ( ( bytes / Math . pow ( k , i ) ) . toFixed ( dm ) ) + ' ' + sizes [ i ] ; }