Convert an array of strings with optional wildcards, to an equivalent regular expression.
function settingsToRegExp(settings, baseRegExp, defaultRegExp) {
var regExpString = "";
if (settings instanceof Array && settings.length > 0) {
// Append base settings to user settings. The base
// settings are builtin and cannot be overridden.
if (baseRegExp) {
settings.push("/" + baseRegExp.source + "/");
}
// convert each string, with optional wildcards to an equivalent
// string in a regular expression.
settings.forEach(function (value, index) {
if (typeof value === "string") {
var isRegExp = value[0] === '/' && value[value.length - 1] === '/';
if (isRegExp) {
value = value.substring(1, value.length - 1);
} else {
value = StringUtils.regexEscape(value);
// convert user input wildcard, "*" or "?", to a regular
// expression. We can just replace the escaped "*" or "?"
// since we know it is a wildcard.
value = value.replace("\\?", ".?");
value = value.replace("\\*", ".*");
// Add "^" and "$" to prevent matching in the middle of strings.
value = "^" + value + "$";
}
if (index > 0) {
regExpString += "|";
}
regExpString = regExpString.concat(value);
}
});
}
if (!regExpString) {
var defaultParts = [];
if (baseRegExp) {
defaultParts.push(baseRegExp.source);
}
if (defaultRegExp) {
defaultParts.push(defaultRegExp.source);
}
if (defaultParts.length > 0) {
regExpString = defaultParts.join("|");
} else {
return null;
}
}
return new RegExp(regExpString);
}
Constructor to create a default preference object.
function Preferences(prefs) {
var BASE_EXCLUDED_DIRECTORIES = null,
Get the regular expression for excluded directories.
Preferences.prototype.getExcludedDirectories = function () {
return this._excludedDirectories;
};
Get the regular expression for excluded files.
Preferences.prototype.getExcludedFiles = function () {
return this._excludedFiles;
};