did encodeAllSpecialChars programmatically

This commit is contained in:
TheSavageTeddy 2022-11-27 12:46:02 +08:00
parent 4e13b074c5
commit bd51fc05e3

View file

@ -65,18 +65,16 @@ class URLEncode extends Operation {
* @returns {string} * @returns {string}
*/ */
encodeAllSpecialChars (str) { encodeAllSpecialChars (str) {
// TODO Do this programmatically const specialChars = "!#'()*-._~";
return encodeURIComponent(str) let encoded = "";
.replace(/!/g, "%21") for (var char of str) {
.replace(/#/g, "%23") if (encodeURIComponent(char) === char && specialChars.includes(char)){
.replace(/'/g, "%27") encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0");
.replace(/\(/g, "%28") } else {
.replace(/\)/g, "%29") encoded += encodeURIComponent(char);
.replace(/\*/g, "%2A") }
.replace(/-/g, "%2D") }
.replace(/\./g, "%2E") return encoded;
.replace(/_/g, "%5F")
.replace(/~/g, "%7E");
} }
/** /**