Skip to content

Instantly share code, notes, and snippets.

@camd
Created November 22, 2017 16:37
Show Gist options
  • Save camd/d8033c7fbe75b295f054b9509cbf760c to your computer and use it in GitHub Desktop.
Save camd/d8033c7fbe75b295f054b9509cbf760c to your computer and use it in GitHub Desktop.
treeherder.filter('highlightCommonTerms2', function () {
return function (input) {
const compareStr = Array.prototype.slice.call(arguments, 1).filter(x => x).join(" ");
let tokens = compareStr.split(/[^a-zA-Z0-9]+/).map(item => item.toLowerCase());
tokens = [ ...new Set(tokens) ]; // remove duplicates
console.log("tokens", tokens);
const subs = tokens.reduce((subAcc, token) => {
let innerSubs = tokens.filter(innerToken => (innerToken.includes(token)));
let substr = innerSubs.filter(sub => sub !== token);
return [ ...subAcc, ...substr ];
}, []);
// we would need to split the subs that are substrings of one another
// and make them their own terms to highlight. But then what if that
// new substring belonged to another term that shouldn't be highlighted.
console.log("subs", subs);
// var tokens = compareStr.split(/[^a-zA-Z0-9_-]+/);
tokens.sort(function (a, b) {
return b.length - a.length;
});
angular.forEach(tokens, function (elem) {
if (elem.length > 0) {
input = input.replace(new RegExp("([<strong>]|^|\\W|)(" + elem + ")([</strong>]|$|\\W|)", "gi"), function (match, prefix, token, suffix) {
return prefix + "<strong>" + token + "</strong>" + suffix;
});
}
});
console.log("input after", input);
return input;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment