From f2a5cf3496536a5e0dbaa071c37f74182f0ff74b Mon Sep 17 00:00:00 2001 From: avadootharajesh Date: Thu, 10 Jul 2025 17:50:32 +0530 Subject: [PATCH 1/2] Improved blacklist and whitelist function: added validation, escaping, and optimizations --- src/lib/blacklist.js | 18 +++++++++++++++++- src/lib/whitelist.js | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/lib/blacklist.js b/src/lib/blacklist.js index d796dbb10..e07183cc9 100644 --- a/src/lib/blacklist.js +++ b/src/lib/blacklist.js @@ -1,6 +1,22 @@ import assertString from './util/assertString'; +/** + * Removes all characters from `str` that appear in the `chars` string. + * + * @param {string} str - The input string to modify. + * @param {string} chars - A string containing characters to remove from `str`. + * @returns {string} - The modified string with characters from `chars` removed. + */ export default function blacklist(str, chars) { assertString(str); - return str.replace(new RegExp(`[${chars}]+`, 'g'), ''); + + if (typeof chars !== 'string') { + throw new Error('`chars` must be a string'); + } + + // Escape special characters for use in regex + const escapedChars = chars.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + + // Remove characters in `chars` from `str` + return str.replace(new RegExp(`[${escapedChars}]+`, 'g'), ''); } diff --git a/src/lib/whitelist.js b/src/lib/whitelist.js index 3df56e71b..f66d509da 100644 --- a/src/lib/whitelist.js +++ b/src/lib/whitelist.js @@ -1,6 +1,23 @@ import assertString from './util/assertString'; +/** + * Keeps only characters from `str` that appear in the `chars` string. + * + * @param {string} str - The input string to modify. + * @param {string} chars - A string containing characters to keep in `str`. + * @returns {string} - The modified string with only characters from `chars`. + * @throws {Error} - If `chars` is not a string. + */ export default function whitelist(str, chars) { assertString(str); - return str.replace(new RegExp(`[^${chars}]+`, 'g'), ''); + + if (typeof chars !== 'string') { + throw new Error('`chars` must be a string'); + } + + // Escape special characters for use in regex + const escapedChars = chars.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + + // Keep only characters in `chars` from `str` + return str.replace(new RegExp(`[^${escapedChars}]+`, 'g'), ''); } From 9399162a82a25a8c6e561a86220e0be087c18786 Mon Sep 17 00:00:00 2001 From: avadootharajesh Date: Thu, 10 Jul 2025 18:10:02 +0530 Subject: [PATCH 2/2] whitelist and blacklist utils error handling and performance improvement --- src/lib/blacklist.js | 3 --- src/lib/whitelist.js | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/lib/blacklist.js b/src/lib/blacklist.js index e07183cc9..c525b9961 100644 --- a/src/lib/blacklist.js +++ b/src/lib/blacklist.js @@ -9,14 +9,11 @@ import assertString from './util/assertString'; */ export default function blacklist(str, chars) { assertString(str); - if (typeof chars !== 'string') { throw new Error('`chars` must be a string'); } - // Escape special characters for use in regex const escapedChars = chars.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); - // Remove characters in `chars` from `str` return str.replace(new RegExp(`[${escapedChars}]+`, 'g'), ''); } diff --git a/src/lib/whitelist.js b/src/lib/whitelist.js index f66d509da..e60d3515c 100644 --- a/src/lib/whitelist.js +++ b/src/lib/whitelist.js @@ -10,14 +10,11 @@ import assertString from './util/assertString'; */ export default function whitelist(str, chars) { assertString(str); - if (typeof chars !== 'string') { throw new Error('`chars` must be a string'); } - // Escape special characters for use in regex const escapedChars = chars.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); - // Keep only characters in `chars` from `str` return str.replace(new RegExp(`[^${escapedChars}]+`, 'g'), ''); }