From c555e54dee3837bc91e808078daa53f304f70118 Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:14:06 +0800 Subject: [PATCH 1/7] added encode all chars for url encode --- src/core/operations/URLEncode.mjs | 156 ++++++++++++++++++------------ 1 file changed, 94 insertions(+), 62 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index a5efd213..6e29e6b5 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -4,66 +4,98 @@ * @license Apache-2.0 */ -import Operation from "../Operation.mjs"; + import Operation from "../Operation.mjs"; -/** - * URL Encode operation - */ -class URLEncode extends Operation { - - /** - * URLEncode constructor - */ - constructor() { - super(); - - this.name = "URL Encode"; - this.module = "URL"; - this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.

e.g. = becomes %3d"; - this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; - this.inputType = "string"; - this.outputType = "string"; - this.args = [ - { - "name": "Encode all special chars", - "type": "boolean", - "value": false - } - ]; - } - - /** - * @param {string} input - * @param {Object[]} args - * @returns {string} - */ - run(input, args) { - const encodeAll = args[0]; - return encodeAll ? this.encodeAllChars(input) : encodeURI(input); - } - - /** - * Encode characters in URL outside of encodeURI() function spec - * - * @param {string} str - * @returns {string} - */ - encodeAllChars (str) { - // 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"); - } - -} - - -export default URLEncode; + /** + * URL Encode operation + */ + class URLEncode extends Operation { + + /** + * URLEncode constructor + */ + constructor() { + super(); + + this.name = "URL Encode"; + this.module = "URL"; + this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.

e.g. = becomes %3d"; + this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Encode all special chars", + "type": "boolean", + "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]; + 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; + } + + /** + * Encode characters in URL outside of encodeURI() function spec + * + * @param {string} str + * @returns {string} + */ + encodeAllChars (str) { + // 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; + \ No newline at end of file From e80fad227e3307a19d9eaa583181120545f7e1e6 Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:15:42 +0800 Subject: [PATCH 2/7] fix formatting --- src/core/operations/URLEncode.mjs | 187 +++++++++++++++--------------- 1 file changed, 93 insertions(+), 94 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index 6e29e6b5..45b64632 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -4,98 +4,97 @@ * @license Apache-2.0 */ - import Operation from "../Operation.mjs"; +import Operation from "../Operation.mjs"; - /** - * URL Encode operation - */ - class URLEncode extends Operation { - - /** - * URLEncode constructor - */ - constructor() { - super(); - - this.name = "URL Encode"; - this.module = "URL"; - this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.

e.g. = becomes %3d"; - this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; - this.inputType = "string"; - this.outputType = "string"; - this.args = [ - { - "name": "Encode all special chars", - "type": "boolean", - "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]; - 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; - } - - /** - * Encode characters in URL outside of encodeURI() function spec - * - * @param {string} str - * @returns {string} - */ - encodeAllChars (str) { - // 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; - \ No newline at end of file +/** + * URL Encode operation + */ +class URLEncode extends Operation { + + /** + * URLEncode constructor + */ + constructor() { + super(); + + this.name = "URL Encode"; + this.module = "URL"; + this.description = "Encodes problematic characters into percent-encoding, a format supported by URIs/URLs.

e.g. = becomes %3d"; + this.infoURL = "https://wikipedia.org/wiki/Percent-encoding"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Encode all special chars", + "type": "boolean", + "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]; + 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; + } + + /** + * Encode characters in URL outside of encodeURI() function spec + * + * @param {string} str + * @returns {string} + */ + encodeAllChars (str) { + // 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; From 4e13b074c51f40f3358cbf77bbd8f5fc8ff4c1dc Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:28:51 +0800 Subject: [PATCH 3/7] renamed for clarity --- src/core/operations/URLEncode.mjs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index 45b64632..43426f01 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -43,10 +43,9 @@ class URLEncode extends Operation { * @returns {string} */ run(input, args) { - const encodeAll = args[0]; - const encodeAllEvery = args[1]; - return encodeAllEvery ? this.encodeAllEveryChars(input) : encodeAll ? this.encodeAllChars(input) : encodeURI(input); - //return encodeAll ? this.encodeAllChars(input) : encodeURI(input); + const encodeSpecial = args[0]; + const encodeAll = args[1]; + return encodeAll ? this.encodeAllChars(input) : encodeSpecial ? this.encodeAllSpecialChars(input) : encodeURI(input); } /** @@ -65,7 +64,7 @@ class URLEncode extends Operation { * @param {string} str * @returns {string} */ - encodeAllChars (str) { + encodeAllSpecialChars (str) { // TODO Do this programmatically return encodeURIComponent(str) .replace(/!/g, "%21") @@ -86,7 +85,7 @@ class URLEncode extends Operation { * @param {string} str * @returns {string} */ - encodeAllEveryChars (str) { + encodeAllChars (str) { let encoded = ""; for (var char of str) { encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0") From bd51fc05e320b07eb8dc6e69a4c4700d8ac114ed Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:46:02 +0800 Subject: [PATCH 4/7] did encodeAllSpecialChars programmatically --- src/core/operations/URLEncode.mjs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index 43426f01..f326d85f 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -65,18 +65,16 @@ class URLEncode extends Operation { * @returns {string} */ encodeAllSpecialChars (str) { - // 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"); + const specialChars = "!#'()*-._~"; + let encoded = ""; + for (var char of str) { + if (encodeURIComponent(char) === char && specialChars.includes(char)){ + encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); + } else { + encoded += encodeURIComponent(char); + } + } + return encoded; } /** From fa51addfd5abe8be3887723e770c000e60bdf315 Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:51:00 +0800 Subject: [PATCH 5/7] fix linting --- src/core/operations/URLEncode.mjs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index f326d85f..61a18d75 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -54,7 +54,7 @@ class URLEncode extends Operation { * @param {string} str * @returns {string} */ - frontPad (str, length, char){ + frontPad (str, length, char) { return str.length >= length ? str : (char * (length - str.length)) + str; } @@ -67,8 +67,8 @@ class URLEncode extends Operation { encodeAllSpecialChars (str) { const specialChars = "!#'()*-._~"; let encoded = ""; - for (var char of str) { - if (encodeURIComponent(char) === char && specialChars.includes(char)){ + for (let char of str) { + if (encodeURIComponent(char) === char && specialChars.includes(char)) { encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); } else { encoded += encodeURIComponent(char); @@ -85,8 +85,8 @@ class URLEncode extends Operation { */ encodeAllChars (str) { let encoded = ""; - for (var char of str) { - encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0") + for (let char of str) { + encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); } return encoded; } From afb9fd8408818c155f7568ddd0081dc62b52c895 Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:53:16 +0800 Subject: [PATCH 6/7] fix lint again --- src/core/operations/URLEncode.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index 61a18d75..27cd0403 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -67,7 +67,7 @@ class URLEncode extends Operation { encodeAllSpecialChars (str) { const specialChars = "!#'()*-._~"; let encoded = ""; - for (let char of str) { + for (const char of str) { if (encodeURIComponent(char) === char && specialChars.includes(char)) { encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); } else { @@ -85,7 +85,7 @@ class URLEncode extends Operation { */ encodeAllChars (str) { let encoded = ""; - for (let char of str) { + for (const char of str) { encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); } return encoded; From c19fe5cffad09995574676aa810a040aaebfb419 Mon Sep 17 00:00:00 2001 From: TheSavageTeddy <51810476+TheSavageTeddy@users.noreply.github.com> Date: Sun, 27 Nov 2022 14:25:18 +0800 Subject: [PATCH 7/7] uppercase constant --- src/core/operations/URLEncode.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/operations/URLEncode.mjs b/src/core/operations/URLEncode.mjs index 27cd0403..20c62339 100644 --- a/src/core/operations/URLEncode.mjs +++ b/src/core/operations/URLEncode.mjs @@ -65,10 +65,10 @@ class URLEncode extends Operation { * @returns {string} */ encodeAllSpecialChars (str) { - const specialChars = "!#'()*-._~"; + const SPECIAL_CHARS = "!#'()*-._~"; let encoded = ""; for (const char of str) { - if (encodeURIComponent(char) === char && specialChars.includes(char)) { + if (encodeURIComponent(char) === char && SPECIAL_CHARS.includes(char)) { encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0"); } else { encoded += encodeURIComponent(char);