Merge "Introduce regex filter"
This commit is contained in:
commit
b0aa5e69e3
30
app/js/filters/regex.js
Normal file
30
app/js/filters/regex.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
var filtersModule = require('./_index.js');
|
||||
|
||||
/**
|
||||
* @ngInject
|
||||
*/
|
||||
function regex($filter) {
|
||||
return function(input, field, regex) {
|
||||
if (!input) {
|
||||
return [];
|
||||
}
|
||||
var pattern = null;
|
||||
var out = [];
|
||||
try {
|
||||
pattern = new RegExp(regex);
|
||||
} catch (e) {
|
||||
return input;
|
||||
}
|
||||
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
if (pattern.test(input[i][field])) {
|
||||
out.push(input[i]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
}
|
||||
|
||||
filtersModule.filter('regex', regex);
|
@ -105,7 +105,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr table-ref="table" ng-repeat="p in home.projects | filter:{name:home.searchProject}"
|
||||
<tr table-ref="table" ng-repeat="p in home.projects | regex:'name':home.searchProject"
|
||||
ng-class="p.failRate | ctxcls">
|
||||
<td class="text-right">{{$index+1}}</td>
|
||||
<td class="text-left">
|
||||
|
47
test/unit/filters/regex_spec.js
Normal file
47
test/unit/filters/regex_spec.js
Normal file
@ -0,0 +1,47 @@
|
||||
describe('Regex Filter', function() {
|
||||
|
||||
var regexFilter;
|
||||
var input = [{'id': 1, 'name': 'foo'},
|
||||
{'id': 2, 'name': 'bar'},
|
||||
{'id': 3, 'name': 'koo'}];
|
||||
|
||||
beforeEach(function() {
|
||||
module('app');
|
||||
module('app.filters');
|
||||
});
|
||||
|
||||
beforeEach(inject(function(_regexFilter_) {
|
||||
regexFilter = _regexFilter_;
|
||||
}));
|
||||
|
||||
it('should get a object correctly', function() {
|
||||
expect(regexFilter(input, 'name', 'foo')).toEqual([
|
||||
{'id':1, 'name': 'foo'}]);
|
||||
});
|
||||
|
||||
it('should get 2 objects correctly', function() {
|
||||
expect(regexFilter(input, 'name', 'foo|koo')).toEqual([
|
||||
{'id':1, 'name': 'foo'},
|
||||
{'id':3, 'name': 'koo'}]);
|
||||
});
|
||||
|
||||
it('should get no object correctly', function() {
|
||||
expect(regexFilter(input, 'name', 'baar')).toEqual([]);
|
||||
});
|
||||
|
||||
it('should get objects correctly', function() {
|
||||
expect(regexFilter(input, 'name', 'f|b|k')).toEqual([
|
||||
{'id': 1, 'name': 'foo'},
|
||||
{'id': 2, 'name': 'bar'},
|
||||
{'id': 3, 'name': 'koo'}
|
||||
]);
|
||||
});
|
||||
|
||||
it('should get all objects correctly', function() {
|
||||
expect(regexFilter(input, 'name', '')).toEqual([
|
||||
{'id': 1, 'name': 'foo'},
|
||||
{'id': 2, 'name': 'bar'},
|
||||
{'id': 3, 'name': 'koo'}
|
||||
]);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user