Skip to content

Commit

Permalink
Simplify templating code, fix array handling (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkr authored Nov 16, 2023
1 parent 47f6f12 commit 3436b0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
42 changes: 19 additions & 23 deletions services/queryTemplateSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ angular.module('o19s.splainer-search')
const isObject = (a) => typeof a === 'object' && a !== null;
const isString = (a) => typeof a === 'string';

function replaceInObjectRecursively(o, parameters) {
// copy the input to make sure we don't modify the original
const obj = { ...o };
for (const key of Object.keys(obj)) {
const value = obj[key];
if (Array.isArray(value)) {{
obj[key] = value.map((entry) => replaceInObjectRecursively(entry, parameters));
}} else if (isObject(value)) {
obj[key] = replaceInObjectRecursively(value, parameters);
} else if (isString(value)) {
obj[key] = replaceInString(value, parameters);
function applyTemplating(o, parameters) {
if (isString(o)) {
return replaceInString(o, parameters);
} else if (Array.isArray(o)) {
return o.map((entry) => applyTemplating(entry, parameters));
} else if (isObject(o)) {
// copy the input to make sure we don't modify the original
const obj = Object.assign({}, o);
for (const key of Object.keys(obj)) {
obj[key] = applyTemplating(obj[key], parameters);
}
return obj;
} else {
return o;
}
}
return obj;
}

function hydrate(template, queryText, config) {
Expand All @@ -117,23 +118,18 @@ angular.module('o19s.splainer-search')
}

const parameters = Object.create(null);
parameters['query'] = encode(queryText, config);
parameters.query = encode(queryText, config);

const keywords = queryText.split(/[ ,]+/).map((term) => encode(term.trim(), config));
parameters['keyword'] = keywords;
parameters.keyword = keywords;
// legacy: also support #$keyword1## syntax
keywords.forEach((keyword, idx) => parameters[`keyword${idx + 1}`] = keyword);

if (config.qOption) {
parameters['qOption'] = config.qOption;
parameters.qOption = config.qOption;
}

if (isObject(template)) {
return replaceInObjectRecursively(template, parameters);
} else if (isString(template)) {
return replaceInString(template, parameters);
}

return template;
return applyTemplating(template, parameters);
}

});
24 changes: 24 additions & 0 deletions test/spec/queryTemplateSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ describe('Service: queryTemplateSvc', function () {
expect(replaced).toEqual(expectedReplaced);
});

it('passes through arrays', function() {
const queryText = "test query"
const qOption = {
"complexDoc": {
"doc_id": 1,
"prefix": "doc"
}
}
const template = {
"query": {
"query": "#$query##",
"docs": ["#$qOption.complexDoc##", "doc2", "doc3"]
}
}
const replaced = queryTemplateSvc.hydrate(template, queryText, { qOption: qOption, encodeURI: false, defaultKw: '\\"\\"'});
var expectedReplaced = {
query: {
query: "test query",
docs: [ { "doc_id": 1, "prefix": "doc" }, "doc2", "doc3"]
}
}
expect(replaced).toEqual(expectedReplaced);
});

it('leaves unresolved parameters untouched', function() {

var queryText = 'rambo movie';
Expand Down

0 comments on commit 3436b0d

Please sign in to comment.