Senthil
Senthil

Reputation: 1101

Changing urlpath with Object using javascript

I need to know how to replace the urltext with an object using javavscript.

If the url is www.xyz.com/en/all-services-from-mal-to-sin/details?amount=1000&scy=SGD and if the lang is en, then replace the url with matching object key and if the lang is zh then replace the url with the matching object value.

ExpectedOutput:
if url is 
www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD 
=> output :www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD

if url is 
www.xyz.com/zh/all-services-from-mal-to-sin?amount=1000&scy=SGD  
=> output: www.xyz.com/zh/hui-zhi-phi-tho-zmal-zhi-stin?amount=1000&scy=SG

if url is 
www.xyz.com/en/hui-zhi-phi-tho-zmal-zhi-stin?amount=1000&scy=SG 
 => output: www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD
var obj1={
    "transfer-services": "xi-hou-zhi-n",
    "about-info": "zhi-zhu",
    "contact": "zhi-phi",
    "all-services-from": "hui-zhi-phi-tho",
    "to": "zhi",
    "sin": "stin",
    "mal": "zmal"
};

function transformURL(url,value) {
    let [base, lang, segment, ...rest] = url.split('/');
    lang=value;
    if(obj1.hasOwnProperty(segment)) {
        segment = obj1[segment];
    } else {
        Object.entries(obj1).forEach(([key, val]) => {
            if(val == segment) {segment = key};
        });
    }

    return [base, lang, segment, ...rest].join('/');
}
console.log(transformURL('www.xyz.com/en/all-services-from-mal-to-sin?amount=1000&scy=SGD', "zh"));

Upvotes: 1

Views: 76

Answers (2)

AvcS
AvcS

Reputation: 2323

The mapping you have is en to zh, if you want 2 way conversion you need the reverse mapping too.

And your check obj1.hasOwnProperty(segment) and val === segment both of them are never going to work as all the keys in your object are partial, your segment is a combination of multiple keys, so you need to loop through the keys, check if key is part of the segment and replace that part of the segment.

const enToZh = {
    "transfer-services": "xi-hou-zhi-n",
    "about-info": "zhi-zhu",
    "contact": "zhi-phi",
    "all-services-from": "hui-zhi-phi-tho",
    "to": "zhi",
    "sin": "stin",
    "mal": "zmal"
};

const zhToEn = Object.keys(enToZh).reduce((a, c) => (
    { ...a, [enToZh[c]]: c }
), {});

function transformURL(url) {
    let [base, lang, segment, ...rest] = url.split('/');
    const obj = lang === 'en' ? zhToEn : enToZh;
    Object.keys(obj).forEach(key => {
        segment = segment.replace(key, obj[key]);
    });

    return [base, lang, segment, ...rest].join('/');
}

console.log(transformURL('www.xyz.com/zh/all-services-from-mal-to-sin/details?amount=1000&scy=SGD'));

Upvotes: 1

Ravi
Ravi

Reputation: 1330

This might help you.

function transformURL(url,value) {
let [base, lang, segment, ...rest] = url.split('/');
lang=value;

var newsegment=segment.split('?')[0]

   if(lang=="zh")
   {
       newsegment = obj1["all-services-from"]+"-"+obj1["mal"]+"-"+obj1["to"]+"-"+obj1["sin"]+"?"+segment.split('?')[1];
   }else{
      newsegment = "all-services-from"+"-"+"mal"+"-"+"to"+"-"+"sin"+"?"+segment.split('?')[1];
   }

   return [base, lang, newsegment, ...rest].join('/');
}

https://jsfiddle.net/d2tq4w3r/

Upvotes: 0

Related Questions