diff --git a/services/queryTemplateSvc.js b/services/queryTemplateSvc.js index e4594f9..26a8ac0 100644 --- a/services/queryTemplateSvc.js +++ b/services/queryTemplateSvc.js @@ -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) { @@ -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); } + }); diff --git a/test/spec/queryTemplateSvc.js b/test/spec/queryTemplateSvc.js index 51a1694..d621caf 100644 --- a/test/spec/queryTemplateSvc.js +++ b/test/spec/queryTemplateSvc.js @@ -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';