Skip to content

Commit 34ed91e

Browse files
author
Caitlin Potter
committed
feat(filters): implement isState and includedByState filters
Some global filters to help mitigate the writing of really bad code for ng-class expressions, among other things, in templates. Usage: "stateName" | includedByState --- Translates to $state.includes("stateName") "stateName" | isState --- Translates to $state.is("stateName") --- Such filters are trivial for an application to implement themselves, however people seem to not do this and prefer to write horrible verbose expressions instead --- Lets encourage the use of a powerful AngularJS feature instead.
1 parent f4002a9 commit 34ed91e

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

config/karma.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ files = [
2020
'src/state.js',
2121
'src/viewDirective.js',
2222
'src/stateDirectives.js',
23+
'src/stateFilters.js',
2324
'src/compat.js',
2425

2526
'test/*Spec.js',

src/stateFilters.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$IsStateFilter.$inject = ['$state'];
2+
function $IsStateFilter($state) {
3+
return function(state) {
4+
return $state.is(state);
5+
};
6+
}
7+
8+
$IncludedByStateFilter.$inject = ['$state'];
9+
function $IncludedByStateFilter($state) {
10+
return function(state) {
11+
return $state.includes(state);
12+
};
13+
}
14+
15+
angular.module('ui.router.state')
16+
.filter('isState', $IsStateFilter)
17+
.filter('includedByState', $IncludedByStateFilter);

test/stateFiltersSpec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
describe('isState filter', function() {
2+
beforeEach(module('ui.router'));
3+
beforeEach(module(function($stateProvider) {
4+
$stateProvider
5+
.state('a', { url: '/' })
6+
.state('a.b', { url: '/b' });
7+
}));
8+
9+
it('should return true if the current state exactly matches the input state', inject(function($parse, $state, $q, $rootScope) {
10+
$state.go('a');
11+
$q.flush();
12+
expect($parse('"a" | isState')($rootScope)).toBe(true);
13+
}));
14+
15+
it('should return false if the current state does not exactly match the input state', inject(function($parse, $q, $state, $rootScope) {
16+
$state.go('a.b');
17+
$q.flush();
18+
expect($parse('"a" | isState')($rootScope)).toBe(false);
19+
}));
20+
});
21+
22+
describe('includedByState filter', function() {
23+
beforeEach(module('ui.router'));
24+
beforeEach(module(function($stateProvider) {
25+
$stateProvider
26+
.state('a', { url: '/' })
27+
.state('a.b', { url: '/b' })
28+
.state('c', { url: '/c' });
29+
}));
30+
31+
it('should return true if the current state exactly matches the input state', inject(function($parse, $state, $q, $rootScope) {
32+
$state.go('a');
33+
$q.flush();
34+
expect($parse('"a" | includedByState')($rootScope)).toBe(true);
35+
}));
36+
37+
it('should return true if the current state includes the input state', inject(function($parse, $state, $q, $rootScope) {
38+
$state.go('a.b');
39+
$q.flush();
40+
expect($parse('"a" | includedByState')($rootScope)).toBe(true);
41+
}));
42+
43+
it('should return false if the current state does not include input state', inject(function($parse, $state, $q, $rootScope) {
44+
$state.go('c');
45+
$q.flush();
46+
expect($parse('"a" | includedByState')($rootScope)).toBe(false);
47+
}));
48+
});

0 commit comments

Comments
 (0)