Merge "Introduce regex filter"

This commit is contained in:
Jenkins 2016-03-16 18:18:28 +00:00 committed by Gerrit Code Review
commit b0aa5e69e3
3 changed files with 78 additions and 1 deletions

30
app/js/filters/regex.js Normal file
View 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);

View File

@ -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">

View 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'}
]);
});
});