Skip to content

Commit 0b2b320

Browse files
committed
Add string test filter (cf #68)
1 parent d28dece commit 0b2b320

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,14 @@ Repeats a string n times<br/>
10801080
<!--repeat:
10811081
foo-foo-foo
10821082
```
1083+
###test
1084+
Test if a string match a pattern<br/>
1085+
**Usage:** ```string | test: pattern: flag[optional]```
1086+
```html
1087+
<p>{{ '15/12/2003' | test: '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$': 'i' }}</p>
1088+
<!--result:
1089+
true
1090+
```
10831091
#Math
10841092
10851093
###max

src/_filter/string/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @ngdoc filter
3+
* @name test
4+
* @kind function
5+
*
6+
* @description
7+
* test if a string match a pattern.
8+
*/
9+
angular.module('a8m.test', [])
10+
11+
.filter('test', function () {
12+
return function (input, pattern, flag) {
13+
14+
var reg = new RegExp(pattern, flag);
15+
16+
return isString(input)
17+
? reg.test(input)
18+
: input;
19+
}
20+
});

src/filters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ angular.module('angular.filter', [
2222
'a8m.ltrim',
2323
'a8m.rtrim',
2424
'a8m.repeat',
25+
'a8m.test',
2526

2627
'a8m.to-array',
2728
'a8m.concat',

test/spec/filter/string/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
describe('testFilter', function () {
4+
5+
var filter;
6+
7+
beforeEach(module('a8m.test'));
8+
9+
beforeEach(inject(function ($filter) {
10+
filter = $filter('test');
11+
}));
12+
13+
it('should test a string with given pattern', function() {
14+
15+
expect(filter('15/12/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i')).toEqual(true);
16+
expect(filter('foobarbaz', '^[a-z]{3,}$')).toEqual(true);
17+
expect(filter('FOOBARBAZ', '^[a-z]{3,}$', 'i')).toEqual(true);
18+
expect(filter('FOOBARBAZ', '^[a-z]{3,}$')).toEqual(false);
19+
expect(filter('foobarbaz', '\W')).toEqual(false);
20+
expect(filter('1a/bb/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i')).toEqual(false);
21+
22+
});
23+
24+
it('should get a !string and not touch it', function() {
25+
expect(filter({})).toEqual({});
26+
expect(filter([])).toEqual([]);
27+
expect(filter(1)).toEqual(1);
28+
expect(filter(!1)).toBeFalsy();
29+
});
30+
31+
});

0 commit comments

Comments
 (0)