mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[ui/fieldWildcard] add lib to match names based on field-style wildcards
This commit is contained in:
parent
d4b9c116a6
commit
d75a63fa34
2 changed files with 76 additions and 0 deletions
65
src/ui/public/__tests__/field_wildcard.js
Normal file
65
src/ui/public/__tests__/field_wildcard.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
import expect from 'expect.js';
|
||||
|
||||
import { makeRegEx, fieldWildcardFilter } from '../field_wildcard';
|
||||
|
||||
describe('fieldWildcard', function () {
|
||||
describe('makeRegEx', function () {
|
||||
it('matches * in any position', function () {
|
||||
expect('aaaaaabbbbbbbcccccc').to.match(makeRegEx('*a*b*c*'));
|
||||
expect('a1234').to.match(makeRegEx('*1234'));
|
||||
expect('1234a').to.match(makeRegEx('1234*'));
|
||||
expect('12a34').to.match(makeRegEx('12a34'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('filter', function () {
|
||||
it('filters nothing when given an empty array', function () {
|
||||
const filter = fieldWildcardFilter([]);
|
||||
const original = [
|
||||
'foo',
|
||||
'bar',
|
||||
'baz',
|
||||
1234
|
||||
];
|
||||
|
||||
expect(original.filter(filter)).to.eql(original);
|
||||
});
|
||||
|
||||
it('filters values that match the globs', function () {
|
||||
const filter = fieldWildcardFilter([
|
||||
'f*',
|
||||
'*4'
|
||||
]);
|
||||
|
||||
const original = [
|
||||
'foo',
|
||||
'bar',
|
||||
'baz',
|
||||
1234
|
||||
];
|
||||
|
||||
expect(original.filter(filter)).to.eql(['bar', 'baz']);
|
||||
});
|
||||
|
||||
it('handles weird values okay', function () {
|
||||
const filter = fieldWildcardFilter([
|
||||
'f*',
|
||||
'*4',
|
||||
'undefined'
|
||||
]);
|
||||
|
||||
const original = [
|
||||
'foo',
|
||||
null,
|
||||
'bar',
|
||||
undefined,
|
||||
{},
|
||||
[],
|
||||
'baz',
|
||||
1234
|
||||
];
|
||||
|
||||
expect(original.filter(filter)).to.eql([null, 'bar', {}, [], 'baz']);
|
||||
});
|
||||
});
|
||||
});
|
11
src/ui/public/field_wildcard.js
Normal file
11
src/ui/public/field_wildcard.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { memoize } from 'lodash';
|
||||
|
||||
export const makeRegEx = memoize(function makeRegEx(glob) {
|
||||
return new RegExp(glob.replace(/\*/g, '.*'));
|
||||
});
|
||||
|
||||
export function fieldWildcardFilter(globs) {
|
||||
return function filter(val) {
|
||||
return !globs.some(p => makeRegEx(p).test(val));
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue