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

@ -4,66 +4,98 @@
* @license Apache-2.0 * @license Apache-2.0
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
/** /**
* URL Encode operation * URL Encode operation
*/ */
class URLEncode extends Operation { class URLEncode extends Operation {
/** /**
* URLEncode constructor * URLEncode constructor
*/ */
constructor() { constructor() {
super(); super();
this.name = "URL Encode"; this.name = "URL Encode";
this.module = "URL"; this.module = "URL";
this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.<br><br>e.g. <code>=</code> becomes <code>%3d</code>"; this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.<br><br>e.g. <code>=</code> becomes <code>%3d</code>";
this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; this.infoURL = "https://wikipedia.org/wiki/Percent-encoding";
this.inputType = "string"; this.inputType = "string";
this.outputType = "string"; this.outputType = "string";
this.args = [ this.args = [
{ {
"name": "Encode all special chars", "name": "Encode all special chars",
"type": "boolean", "type": "boolean",
"value": false "value": false
} },
]; {
} "name": "Encode all chars",
"type": "boolean",
/** "value": false
* @param {string} input },
* @param {Object[]} args ];
* @returns {string} }
*/
run(input, args) { /**
const encodeAll = args[0]; * @param {string} input
return encodeAll ? this.encodeAllChars(input) : encodeURI(input); * @param {Object[]} args
} * @returns {string}
*/
/** run(input, args) {
* Encode characters in URL outside of encodeURI() function spec const encodeAll = args[0];
* const encodeAllEvery = args[1];
* @param {string} str return encodeAllEvery ? this.encodeAllEveryChars(input) : encodeAll ? this.encodeAllChars(input) : encodeURI(input);
* @returns {string} //return encodeAll ? this.encodeAllChars(input) : encodeURI(input);
*/ }
encodeAllChars (str) {
// TODO Do this programmatically /**
return encodeURIComponent(str) * Pads a string from the front to a given length with a given char
.replace(/!/g, "%21") *
.replace(/#/g, "%23") * @param {string} str
.replace(/'/g, "%27") * @returns {string}
.replace(/\(/g, "%28") */
.replace(/\)/g, "%29") frontPad (str, length, char){
.replace(/\*/g, "%2A") return str.length >= length ? str : (char * (length - str.length)) + str;
.replace(/-/g, "%2D") }
.replace(/\./g, "%2E")
.replace(/_/g, "%5F") /**
.replace(/~/g, "%7E"); * Encode characters in URL outside of encodeURI() function spec
} *
* @param {string} str
} * @returns {string}
*/
encodeAllChars (str) {
export default URLEncode; // TODO Do this programmatically
return encodeURIComponent(str)
.replace(/!/g, "%21")
.replace(/#/g, "%23")
.replace(/'/g, "%27")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29")
.replace(/\*/g, "%2A")
.replace(/-/g, "%2D")
.replace(/\./g, "%2E")
.replace(/_/g, "%5F")
.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;