Javascript partial wildcard in json sitemap

I am working on a project that has a language switcher. This way it will pick up the current url, loop through the json object and return its language pair.

So

/en/cookie-policy
/de/cookie-richtlinie

      

the problem i am facing now - accepts a wildcard - as a pair of matches with an id

so instead of hardcoding in json all possible id's

/en/virtual-conference-room/1
/de/virtuellier-konferenz-raum/1

      

I want to put some kind of wildcard in json that decodes the language pairs but keeps the desired id

So

/en/virtual-conference-room/[:num]
/de/virtuellier-konferenz-raum/[:num]

      

then I am doing a language switch / pairwise match - produces the substitution result for id 1, 234, 1000, etc.

I think its a case of creating an else if - and at this point allows a couple of wildcard pairs to be expressed, but not sure where to start.

JSFiddle

function getUrl(pairUrl, currentLng, enMenu, deMenu, obj) {
  for (let k in obj) {
    if (!obj.hasOwnProperty(k)) continue
    if (obj[k].link === pairUrl) {
      if (currentLng === 'de') {
        return enMenu[k].link // get en link equivlant
      } else {
        return deMenu[k].link // get de link equivlant
      }
    } else if (paritalmatch) { 
        //finds a wild card match and allows a pair match
    }
    else {
      if (!obj[k].hasOwnProperty('children') || obj[k].children.length <= 0) continue;
      var ret = getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children);
      if(typeof ret != 'undefined') return ret;
    }
  }
}


function getLanguagePair(currentLng, pairUrl) {
  //  'find url in json tree'
  var enMenu = linkTreeObject.langs[1].lines.menu
  var deMenu = linkTreeObject.langs[0].lines.menu

  let obj = {}
    // find position in tree
  if (currentLng === 'de') {
    obj = deMenu
  } else {
    obj = enMenu
  }

  return getUrl(pairUrl, currentLng, enMenu, deMenu, obj)
}

//works
console.log(getLanguagePair("en", "/en/how-it-works"))
console.log(getLanguagePair("en", "/en/virtual-conference-room/1"))
console.log(getLanguagePair("en", "/en/virtual-conference-room/2"))

      

+3


source to share


2 answers


You can use regex to archive the result you want. The original idea replaces every pattern in the link with a matching regex. Then we could use a regular expression to compare between the two strings.
Example:

(1) Url: /en/virtual-conference-room/1  
(2) Link: /en/virtual-conference-room/[:num]  
(3) Regex: /en/virtual-conference-room/(\d+)  

      



If regex (3) matches Url (1), we can get the corresponding reference and replace the wildcards with the appropriate values.

I created an update to your code here , please check.

+1


source


Please consider the following: http://jsfiddle.net/jg4cgkm8/

I removed [:num]

as unnecessary from here:

{
   "title": "Virtual Conference Room",
   "link": "/en/virtual-conference-room/"
}

      

And updated function getUrl

as below:



function getUrl(pairUrl, currentLng, enMenu, deMenu, obj) {
  for (let k in obj) {
    if (!obj.hasOwnProperty(k)) continue;
    if (pairUrl.indexOf(obj[k].link) === 0) {
      var tail = pairUrl.substr(obj[k].link.length);
      if (currentLng === 'de') {
        return enMenu[k].link + tail // get en link equivlant
      } else {
        return deMenu[k].link + tail // get de link equivlant
      }
    }
    else {
      if (!obj[k].hasOwnProperty('children') || obj[k].children.length <= 0) continue;
      var ret = getUrl(pairUrl, currentLng, enMenu[k].children, deMenu[k].children, obj[k].children);
      if(typeof ret != 'undefined') return ret;
    }
  }
}

      

Where if (pairUrl.indexOf(obj[k].link) === 0)

checks to see if the rest starts pairUrl

with obj[k].link

and , if any.var tail

pairUrl

The resulting link then matches the equivalent + tail.

Hope it helps.

0


source







All Articles