Skip to content

Instantly share code, notes, and snippets.

@zymr-keshav
Last active August 31, 2017 19:15
Show Gist options
  • Save zymr-keshav/2377d5d46dafde1e2c474b60152c4696 to your computer and use it in GitHub Desktop.
Save zymr-keshav/2377d5d46dafde1e2c474b60152c4696 to your computer and use it in GitHub Desktop.
useful angulr basic filters
(function() {
'use strict';
angular
.module('app')
.filter('STRING_LIMIT_FILTER', function() {
return function(input, num) {
let output = input.split(' ').slice(0, num).join(' ');
return output;
};
})
.filter('FIRST_CAPS', function() {
return function(input) {
let output = input ? (input.charAt(0).toUpperCase() + input.substr(1).toLowerCase()) : '';
return output;
};
})
.filter('isEmpty', function() {
return function(object) {
return angular.equals({}, object);
};
})
.filter('fromNow', function() { // require moment.js
return function(input) {
return moment(input).fromNow();
};
})
.filter('htmlDecode', function () {
// can use as `let output = $filter('htmlDecode')(inputString)` in JS
return function (input) { // input is string with html entities such as …
let doc = new DOMParser().parseFromString(input, "text/html");
let output = doc.documentElement.textContent;
return output;
};
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment