1+ export const formatDate = (
2+ value : string | Date ,
3+ formatting : Intl . DateTimeFormatOptions = { month : "short" , day : "numeric" , year : "numeric" }
4+ ) => {
5+ if ( ! value ) return value ;
6+ return new Intl . DateTimeFormat ( "en-US" , formatting ) . format ( new Date ( value ) ) ;
7+ } ;
8+
9+ export const formatDateToMonthShort = ( value : string | Date , toTimeForCurrentDay = true ) => {
10+ const date = new Date ( value ) ;
11+ let formatting : Intl . DateTimeFormatOptions = { month : "short" , day : "numeric" } ;
12+ if ( toTimeForCurrentDay && date . toDateString ( ) === new Date ( ) . toDateString ( ) ) {
13+ formatting = { hour : "numeric" , minute : "numeric" } ;
14+ }
15+ return new Intl . DateTimeFormat ( "en-US" , formatting ) . format ( new Date ( value ) ) ;
16+ } ;
17+
18+ export const getFileExt = ( uri : string ) => uri . includes ( '.' ) ? uri . split ( '.' ) . pop ( ) : '' ;
19+
20+ export const getYearOptions = ( from = 1950 , to = new Date ( ) . getFullYear ( ) ) => {
21+ let result = [ ] ;
22+ for ( let i = to ; i >= from ; i -- ) {
23+ result . push ( { label : i , value : i } ) ;
24+ }
25+ return result ;
26+ } ;
27+
28+ export const removeFalsy = ( obj : any ) => {
29+ let newObj : any = { } ;
30+ Object . keys ( obj ) . forEach ( props => {
31+ if ( obj [ props ] || obj [ props ] === '0' || obj [ props ] === 0 || obj [ props ] !== '' ) {
32+ newObj [ props ] = obj [ props ] ;
33+ }
34+ } ) ;
35+ return newObj ;
36+ } ;
37+
38+ export const getNumberWithOrdinal = ( n : number ) => {
39+ const s = [ "th" , "st" , "nd" , "rd" ] , v = n % 100 ;
40+ return n + ( s [ ( v - 20 ) % 10 ] || s [ v ] || s [ 0 ] ) ;
41+ } ;
42+
43+ export const handleGetDirection = ( lat = '' , lng = '' ) => {
44+ window . open ( `https://www.google.com/maps/search/?api=1&query=${ lat } ,${ lng } ` , '_blank' ) ;
45+ } ;
46+
47+ export const convertTimeFormat = ( time = '' ) => {
48+ if ( ! time ) return '' ;
49+ let [ hr , min ] = time . split ( ':' ) ;
50+ let hour = parseInt ( hr ) ;
51+ if ( hour < 12 ) return `${ hour } :${ min } AM` ;
52+ return `${ hour - 12 < 10 ? `0${ hour - 12 } ` : hour - 12 } :${ min } PM` ;
53+ } ;
54+
55+ export const isJsonParsable = ( str : string ) => {
56+ try { JSON . parse ( str ) ; } catch { return false ; }
57+ return true ;
58+ } ;
59+
60+ export const duplicateObj = ( obj : any ) => JSON . parse ( JSON . stringify ( obj ) ) ;
61+
62+ export const makeTwoDigit = ( num : number ) => num < 10 ? `0${ num } ` : num . toString ( ) ;
63+
64+ export const doesObjsMatch = ( obj1 : any , obj2 : any ) => JSON . stringify ( obj1 ) === JSON . stringify ( obj2 ) ;
65+
66+ export const onlyAllowNumber = ( evt : KeyboardEvent ) => {
67+ const charCode = evt . which ? evt . which : evt . keyCode ;
68+ if ( charCode > 31 && ( charCode < 48 || charCode > 57 ) && charCode !== 46 ) evt . preventDefault ( ) ;
69+ } ;
70+
71+ export const createDateObj = ( date : string , time : string ) => {
72+ try {
73+ const [ y , m , d ] = date . split ( '-' ) ;
74+ const [ hr , min ] = time . split ( ':' ) ;
75+ return new Date ( parseInt ( y ) , parseInt ( m ) - 1 , parseInt ( d ) , parseInt ( hr ) , parseInt ( min ) ) ;
76+ } catch ( e ) { console . log ( 'Error createDateObj' , e ) ; return null ; }
77+ } ;
78+
79+ export const getYoutubeVideoId = ( link : string ) => {
80+ try { let url = new URL ( link ) ; return url . searchParams . get ( 'v' ) || '' ; } catch { return '' ; }
81+ } ;
0 commit comments