JSFiddle - React, Tailwind, and code Playground
JavaScript
var app = angular.module("Cisco", []);
var UtilitiesService = function() {
/**
* Performs a wildcard matching for the text and pattern provided.
* If not wildcards characters found, will perform a simple strings equality check.
* Modified by Netcraft, inspired by http://www.adarshr.com/papers/wildcard
* @param text the text to be tested for matches.
* @param pattern the pattern to be matched for. This can contain the wildcard character '*' (asterisk).
* @return true if a match is found, false otherwise.
*/
var wildcardMatch = function(text, pattern) {
var patternHasAsterisk = pattern.indexOf("*") >= 0;
var result = patternHasAsterisk || (text === pattern);
// Do wildcard check only if there are asterisks in the string
if (patternHasAsterisk) {
// Create the cards
var cards = pattern.split("*");
// Iterate over the cards
cards.forEach(function(card) {
var idx = text.indexOf(card);
// Card not detected in the text.
if (idx === -1) {
result = false;
return;
}
// Move ahead, towards the right of the text
text = text.substring(idx + card.length);
});
}
return result;
};
// Public API
return {
wildcardMatch: wildcardMatch
};
};
return function(app) {
app.factory("utilities", UtilitiesService);
};