Last active
October 27, 2021 13:33
-
-
Save dwsmart/8cfe3f64a5cb8694585df74aa89d40da to your computer and use it in GitHub Desktop.
decode-encode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fixedEncodeURI(str) { | |
const prepURL = new URL(str); | |
const origin = prepURL.origin; | |
let raw = str; | |
let decoded = decodeURI(str); | |
while (raw !== decoded) { | |
decoded = decodeURI(decoded); | |
raw = decodeURI(raw); | |
} | |
justPath = decoded.replace(origin, ''); | |
let output = origin + encodeURIComponent(justPath).replace(/[!'()*]/g, function(c) { | |
return '%' + c.charCodeAt(0).toString(16); | |
}); | |
return output.replace(/%3F/g, '?').replace(/%3D/g, '=').replace(/%26/g, '&'); | |
} | |
// fixedEncodeURI('http://foo.bar/foo/bar/ツ') returns http://foo.bar%2Ffoo%2Fbar%2F%E3%83%84 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment