Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(filter): allow map of filters to be registered in $FilterProvider #4091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ng/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ function $FilterProvider($provide) {
var suffix = 'Filter';

function register(name, factory) {
return $provide.factory(name + suffix, factory);
if(isObject(name)) {
forEach(name, function(filter, key) {
register(key, filter);
});
} else {
return $provide.factory(name + suffix, factory);
}
}
this.register = register;

Expand Down
53 changes: 53 additions & 0 deletions test/ng/filterSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

describe('$filter', function() {
var $filterProvider, $filter;

beforeEach(module(function(_$filterProvider_) {
$filterProvider = _$filterProvider_;
}));

beforeEach(inject(function(_$filter_) {
$filter = _$filter_;
}));

describe('provider', function() {
it('should allow registration of filters', function() {
var FooFilter = function() {
return function() {
return 'foo';
}
};

$filterProvider.register('foo', FooFilter);

var fooFilter = $filter('foo');
expect(fooFilter()).toBe('foo');
});

it('should allow registration of a map of filters', function() {
var FooFilter = function() {
return function() {
return 'foo';
}
};

var BarFilter = function() {
return function() {
return 'bar';
}
};

$filterProvider.register({
'foo': FooFilter,
'bar': BarFilter
});

var fooFilter = $filter('foo');
expect(fooFilter()).toBe('foo');

var barFilter = $filter('bar');
expect(barFilter()).toBe('bar');
});
});
});