1

I would like the decode the string URI, until there is no change. Usualy the string URI has around 53'000 characters. So the compare should be fast. In my example code I have used a short form of the string.

Here is my example code, which unfortunately not working:

var uri = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
var firstDecode = decodeURIComponent(uri.replace(/\+/g,  " "));
var res = Decode(firstDecode);

function Decode(firstDecode){
    var secondDecode = decodeURIComponent(uri.replace(/\+/g,  " "))
    while (firstDecode.localeCompare(secondDecode) != 0) {
        firstDecode = decodeURIComponent(uri.replace(/\+/g,  " "))
    }

  return firstDecode;
}


/* Expected Returns:
localeCompare()
 0:  exact match
-1:  string_a < string_b
 1:  string_a > string_b
 */

How can I do that with the smoothest way? Thanks in advance.

Update 1

Ok new version of my code:

var uri = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
var res = Decode(uri);

function Decode(uri){
    var initialURI = URI
    var newURI = decodeURIComponent(uri.replace(/\+/g,  " "));

    If (initialURI === newURI) {
        // no changes anymore
        return newURI;
    } else {
        // changes were detected, do this function again
        var res = Decode(newURI);
    }
}

But its still doesn't work correct.

Baku Bakar
  • 442
  • 2
  • 8
  • 20
  • You're trying to decode the URI? Try this SO answer if it might be what you're looking for https://stackoverflow.com/a/13691499/6717375 – jonathangersam Oct 05 '18 at 11:56
  • @jonathangersam Thanks for the link. decode the URI is not the issue. The issue is, how to realize, that the decoded URI is the same like the URI before and than stop to do it. Because a URI with 53'000 characters needs to decode multiple times. – Baku Bakar Oct 05 '18 at 12:12
  • _how to realize that the decoded URI is the same_ -- in your while loop condition, (1) consider using `localeCompare` (local**e** with an **e**); (2) edit condition as this current one will always evaluate to `0` (match) because its like saying `'hello'.localeCompare('hello')`. – jonathangersam Oct 05 '18 at 23:55
  • @jonathangersam Thanks for the hint. I have updated my question, new try. – Baku Bakar Oct 06 '18 at 10:11

1 Answers1

0

You're trying to recursively decode some encoded URI, until decoding it doesn't change the result anymore?

If my understanding is correct, please see below:

/* Suppose our sample case is a recursively encoded URI */

let URIEncoded = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
console.log('URI ENCODED ONCE:', URIEncoded)

let URIEncodedTwice = encodeURIComponent(URIEncoded)
console.log('URI ENCODED TWICE:', URIEncodedTwice)

let URIEncodedThrice = encodeURIComponent(URIEncodedTwice)
console.log('URI ENCODED THRICE:', URIEncodedThrice)


/* Get the original URI */
let baseURI = decodeNestedURI(URIEncodedThrice);
console.log('BASE URI:', baseURI) // print result

/* function decodeNestedURI returns the base URI of one that's been encoded multiple times */
function decodeNestedURI(nestedURI) {
    let oldURI = nestedURI
    let newURI = null
    let tries = 0

    while (true) {
      tries++
      newURI = decodeURIComponent(oldURI);
      
      if (newURI == oldURI) break // quit when decoding didn't change anything
      
      oldURI = newURI
    } 
    console.log('TIMES DECODED:', tries-1) // -1 because the last decoding didn't change anything
    return newURI 
}

Hope this helps. Cheers,

jonathangersam
  • 1,137
  • 7
  • 14
  • Wonderful, thanks a lot jonathangersam! It works. This is a good example for me for continue working on my code. It's really helpful for me, because im new in JS. Just a short question: is there a special reason, why you are using 'let' instead of 'var'? Thanks and have a nice day! – Baku Bakar Oct 08 '18 at 07:03
  • Glad to help :) technically ‘var’ is function-scoped, while ‘let’ is block-scoped. ‘let’ is essentially best practice to avoid scoping issues that original javascript ‘var’ had as baggage. For more detail, I recommend scanning thru this chapter http://exploringjs.com/es6/ch_variables.html – jonathangersam Oct 08 '18 at 15:47