Skip to content

Commit

Permalink
Refactor: Improved Protocol Detection and Defined Regex Outside Compo…
Browse files Browse the repository at this point in the history
…nent

refactored the DomainLink component by enhancing the protocol detection logic, now using a pre-defined regex 'protocolRegex' for improved efficiency
  • Loading branch information
sohammk08 committed Oct 18, 2023
1 parent 37e0ed3 commit 59303b6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/containers/shared/components/DomainLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ export interface Props {
domain: string
}

// Matches a protocol (e.g. 'http://' or 'https://') at the start of a string.
const protocolRegex = /^([a-z][a-z0-9+\-.]*):\/\//

const DomainLink = (props: Props) => {
const { className, decode = false, domain } = props

// If decode is true, decode the domain
const decodedDomain = decode ? decodeHex(domain) : domain

// Check if the decodedDomain contains a protocol (http:// or https://)
const domainHasProtocol = decodedDomain.match(/^([a-z][a-z0-9+\-.]*):\/\//)
// Use the test method to check for the protocol
const domainHasProtocol = protocolRegex.test(decodedDomain)

// if the decodedDomain already has a protocol, use it; oitherwise, add 'https://'
// If decoded domain does not have a protocol, add one ; otherwise, don't
const href = domainHasProtocol ? decodedDomain : `https://${decodedDomain}`

return (
Expand Down

0 comments on commit 59303b6

Please sign in to comment.