Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Swapping out Constants for a Function

I'm trying to refactor the following code, which depending on the top level domain of the recipients email, changes where you click through to in my button. I currently do this with two consts, which I need to refactor into only one.

 const CCENTERURL_AT= `${HOSTURL_AT}/ccenter/zendesk/landing/`;
    const CCENTERURL = `${HOSTURL}.com/ccenter/zendesk/landing/`;

 const recipientEmail = data.ticket.recipient;
      var cCenterUrl;
      if(recipientEmail.indexOf(".com") > 0) 
      {
        cCenterUrl = getCcenterUrl(zendeskID)
      }else{
       cCenterUrl = getAustrianCcenterUrl(zendeskID)
      }



    function getCcenterUrl(ticketID) {
      const cCenterTicketUrl =  CCENTERURL + ticketID ;
      return cCenterTicketUrl;
    }


     // Get Austrian Ccenter Ticket Url using Zendesk ticket ID
    function getAustrianCcenterUrl(ticketID) {
      const cCenterTicketUrlAustria =  CCENTERURL_AT + ticketID ;
      return cCenterTicketUrlAustria;
    }

I know I should be able to create a function which will take recipient Email`s top-level domain as parameter and return appropriate URL for CCENTERURL. But no matter what I've tried its become overcomplicated or hasn't worked. I would be interested to hear peoples opinions on either how I can achieve my goal or even how it would be better to go about this!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So you basically want to combine these into one function and use a string template like this.

const HOSTURL = 'example.com/'
const HOSTURL_AT = 'example.au/'

const reAu = /\.au$/;
const getTicketURL = (
  (mail, id) => `${ reAu.test(mail) > 0 ? HOSTURL : HOSTURL_AT }ccenter/zendesk/landing/${id}`
);


// Test AU
console.log(getTicketURL('foo@google.com.au', 'ABC1231'))

// Test US
console.log(getTicketURL('foo@google.com', 'ABC1231'))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!