added encode all chars for url encode

This commit is contained in:
TheSavageTeddy 2022-11-27 12:14:06 +08:00
parent ba12ad8e7c
commit c555e54dee

View file

@ -28,7 +28,12 @@ class URLEncode extends Operation {
"name": "Encode all special chars",
"type": "boolean",
"value": false
}
},
{
"name": "Encode all chars",
"type": "boolean",
"value": false
},
];
}
@ -39,7 +44,19 @@ class URLEncode extends Operation {
*/
run(input, args) {
const encodeAll = args[0];
return encodeAll ? this.encodeAllChars(input) : encodeURI(input);
const encodeAllEvery = args[1];
return encodeAllEvery ? this.encodeAllEveryChars(input) : encodeAll ? this.encodeAllChars(input) : encodeURI(input);
//return encodeAll ? this.encodeAllChars(input) : encodeURI(input);
}
/**
* Pads a string from the front to a given length with a given char
*
* @param {string} str
* @returns {string}
*/
frontPad (str, length, char){
return str.length >= length ? str : (char * (length - str.length)) + str;
}
/**
@ -63,7 +80,22 @@ class URLEncode extends Operation {
.replace(/~/g, "%7E");
}
/**
* Encode ALL characters in URL including alphanumeric based on char codes
*
* @param {string} str
* @returns {string}
*/
encodeAllEveryChars (str) {
let encoded = "";
for (var char of str) {
encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0")
}
return encoded;
}
}
export default URLEncode;