import { LinkGetProps } from '@reach/router' ; import { FormDataWithFile , FormDataWithoutFile , GraphQLEdges } from '../types' ; export function cx ( ... args ) { return args . filter ( cls => typeof cls !== 'boolean' && typeof cls !== 'undefined' && cls . trim ( ) !== '' ) . join ( ' ' ) ; } export function numberFormat ( val , decimalPlaces ) { var multiplier = Math . pow ( 10 , decimalPlaces ) ; return Number ( ( Math . round ( val * multiplier ) / multiplier ) . toFixed ( decimalPlaces ) ) ; } export function hexToRGB ( hex , alpha = 1 ) { const r = parseInt ( hex . slice ( 1 , 3 ) , 16 ) ; const g = parseInt ( hex . slice ( 3 , 5 ) , 16 ) ; const b = parseInt ( hex . slice ( 5 , 7 ) , 16 ) ; if ( alpha < 0 || alpha > 1 ) { throw new Error ( 'Alpha value must be between 0 and 1.' ) ; } return alpha === 1 ? `rgb(${ r }, ${ g }, ${ b })` : `rgba(${ r }, ${ g }, ${ b }, ${ alpha })` ; } export function isValidEmail ( email ) { return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i . test ( email ) ; } export function createPhoneNumber ( { phone , countryCode = '62' , } ) { const splitNumbers = phone . replace ( /-|\s/g , '' ) . match ( /\d+/g ) || [ ] ; if ( splitNumbers . length > 0 ) { const sanitizedNumber = splitNumbers . length > 0 ? splitNumbers [ 0 ] : '' ; if ( sanitizedNumber . startsWith ( '0' ) ) { return countryCode + Number ( sanitizedNumber ) ; } if ( sanitizedNumber . startsWith ( countryCode ) ) { return sanitizedNumber ; } return countryCode + sanitizedNumber ; } throw new Error ( 'Please pass a valid phone number' ) ; } export function createWhatsAppLink ( { phone , text , } ) { let link = `https://wa.me/${ createPhoneNumber ( { phone } ) }` ; if ( text ) { link += `?text=${ encodeURIComponent ( text ) }` ; } return link ; } export function setPartiallyCurrent ( { href , isPartiallyCurrent , isCurrent , } ) { if ( isPartiallyCurrent && ! isCurrent && href !== '/' ) { return { 'data-partially-current' : true , } ; } return { } ; } export function extractNodes < T > ( arr ) { return arr . edges . map ( ( { node } ) => node ) ; } export function fakeGraphQLTag ( query ) { const tagArgs = arguments ; return tagArgs [ 0 ] . reduce ( ( accumulator , string , index ) => { accumulator += string ; if ( index + 1 in tagArgs ) accumulator += tagArgs [ index + 1 ] ; return accumulator ; } , '' ) ; } function encodeWithFile ( data ) { const formData = new FormData ( ) ; Object . entries ( data ) . forEach ( ( [ key , value ] ) => { formData . append ( key , value ) ; } ) ; return formData ; } function encodeWithoutFile ( data ) { return Object . entries ( data ) . map ( ( [ key , value ] ) => encodeURIComponent ( key ) + '=' + encodeURIComponent ( value ) ) . join ( '&' ) ; } function isFormDataWithFile ( data ) { return Object . values ( data ) . some ( value => value instanceof File ) ; } export function submitFormToNetlify ( data ) { return fetch ( '/' , { method : 'POST' , ... ( ! isFormDataWithFile ( data ) && { headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } , } ) , body : isFormDataWithFile ( data ) ? encodeWithFile ( data ) : encodeWithoutFile ( data ) , } ) ; }