Skip to content

Instantly share code, notes, and snippets.

@elgervb
Last active April 7, 2016 14:11
Show Gist options
  • Save elgervb/a6a36af0bc24927474baa82539c69c40 to your computer and use it in GitHub Desktop.
Save elgervb/a6a36af0bc24927474baa82539c69c40 to your computer and use it in GitHub Desktop.
TypeScript angular 1.5 filter
/**
* Wraps a match inside a string between <b>...</b> tags
*
* Example:
* $filter('filtername').('match');
*
* Note: make sure you'll render this using ng-bind-html (in a repeater you'll probably must use ng-if as well, or otherwise all nodes will be rendered (empty when no match))
*/
class MarkSelectedFilter {
public static factory() {
let factory = ($sce: ng.ISCEService) => {
return (input: string, match: string) => {
let regex = new RegExp(`(${match})`, 'gi');
if (regex.test(input)) {
return $sce.trustAsHtml(input.replace(regex, '<b>$1</b>'));
}
return null;
}
};
factory.$inject = ['$sce'];
return factory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment