Skip to content

Commit

Permalink
feat(view): Add generic match leaf view
Browse files Browse the repository at this point in the history
This view can be instantiated many times
  • Loading branch information
orangejulius committed Sep 10, 2019
1 parent 05d7c93 commit b650df8
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports.layout = {
module.exports.view = {
leaf: {
match_all: require('./view/leaf/match_all'),
match_phrase: require('./view/leaf/match_phrase')
match_phrase: require('./view/leaf/match_phrase'),
match: require('./view/leaf/match')
},
focus: require('./view/focus'),
focus_only_function: require('./view/focus_only_function'),
Expand Down
1 change: 1 addition & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var tests = [
require('./view/sort_distance.js'),
require('./view/sources.js'),
require('./view/boundary_gid.js'),
require('./view/leaf/match.js'),
require('./view/leaf/match_phrase.js')
];

Expand Down
86 changes: 86 additions & 0 deletions test/view/leaf/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const VariableStore = require('../../../lib/VariableStore');
const match = require('../../../view/leaf/match');

module.exports.tests = {};

module.exports.tests.base_usage = function(test, common) {
test('input and field specified', function(t) {
const vs = new VariableStore();

vs.var('match:example:input', 'input value');
vs.var('match:example:field', 'field value');

const view = match('example')(vs);

const actual = JSON.parse(JSON.stringify(view));

const expected = {
match: {
['field value']: {
query: 'input value'
}
}
};

t.deepEqual(actual, expected, 'match view rendered as expected');
t.end();
});

test('optional parameters specified', function(t) {
const vs = new VariableStore();

vs.var('match:example2:input', 'input value');
vs.var('match:example2:field', 'field value');
vs.var('match:example2:fuzziness', 2);
vs.var('match:example2:cutoff_frequency', 0.01);
vs.var('match:example2:analyzer', 'customAnalyzer');

const view = match('example2')(vs);

const actual = JSON.parse(JSON.stringify(view));

const expected = {
match: {
['field value']: {
query: 'input value',
fuzziness: 2,
cutoff_frequency: 0.01,
analyzer: 'customAnalyzer'
}
}
};

t.deepEqual(actual, expected, 'match view rendered with optional parameters');
t.end();
});
};

module.exports.tests.incorrect_usage = function(test, common) {
test('no variables specified', function(t) {
const vs = new VariableStore();

const view = match('broken_example')(vs);

t.equal(view, null, 'should return null');
t.end();
});

test('no input specified', function(t) {
const vs = new VariableStore();
vs.var('match:broken_example:field', 'field value');

const view = match('broken_example')(vs);

t.equal(view, null, 'should return null');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('view/leaf/match ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
35 changes: 35 additions & 0 deletions view/leaf/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const match = require('../../lib/leaf/match');

module.exports = function( prefix ){
return function( vs ){
const input_variable = `match:${prefix}:input`;
const field_variable = `match:${prefix}:field`;

if( !vs.isset(input_variable) ||
!vs.isset(field_variable) ) {
return null;
}

const options = { };

const optional_params = ['boost',
'operator',
'analyzer',
'cutoff_frequency',
'fuzziness',
'max_expansions',
'prefix_length',
'fuzzy_transpositions',
'minimum_should_match',
'zero_terms_query'];

optional_params.forEach(function(param) {
const variable_name = `match:${prefix}:${param}`;
if (vs.isset(variable_name)) {
options[param] = vs.var(variable_name);
}
});

return match(vs.var(field_variable), vs.var(input_variable), options);
};
};

0 comments on commit b650df8

Please sign in to comment.