Skip to content

Commit

Permalink
Only include necessary comparators and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bargs committed Nov 22, 2016
1 parent 79e69bd commit 5b9137b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/ui/public/filter_manager/lib/__tests__/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe('Filter Manager', function () {
expect(fn(indexPattern.fields.byName['script number'], {gte: 1, lte: 3}, indexPattern)).to.eql(expected);
});

it('should wrap painless scripts in comparator lambdas', function () {
const expected = `boolean gte(Supplier s, def v) {return s.get() >= v} ` +
`boolean lte(Supplier s, def v) {return s.get() <= v}` +
`gte(() -> { ${indexPattern.fields.byName['script date'].script} }, params.gte) && ` +
`lte(() -> { ${indexPattern.fields.byName['script date'].script} }, params.lte)`;

const inlineScript = fn(indexPattern.fields.byName['script date'], {gte: 1, lte: 3}, indexPattern).script.script.inline;
expect(inlineScript).to.be(expected);
});

it('should throw an error when gte and gt, or lte and lt are both passed', function () {
expect(function () {
fn(indexPattern.fields.byName['script number'], {gte: 1, gt: 3}, indexPattern);
Expand Down
13 changes: 8 additions & 5 deletions src/ui/public/filter_manager/lib/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export default function buildRangeFilter(field, params, indexPattern, formattedV
lte: '<=',
lt: '<',
};
const comparators = {
gt: 'boolean gt(Supplier s, def v) {return s.get() > v}',
gte: 'boolean gte(Supplier s, def v) {return s.get() >= v}',
lte: 'boolean lte(Supplier s, def v) {return s.get() <= v}',
lt: 'boolean lt(Supplier s, def v) {return s.get() < v}',
};

const knownParams = _.pick(params, (val, key) => { return key in operators; });
let script = _.map(knownParams, function (val, key) {
Expand All @@ -40,16 +46,13 @@ export default function buildRangeFilter(field, params, indexPattern, formattedV

// We must wrap painless scripts in a lambda in case they're more than a simple expression
if (field.lang === 'painless') {
const comparators = `boolean gt(Supplier s, def v) {return s.get() > v;}
boolean gte(Supplier s, def v) {return s.get() >= v;}
boolean lte(Supplier s, def v) {return s.get() <= v;}
boolean lt(Supplier s, def v) {return s.get() < v;}`;
const currentComparators = _.reduce(knownParams, (acc, val, key) => acc.concat(comparators[key]), []).join(' ');

const comparisons = _.map(knownParams, function (val, key) {
return `${key}(() -> { ${field.script} }, params.${key})`;
}).join(' && ');

script = `${comparators}${comparisons}`;
script = `${currentComparators}${comparisons}`;
}

const value = _.map(knownParams, function (val, key) {
Expand Down

0 comments on commit 5b9137b

Please sign in to comment.