Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 62 additions & 66 deletions src/15utility.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jshint unused:false*/
/*jshint unused:false*/
/*
Utilities for Alasql.js

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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;
};

/**
Expand Down