Skip to content

Commit a5f3e9b

Browse files
mathiasrwJoshuaPoon1Copilot
authored
improve readability of 15utility.js (#2284)
Co-authored-by: Joshua Poon <93095025+JoshuaPoon1@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
1 parent adfdebc commit a5f3e9b

File tree

1 file changed

+89
-55
lines changed

1 file changed

+89
-55
lines changed

src/15utility.js

Lines changed: 89 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ var utils = (alasql.utils = {});
2525
NaN => undefined
2626
2727
*/
28-
function n2u(s) {
28+
function nanToUndefined(s) {
29+
//rename for clarity.
2930
return '(y=' + s + ',y===y?y:undefined)';
3031
}
32+
var n2u = nanToUndefined; // Alias for backward compatibility
3133

3234
/**
3335
Return undefined if s undefined
@@ -41,9 +43,11 @@ function n2u(s) {
4143
NaN,a => undefined
4244
4345
*/
44-
function und(s, r) {
46+
function undefinedOrValue(s, r) {
47+
//rename for clarity
4548
return '(y=' + s + ',typeof y=="undefined"?undefined:' + r + ')';
4649
}
50+
var und = undefinedOrValue; // Alias for backward compatibility
4751

4852
/**
4953
Return always true. Stub for non-ecisting WHERE clause, because is faster then if(whenrfn) whenfn()
@@ -1186,76 +1190,106 @@ var domEmptyChildren = (utils.domEmptyChildren = function (container) {
11861190
@parameter {string} escape Escape character (optional)
11871191
@return {boolean} If value LIKE pattern ESCAPE escape
11881192
*/
1193+
1194+
/* * Tests if a given value matches a pattern with optional escape character.
1195+
* Supports SQL-like syntax with % and _ as wildcards and custom escape character.
1196+
*/
11891197
var patternCache = {};
1190-
var like = (utils.like = function (pattern, value, escape) {
1198+
var like = (utils.like = function (pattern, value, escape = '') {
11911199
if (!patternCache[pattern]) {
1192-
// Verify escape character
1193-
if (!escape) escape = '';
1194-
1195-
var i = 0;
1196-
var s = '^';
1200+
var regexStr = '^'; // Start regex pattern to match from the beginning.
1201+
var i = 0; // Index for traversing the pattern string.
11971202

11981203
while (i < pattern.length) {
1199-
var c = pattern[i],
1200-
c1 = '';
1201-
if (i < pattern.length - 1) c1 = pattern[i + 1];
1202-
1203-
if (c === escape) {
1204-
s += '\\' + c1;
1205-
i++;
1206-
} else if (c === '[' && c1 === '^') {
1207-
s += '[^';
1208-
i++;
1209-
} else if (c === '[' || c === ']') {
1210-
s += c;
1211-
} else if (c === '%') {
1212-
s += '[\\s\\S]*';
1213-
} else if (c === '_') {
1214-
s += '.';
1215-
} else if ('/.*+?|(){}'.indexOf(c) > -1) {
1216-
s += '\\' + c;
1217-
} else {
1218-
s += c;
1204+
var currentChar = pattern[i];
1205+
var nextChar = i < pattern.length - 1 ? pattern[i + 1] : '';
1206+
1207+
// Handle escape character.
1208+
if (currentChar === escape) {
1209+
regexStr += '\\' + nextChar;
1210+
i++; // Skip next character as it's escaped.
1211+
}
1212+
// Handle negation within character classes.
1213+
else if (currentChar === '[' && nextChar === '^') {
1214+
regexStr += '[^';
1215+
i++; // Include '^' as part of the set.
1216+
}
1217+
// Directly append square brackets.
1218+
else if (currentChar === '[' || currentChar === ']') {
1219+
regexStr += currentChar;
1220+
}
1221+
// Replace '%' with regex to match any character sequence.
1222+
else if (currentChar === '%') {
1223+
regexStr += '[\\s\\S]*';
1224+
}
1225+
// Replace '_' with regex to match any single character.
1226+
else if (currentChar === '_') {
1227+
regexStr += '.';
1228+
}
1229+
// Escape special regex characters.
1230+
else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {
1231+
regexStr += '\\' + currentChar;
1232+
}
1233+
// Append literal characters.
1234+
else {
1235+
regexStr += currentChar;
12191236
}
12201237
i++;
12211238
}
12221239

1223-
s += '$';
1224-
// if(value == undefined) return false;
1225-
//console.log(s,value,(value||'').search(RegExp(s))>-1);
1226-
patternCache[pattern] = RegExp(s, 'i');
1240+
regexStr += '$'; // End regex pattern to match until the end.
1241+
// Compile and cache the regex pattern for future use.
1242+
patternCache[pattern] = RegExp(regexStr, 'i');
12271243
}
1244+
1245+
// Convert value to string (handling null/undefined) and test against compiled pattern.
12281246
return ('' + (value ?? '')).search(patternCache[pattern]) > -1;
12291247
});
12301248

1249+
/**
1250+
* Tests if a given value matches a glob pattern.
1251+
* The function supports '*', '?' as wildcards where '*' matches any sequence of characters,
1252+
* and '?' matches any single character. Square brackets can be used for character sets and ranges.
1253+
*
1254+
* @param {string} value - The string value to test against the glob pattern.
1255+
* @param {string} pattern - The glob pattern to match the value against.
1256+
* @returns {boolean} - True if the value matches the pattern, false otherwise.
1257+
*/
12311258
utils.glob = function (value, pattern) {
1232-
var i = 0;
1233-
var s = '^';
1234-
1235-
while (i < pattern.length) {
1236-
var c = pattern[i],
1237-
c1 = '';
1238-
if (i < pattern.length - 1) c1 = pattern[i + 1];
1239-
1240-
if (c === '[' && c1 === '^') {
1241-
s += '[^';
1242-
i++;
1243-
} else if (c === '[' || c === ']') {
1244-
s += c;
1245-
} else if (c === '*') {
1246-
s += '.*';
1247-
} else if (c === '?') {
1248-
s += '.';
1249-
} else if ('/.*+?|(){}'.indexOf(c) > -1) {
1250-
s += '\\' + c;
1259+
var currentIndex = 0; // Index for traversing the pattern string.
1260+
var regexPattern = '^'; // Start regex pattern to match from the beginning.
1261+
1262+
while (currentIndex < pattern.length) {
1263+
var currentChar = pattern[currentIndex];
1264+
var nextChar = currentIndex < pattern.length - 1 ? pattern[currentIndex + 1] : '';
1265+
1266+
// Handle character sets and negation within them.
1267+
if (currentChar === '[' && nextChar === '^') {
1268+
regexPattern += '[^';
1269+
currentIndex++; // Include '^' as part of the set.
1270+
} else if (currentChar === '[' || currentChar === ']') {
1271+
// Directly append square brackets.
1272+
regexPattern += currentChar;
1273+
} else if (currentChar === '*') {
1274+
// Replace '*' with regex to match any character sequence.
1275+
regexPattern += '.*';
1276+
} else if (currentChar === '?') {
1277+
// Replace '?' with regex to match any single character.
1278+
regexPattern += '.';
1279+
} else if ('/.*+?|(){}'.indexOf(currentChar) > -1) {
1280+
// Escape special regex characters.
1281+
regexPattern += '\\' + currentChar;
12511282
} else {
1252-
s += c;
1283+
// Append literal characters.
1284+
regexPattern += currentChar;
12531285
}
1254-
i++;
1286+
currentIndex++;
12551287
}
12561288

1257-
s += '$';
1258-
return ('' + (value || '')).toUpperCase().search(RegExp(s.toUpperCase())) > -1;
1289+
regexPattern += '$'; // End regex pattern to match until the end.
1290+
1291+
// Convert value to uppercase, compile the regex pattern in uppercase to perform a case-insensitive match.
1292+
return ('' + (value || '')).toUpperCase().search(RegExp(regexPattern.toUpperCase())) > -1;
12591293
};
12601294

12611295
/**

0 commit comments

Comments
 (0)