I have a task to decode a string URI until there are no more changes. The string URI I am working with typically has around 53,000 characters, so the comparison needs to be fast. For demonstration purposes, I have used a shortened version of the string.
Below is the example code I have written, but unfortunately it is not functioning as expected:
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
*/
What is the best approach to achieve this task seamlessly? Thank you for any assistance.
Update 1
I have updated my code to a new version:
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);
}
}
However, the issue still persists and the code does not work correctly.