From 0f9ae38d2ae91ca0cbd373dab1444091d05e73df Mon Sep 17 00:00:00 2001 From: Joshua Poon <93095025+JoshuaPoon1@users.noreply.github.com> Date: Thu, 8 Feb 2024 20:43:39 -0500 Subject: [PATCH 1/5] Update 15utility.js to improve readability. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Edited utils.glob and utils.like to improve readability. Proved comments to help reader traverse through code. Also rename: und ----> undefinedOrValue n2u ——> nanToUndefined --- src/15utility.js | 156 +++++++++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 65 deletions(-) diff --git a/src/15utility.js b/src/15utility.js index a1b3c95d61..8bb5abbff6 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -1,4 +1,4 @@ -/*jshint unused:false*/ + /*jshint unused:false*/ /* Utilities for Alasql.js @@ -25,7 +25,7 @@ var utils = (alasql.utils = {}); NaN => undefined */ -function n2u(s) { +function nanToUndefined(s) { //rename for clarity. return '(y=' + s + ',y===y?y:undefined)'; } @@ -41,7 +41,7 @@ function n2u(s) { NaN,a => undefined */ -function und(s, r) { +function undefinedOrValue(s, r) { //rename for clarity return '(y=' + s + ',typeof y=="undefined"?undefined:' + r + ')'; } @@ -1186,76 +1186,102 @@ var domEmptyChildren = (utils.domEmptyChildren = function (container) { @parameter {string} escape Escape character (optional) @return {boolean} If value LIKE pattern ESCAPE escape */ + +/* * Tests if a given value matches a pattern with optional escape character. + * Supports SQL-like syntax with % and _ as wildcards and custom escape character. + */ var patternCache = {}; -var like = (utils.like = function (pattern, value, escape) { +var like = (utils.like = function (pattern, value, escape = '') { if (!patternCache[pattern]) { - // Verify escape character - if (!escape) escape = ''; - - var i = 0; - var s = '^'; - - while (i < pattern.length) { - var c = pattern[i], - c1 = ''; - if (i < pattern.length - 1) c1 = pattern[i + 1]; - - if (c === escape) { - s += '\\' + c1; - i++; - } else if (c === '[' && c1 === '^') { - s += '[^'; - i++; - } else if (c === '[' || c === ']') { - s += c; - } else if (c === '%') { - s += '[\\s\\S]*'; - } else if (c === '_') { - s += '.'; - } else if ('/.*+?|(){}'.indexOf(c) > -1) { - s += '\\' + c; - } else { - s += c; - } - i++; - } - s += '$'; - // if(value == undefined) return false; - //console.log(s,value,(value||'').search(RegExp(s))>-1); - patternCache[pattern] = RegExp(s, 'i'); - } - return ('' + (value ?? '')).search(patternCache[pattern]) > -1; -}); + var regexStr = '^'; // Start regex pattern to match from the beginning. + var i = 0; // Index for traversing the pattern string. + + while (i < pattern.length) { + var currentChar = pattern[i]; + var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; + + // Handle escape character. + if (currentChar === escape) { + regexStr += '\\' + nextChar; + i++; // Skip next character as it's escaped. + } + // Handle negation within character classes. + else if (currentChar === '[' && nextChar === '^') { + regexStr += '[^'; + i++; // Include '^' as part of the set. + } + // Directly append square brackets. + else if (currentChar === '[' || currentChar === ']') { + regexStr += currentChar; + } + // Replace '%' with regex to match any character sequence. + else if (currentChar === '%') { + regexStr += '[\\s\\S]*'; + } + // Replace '_' with regex to match any single character. + else if (currentChar === '_') { + regexStr += '.'; + } + // Escape special regex characters. + else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + regexStr += '\\' + currentChar; + } + // Append literal characters. + else { + regexStr += currentChar; + } + i++; + } + + regexStr += '$'; // End regex pattern to match until the end. + // Compile and cache the regex pattern for future use. + patternCache[pattern] = RegExp(regexStr, 'i'); + } + + // Convert value to string (handling null/undefined) and test against compiled pattern. + return ('' + (value ?? '')).search(patternCache[pattern]) > -1; + }); +/** + * Tests if a given value matches a glob pattern. + * The function supports '*', '?' as wildcards where '*' matches any sequence of characters, + * and '?' matches any single character. Square brackets can be used for character sets and ranges. + * + * @param {string} value - The string value to test against the glob pattern. + * @param {string} pattern - The glob pattern to match the value against. + * @returns {boolean} - True if the value matches the pattern, false otherwise. + */ utils.glob = function (value, pattern) { - var i = 0; - var s = '^'; - - while (i < pattern.length) { - var c = pattern[i], - c1 = ''; - if (i < pattern.length - 1) c1 = pattern[i + 1]; - - if (c === '[' && c1 === '^') { - s += '[^'; - i++; - } else if (c === '[' || c === ']') { - s += c; - } else if (c === '*') { - s += '.*'; - } else if (c === '?') { - s += '.'; - } else if ('/.*+?|(){}'.indexOf(c) > -1) { - s += '\\' + c; - } else { - s += c; + var currentIndex = 0; // Index for traversing the pattern string. + var regexPattern = '^'; // Start regex pattern to match from the beginning. + + while (currentIndex < pattern.length) { + var currentChar = pattern[currentIndex]; + var nextChar = currentIndex < pattern.length - 1 ? pattern[currentIndex + 1] : ''; + + // Handle character sets and negation within them. + if (currentChar === '[' && nextChar === '^') { + regexPattern += '[^'; + currentIndex++; // Include '^' as part of the set. + } else if (currentChar === '[' || currentChar === ']') { // Directly append square brackets. + regexPattern += currentChar; + } else if (currentChar === '*') { // Replace '*' with regex to match any character sequence. + regexPattern += '.*'; + } else if (currentChar === '?') { // Replace '?' with regex to match any single character. + regexPattern += '.'; + } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {// Escape special regex characters. + regexPattern += '\\' + currentChar; + } else { // Append literal characters. + regexPattern += currentChar; } - i++; + currentIndex++; } - s += '$'; - return ('' + (value || '')).toUpperCase().search(RegExp(s.toUpperCase())) > -1; + regexPattern += '$'; // End regex pattern to match until the end. + + // Convert value to uppercase, compile the regex pattern in uppercase to perform a case-insensitive match. + return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; }; /** From 86f5adb2a446cea719c25cea02e997500f9486c9 Mon Sep 17 00:00:00 2001 From: Mathias Wulff Date: Thu, 11 Jul 2024 23:06:32 +0200 Subject: [PATCH 2/5] Update 15utility.js --- src/15utility.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/15utility.js b/src/15utility.js index 8bb5abbff6..901b1e0274 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -1,4 +1,4 @@ - /*jshint unused:false*/ +/*jshint unused:false*/ /* Utilities for Alasql.js From 571f4b4229432de0d1f2466f9204f0fcfc7d8f14 Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Sun, 30 Nov 2025 23:47:54 +1100 Subject: [PATCH 3/5] fmt --- src/15utility.js | 124 +++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/src/15utility.js b/src/15utility.js index 901b1e0274..3efdb3f2b8 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -25,7 +25,8 @@ var utils = (alasql.utils = {}); NaN => undefined */ -function nanToUndefined(s) { //rename for clarity. +function nanToUndefined(s) { + //rename for clarity. return '(y=' + s + ',y===y?y:undefined)'; } @@ -41,7 +42,8 @@ function nanToUndefined(s) { //rename for clarity. NaN,a => undefined */ -function undefinedOrValue(s, r) { //rename for clarity +function undefinedOrValue(s, r) { + //rename for clarity return '(y=' + s + ',typeof y=="undefined"?undefined:' + r + ')'; } @@ -1193,55 +1195,54 @@ var domEmptyChildren = (utils.domEmptyChildren = function (container) { var patternCache = {}; var like = (utils.like = function (pattern, value, escape = '') { if (!patternCache[pattern]) { + var regexStr = '^'; // Start regex pattern to match from the beginning. + var i = 0; // Index for traversing the pattern string. - var regexStr = '^'; // Start regex pattern to match from the beginning. - var i = 0; // Index for traversing the pattern string. - - while (i < pattern.length) { - var currentChar = pattern[i]; - var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; - - // Handle escape character. - if (currentChar === escape) { - regexStr += '\\' + nextChar; - i++; // Skip next character as it's escaped. - } - // Handle negation within character classes. - else if (currentChar === '[' && nextChar === '^') { - regexStr += '[^'; - i++; // Include '^' as part of the set. - } - // Directly append square brackets. - else if (currentChar === '[' || currentChar === ']') { - regexStr += currentChar; - } - // Replace '%' with regex to match any character sequence. - else if (currentChar === '%') { - regexStr += '[\\s\\S]*'; - } - // Replace '_' with regex to match any single character. - else if (currentChar === '_') { - regexStr += '.'; - } - // Escape special regex characters. - else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { - regexStr += '\\' + currentChar; - } - // Append literal characters. - else { - regexStr += currentChar; - } - i++; - } - - regexStr += '$'; // End regex pattern to match until the end. - // Compile and cache the regex pattern for future use. - patternCache[pattern] = RegExp(regexStr, 'i'); - } - - // Convert value to string (handling null/undefined) and test against compiled pattern. - return ('' + (value ?? '')).search(patternCache[pattern]) > -1; - }); + while (i < pattern.length) { + var currentChar = pattern[i]; + var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; + + // Handle escape character. + if (currentChar === escape) { + regexStr += '\\' + nextChar; + i++; // Skip next character as it's escaped. + } + // Handle negation within character classes. + else if (currentChar === '[' && nextChar === '^') { + regexStr += '[^'; + i++; // Include '^' as part of the set. + } + // Directly append square brackets. + else if (currentChar === '[' || currentChar === ']') { + regexStr += currentChar; + } + // Replace '%' with regex to match any character sequence. + else if (currentChar === '%') { + regexStr += '[\\s\\S]*'; + } + // Replace '_' with regex to match any single character. + else if (currentChar === '_') { + regexStr += '.'; + } + // Escape special regex characters. + else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + regexStr += '\\' + currentChar; + } + // Append literal characters. + else { + regexStr += currentChar; + } + i++; + } + + regexStr += '$'; // End regex pattern to match until the end. + // Compile and cache the regex pattern for future use. + patternCache[pattern] = RegExp(regexStr, 'i'); + } + + // Convert value to string (handling null/undefined) and test against compiled pattern. + return ('' + (value ?? '')).search(patternCache[pattern]) > -1; +}); /** * Tests if a given value matches a glob pattern. @@ -1256,32 +1257,37 @@ utils.glob = function (value, pattern) { var currentIndex = 0; // Index for traversing the pattern string. var regexPattern = '^'; // Start regex pattern to match from the beginning. - while (currentIndex < pattern.length) { + while (currentIndex < pattern.length) { var currentChar = pattern[currentIndex]; var nextChar = currentIndex < pattern.length - 1 ? pattern[currentIndex + 1] : ''; - + // Handle character sets and negation within them. if (currentChar === '[' && nextChar === '^') { regexPattern += '[^'; currentIndex++; // Include '^' as part of the set. - } else if (currentChar === '[' || currentChar === ']') { // Directly append square brackets. + } else if (currentChar === '[' || currentChar === ']') { + // Directly append square brackets. regexPattern += currentChar; - } else if (currentChar === '*') { // Replace '*' with regex to match any character sequence. + } else if (currentChar === '*') { + // Replace '*' with regex to match any character sequence. regexPattern += '.*'; - } else if (currentChar === '?') { // Replace '?' with regex to match any single character. + } else if (currentChar === '?') { + // Replace '?' with regex to match any single character. regexPattern += '.'; - } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {// Escape special regex characters. + } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + // Escape special regex characters. regexPattern += '\\' + currentChar; - } else { // Append literal characters. + } else { + // Append literal characters. regexPattern += currentChar; } currentIndex++; } - regexPattern += '$'; // End regex pattern to match until the end. + regexPattern += '$'; // End regex pattern to match until the end. // Convert value to uppercase, compile the regex pattern in uppercase to perform a case-insensitive match. - return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; + return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; }; /** From fe55000a09fc6422e522062237cd2bd582861502 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 13:04:33 +0000 Subject: [PATCH 4/5] Add backward compatibility aliases for renamed utility functions (n2u, und) Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/15utility.js | 128 +++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 66 deletions(-) diff --git a/src/15utility.js b/src/15utility.js index 3efdb3f2b8..c4ad3451ff 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -1,4 +1,4 @@ -/*jshint unused:false*/ + /*jshint unused:false*/ /* Utilities for Alasql.js @@ -25,10 +25,10 @@ var utils = (alasql.utils = {}); NaN => undefined */ -function nanToUndefined(s) { - //rename for clarity. +function nanToUndefined(s) { //rename for clarity. return '(y=' + s + ',y===y?y:undefined)'; } +var n2u = nanToUndefined; // Alias for backward compatibility /** Return undefined if s undefined @@ -42,10 +42,10 @@ function nanToUndefined(s) { NaN,a => undefined */ -function undefinedOrValue(s, r) { - //rename for clarity +function undefinedOrValue(s, r) { //rename for clarity return '(y=' + s + ',typeof y=="undefined"?undefined:' + r + ')'; } +var und = undefinedOrValue; // Alias for backward compatibility /** Return always true. Stub for non-ecisting WHERE clause, because is faster then if(whenrfn) whenfn() @@ -1195,54 +1195,55 @@ var domEmptyChildren = (utils.domEmptyChildren = function (container) { var patternCache = {}; var like = (utils.like = function (pattern, value, escape = '') { if (!patternCache[pattern]) { - var regexStr = '^'; // Start regex pattern to match from the beginning. - var i = 0; // Index for traversing the pattern string. - while (i < pattern.length) { - var currentChar = pattern[i]; - var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; - - // Handle escape character. - if (currentChar === escape) { - regexStr += '\\' + nextChar; - i++; // Skip next character as it's escaped. - } - // Handle negation within character classes. - else if (currentChar === '[' && nextChar === '^') { - regexStr += '[^'; - i++; // Include '^' as part of the set. - } - // Directly append square brackets. - else if (currentChar === '[' || currentChar === ']') { - regexStr += currentChar; - } - // Replace '%' with regex to match any character sequence. - else if (currentChar === '%') { - regexStr += '[\\s\\S]*'; - } - // Replace '_' with regex to match any single character. - else if (currentChar === '_') { - regexStr += '.'; - } - // Escape special regex characters. - else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { - regexStr += '\\' + currentChar; - } - // Append literal characters. - else { - regexStr += currentChar; - } - i++; - } - - regexStr += '$'; // End regex pattern to match until the end. - // Compile and cache the regex pattern for future use. - patternCache[pattern] = RegExp(regexStr, 'i'); - } - - // Convert value to string (handling null/undefined) and test against compiled pattern. - return ('' + (value ?? '')).search(patternCache[pattern]) > -1; -}); + var regexStr = '^'; // Start regex pattern to match from the beginning. + var i = 0; // Index for traversing the pattern string. + + while (i < pattern.length) { + var currentChar = pattern[i]; + var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; + + // Handle escape character. + if (currentChar === escape) { + regexStr += '\\' + nextChar; + i++; // Skip next character as it's escaped. + } + // Handle negation within character classes. + else if (currentChar === '[' && nextChar === '^') { + regexStr += '[^'; + i++; // Include '^' as part of the set. + } + // Directly append square brackets. + else if (currentChar === '[' || currentChar === ']') { + regexStr += currentChar; + } + // Replace '%' with regex to match any character sequence. + else if (currentChar === '%') { + regexStr += '[\\s\\S]*'; + } + // Replace '_' with regex to match any single character. + else if (currentChar === '_') { + regexStr += '.'; + } + // Escape special regex characters. + else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + regexStr += '\\' + currentChar; + } + // Append literal characters. + else { + regexStr += currentChar; + } + i++; + } + + regexStr += '$'; // End regex pattern to match until the end. + // Compile and cache the regex pattern for future use. + patternCache[pattern] = RegExp(regexStr, 'i'); + } + + // Convert value to string (handling null/undefined) and test against compiled pattern. + return ('' + (value ?? '')).search(patternCache[pattern]) > -1; + }); /** * Tests if a given value matches a glob pattern. @@ -1257,37 +1258,32 @@ utils.glob = function (value, pattern) { var currentIndex = 0; // Index for traversing the pattern string. var regexPattern = '^'; // Start regex pattern to match from the beginning. - while (currentIndex < pattern.length) { + while (currentIndex < pattern.length) { var currentChar = pattern[currentIndex]; var nextChar = currentIndex < pattern.length - 1 ? pattern[currentIndex + 1] : ''; - + // Handle character sets and negation within them. if (currentChar === '[' && nextChar === '^') { regexPattern += '[^'; currentIndex++; // Include '^' as part of the set. - } else if (currentChar === '[' || currentChar === ']') { - // Directly append square brackets. + } else if (currentChar === '[' || currentChar === ']') { // Directly append square brackets. regexPattern += currentChar; - } else if (currentChar === '*') { - // Replace '*' with regex to match any character sequence. + } else if (currentChar === '*') { // Replace '*' with regex to match any character sequence. regexPattern += '.*'; - } else if (currentChar === '?') { - // Replace '?' with regex to match any single character. + } else if (currentChar === '?') { // Replace '?' with regex to match any single character. regexPattern += '.'; - } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { - // Escape special regex characters. + } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {// Escape special regex characters. regexPattern += '\\' + currentChar; - } else { - // Append literal characters. + } else { // Append literal characters. regexPattern += currentChar; } currentIndex++; } - regexPattern += '$'; // End regex pattern to match until the end. + regexPattern += '$'; // End regex pattern to match until the end. // Convert value to uppercase, compile the regex pattern in uppercase to perform a case-insensitive match. - return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; + return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; }; /** From 7ad3b53cfb52ec3adc21bf73c82576e2a5c87ff3 Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Mon, 1 Dec 2025 00:49:02 +1100 Subject: [PATCH 5/5] fmt --- src/15utility.js | 126 +++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 60 deletions(-) diff --git a/src/15utility.js b/src/15utility.js index c4ad3451ff..3de55a7161 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -1,4 +1,4 @@ - /*jshint unused:false*/ +/*jshint unused:false*/ /* Utilities for Alasql.js @@ -25,7 +25,8 @@ var utils = (alasql.utils = {}); NaN => undefined */ -function nanToUndefined(s) { //rename for clarity. +function nanToUndefined(s) { + //rename for clarity. return '(y=' + s + ',y===y?y:undefined)'; } var n2u = nanToUndefined; // Alias for backward compatibility @@ -42,7 +43,8 @@ var n2u = nanToUndefined; // Alias for backward compatibility NaN,a => undefined */ -function undefinedOrValue(s, r) { //rename for clarity +function undefinedOrValue(s, r) { + //rename for clarity return '(y=' + s + ',typeof y=="undefined"?undefined:' + r + ')'; } var und = undefinedOrValue; // Alias for backward compatibility @@ -1195,55 +1197,54 @@ var domEmptyChildren = (utils.domEmptyChildren = function (container) { var patternCache = {}; var like = (utils.like = function (pattern, value, escape = '') { if (!patternCache[pattern]) { + var regexStr = '^'; // Start regex pattern to match from the beginning. + var i = 0; // Index for traversing the pattern string. - var regexStr = '^'; // Start regex pattern to match from the beginning. - var i = 0; // Index for traversing the pattern string. - - while (i < pattern.length) { - var currentChar = pattern[i]; - var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; - - // Handle escape character. - if (currentChar === escape) { - regexStr += '\\' + nextChar; - i++; // Skip next character as it's escaped. - } - // Handle negation within character classes. - else if (currentChar === '[' && nextChar === '^') { - regexStr += '[^'; - i++; // Include '^' as part of the set. - } - // Directly append square brackets. - else if (currentChar === '[' || currentChar === ']') { - regexStr += currentChar; - } - // Replace '%' with regex to match any character sequence. - else if (currentChar === '%') { - regexStr += '[\\s\\S]*'; - } - // Replace '_' with regex to match any single character. - else if (currentChar === '_') { - regexStr += '.'; - } - // Escape special regex characters. - else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { - regexStr += '\\' + currentChar; - } - // Append literal characters. - else { - regexStr += currentChar; - } - i++; - } - - regexStr += '$'; // End regex pattern to match until the end. - // Compile and cache the regex pattern for future use. - patternCache[pattern] = RegExp(regexStr, 'i'); - } - - // Convert value to string (handling null/undefined) and test against compiled pattern. - return ('' + (value ?? '')).search(patternCache[pattern]) > -1; - }); + while (i < pattern.length) { + var currentChar = pattern[i]; + var nextChar = i < pattern.length - 1 ? pattern[i + 1] : ''; + + // Handle escape character. + if (currentChar === escape) { + regexStr += '\\' + nextChar; + i++; // Skip next character as it's escaped. + } + // Handle negation within character classes. + else if (currentChar === '[' && nextChar === '^') { + regexStr += '[^'; + i++; // Include '^' as part of the set. + } + // Directly append square brackets. + else if (currentChar === '[' || currentChar === ']') { + regexStr += currentChar; + } + // Replace '%' with regex to match any character sequence. + else if (currentChar === '%') { + regexStr += '[\\s\\S]*'; + } + // Replace '_' with regex to match any single character. + else if (currentChar === '_') { + regexStr += '.'; + } + // Escape special regex characters. + else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + regexStr += '\\' + currentChar; + } + // Append literal characters. + else { + regexStr += currentChar; + } + i++; + } + + regexStr += '$'; // End regex pattern to match until the end. + // Compile and cache the regex pattern for future use. + patternCache[pattern] = RegExp(regexStr, 'i'); + } + + // Convert value to string (handling null/undefined) and test against compiled pattern. + return ('' + (value ?? '')).search(patternCache[pattern]) > -1; +}); /** * Tests if a given value matches a glob pattern. @@ -1258,32 +1259,37 @@ utils.glob = function (value, pattern) { var currentIndex = 0; // Index for traversing the pattern string. var regexPattern = '^'; // Start regex pattern to match from the beginning. - while (currentIndex < pattern.length) { + while (currentIndex < pattern.length) { var currentChar = pattern[currentIndex]; var nextChar = currentIndex < pattern.length - 1 ? pattern[currentIndex + 1] : ''; - + // Handle character sets and negation within them. if (currentChar === '[' && nextChar === '^') { regexPattern += '[^'; currentIndex++; // Include '^' as part of the set. - } else if (currentChar === '[' || currentChar === ']') { // Directly append square brackets. + } else if (currentChar === '[' || currentChar === ']') { + // Directly append square brackets. regexPattern += currentChar; - } else if (currentChar === '*') { // Replace '*' with regex to match any character sequence. + } else if (currentChar === '*') { + // Replace '*' with regex to match any character sequence. regexPattern += '.*'; - } else if (currentChar === '?') { // Replace '?' with regex to match any single character. + } else if (currentChar === '?') { + // Replace '?' with regex to match any single character. regexPattern += '.'; - } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {// Escape special regex characters. + } else if ('/.*+?|(){}'.indexOf(currentChar) > -1) { + // Escape special regex characters. regexPattern += '\\' + currentChar; - } else { // Append literal characters. + } else { + // Append literal characters. regexPattern += currentChar; } currentIndex++; } - regexPattern += '$'; // End regex pattern to match until the end. + regexPattern += '$'; // End regex pattern to match until the end. // Convert value to uppercase, compile the regex pattern in uppercase to perform a case-insensitive match. - return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; + return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1; }; /**