Filters are functions which transform the data into a user readable format. They are important because they remove the formatting responsibility from the application logic, further simplifying the application logic.
myModule.filter(‘length’, function() {
return function(text) {
return (” + (text || ”)).length;
}
});
describe(‘length filter’, function() {
var $filter;
beforeEach(inject(function(_$filter_){
$filter = _$filter_;
}));
it(‘returns 0 when given null’, function() {
var length = $filter(‘length’);
expect(length(null)).toEqual(0);
});
it(‘returns the correct value when given a string of chars’, function() {
var length = $filter(‘length’);
expect(length(‘abc’)).toEqual(3);
});
});