Skip to content
Merged
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [stripTags](#striptags)
- [stringular](#stringular)
- [test](#test)
- [match](#match)
- [trim](#trim)
- [ltrim](#ltrim)
- [rtrim](#rtrim)
Expand Down Expand Up @@ -1086,8 +1087,18 @@ Test if a string match a pattern<br/>
**Usage:** ```string | test: pattern: flag[optional]```
```html
<p>{{ '15/12/2003' | test: '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$': 'i' }}</p>
<p>{{ '0123456' | test: '\\D': 'i' }}</p>
<!--result:
true
true
```
###match
Return an array of matched element in a string<br/>
**Usage:** ```string | match: pattern: flag[optional]```
```html
<p>{{ '15/12/2003' | match: '\\d+': 'g' }}</p>
<!--result:
['15', '12', '2003']
```
#Math

Expand Down
24 changes: 23 additions & 1 deletion dist/angular-filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Bunch of useful filters for angularJS(with no external dependencies!)
* @version v0.5.3 - 2015-02-18 * @link https://github.com/a8m/angular-filter
* @version v0.5.3 - 2015-02-19 * @link https://github.com/a8m/angular-filter
* @author Ariel Mashraki <ariel@mashraki.co.il>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
Expand Down Expand Up @@ -1729,6 +1729,27 @@ angular.module('a8m.ltrim', [])
}
});

/**
* @ngdoc filter
* @name match
* @kind function
*
* @description
* Return the matched pattern in a string.
*/
angular.module('a8m.match', [])

.filter('match', function () {
return function (input, pattern, flag) {

var reg = new RegExp(pattern, flag);

return isString(input)
? input.match(reg)
: null;
}
});

/**
* @ngdoc filter
* @name repeat
Expand Down Expand Up @@ -2167,6 +2188,7 @@ angular.module('angular.filter', [
'a8m.rtrim',
'a8m.repeat',
'a8m.test',
'a8m.match',

'a8m.to-array',
'a8m.concat',
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-filter.min.js

Large diffs are not rendered by default.

Binary file modified dist/angular-filter.zip
Binary file not shown.
20 changes: 20 additions & 0 deletions src/_filter/string/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @ngdoc filter
* @name match
* @kind function
*
* @description
* Return the matched pattern in a string.
*/
angular.module('a8m.match', [])

.filter('match', function () {
return function (input, pattern, flag) {

var reg = new RegExp(pattern, flag);

return isString(input)
? input.match(reg)
: null;
}
});
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ angular.module('angular.filter', [
'a8m.rtrim',
'a8m.repeat',
'a8m.test',
'a8m.match',

'a8m.to-array',
'a8m.concat',
Expand Down
28 changes: 28 additions & 0 deletions test/spec/filter/string/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

describe('matchFilter', function () {

var filter;

beforeEach(module('a8m.match'));

beforeEach(inject(function ($filter) {
filter = $filter('match');
}));

it('should test a string with given pattern', function() {

expect(filter('15/12/2003', '\\d+', 'g')).toEqual(['15', '12', '2003']);
expect(angular.equals(filter('foobarbaz', '[a-z]{3}'), ['foo'])).toBeTruthy();
expect(filter('foobarbaz', '[a-z]{3}', 'g')).toEqual(['foo', 'bar', 'baz']);

});

it('should get a !string and return null', function() {
expect(filter({})).toEqual(null);
expect(filter([])).toEqual(null);
expect(filter(1)).toEqual(null);
expect(filter(!1)).toBeFalsy(null);
});

});
3 changes: 2 additions & 1 deletion test/spec/filter/string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('testFilter', function () {
expect(filter('foobarbaz', '^[a-z]{3,}$')).toEqual(true);
expect(filter('FOOBARBAZ', '^[a-z]{3,}$', 'i')).toEqual(true);
expect(filter('FOOBARBAZ', '^[a-z]{3,}$')).toEqual(false);
expect(filter('foobarbaz', '\W')).toEqual(false);
expect(filter('foobarbaz', '\\W')).toEqual(false);
expect(filter('foobarbaz', '\\w')).toEqual(true);
expect(filter('1a/bb/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i')).toEqual(false);

});
Expand Down