diff --git a/packages/string-templates/src/helpers/index.js b/packages/string-templates/src/helpers/index.js index 2d5b910847..76a4c5d2ca 100644 --- a/packages/string-templates/src/helpers/index.js +++ b/packages/string-templates/src/helpers/index.js @@ -13,6 +13,16 @@ const HTML_SWAPS = { ">": ">", } +function isObject(value) { + if (value == null || typeof value !== "object") { + return false + } + return ( + value.toString() === "[object Object]" || + (value.length > 0 && typeof value[0] === "object") + ) +} + const HELPERS = [ // external helpers new Helper(HelperFunctionNames.OBJECT, value => { @@ -22,11 +32,7 @@ const HELPERS = [ new Helper(HelperFunctionNames.JS, processJS, false), // this help is applied to all statements new Helper(HelperFunctionNames.ALL, (value, { __opts }) => { - if ( - value != null && - typeof value === "object" && - (value.toString() === "[object Object]" || Array.isArray(value)) - ) { + if (isObject(value)) { return new SafeString(JSON.stringify(value)) } // null/undefined values produce bad results diff --git a/packages/string-templates/src/processors/preprocessor.js b/packages/string-templates/src/processors/preprocessor.js index 4b296d0fc7..185a3ab38a 100644 --- a/packages/string-templates/src/processors/preprocessor.js +++ b/packages/string-templates/src/processors/preprocessor.js @@ -64,9 +64,10 @@ module.exports.processors = [ return statement } } + const testHelper = possibleHelper.trim().toLowerCase() if ( !noHelpers && - HelperNames().some(option => option.includes(possibleHelper)) + HelperNames().some(option => testHelper === option.toLowerCase()) ) { insideStatement = `(${insideStatement})` } diff --git a/packages/string-templates/test/basic.spec.js b/packages/string-templates/test/basic.spec.js index 6c85aa5fa1..8dd1aeb394 100644 --- a/packages/string-templates/test/basic.spec.js +++ b/packages/string-templates/test/basic.spec.js @@ -106,6 +106,16 @@ describe("Test that the object processing works correctly", () => { }) }) +describe("check returning objects", () => { + it("should handle an array of objects", async () => { + const json = [{a: 1},{a: 2}] + const output = await processString("{{ testing }}", { + testing: json + }) + expect(output).toEqual(JSON.stringify(json)) + }) +}) + describe("check the utility functions", () => { it("should return false for an invalid template string", () => { const valid = isValid("{{ table1.thing prop }}") diff --git a/packages/string-templates/test/escapes.spec.js b/packages/string-templates/test/escapes.spec.js index b845fddec9..a14e78d2f2 100644 --- a/packages/string-templates/test/escapes.spec.js +++ b/packages/string-templates/test/escapes.spec.js @@ -30,6 +30,11 @@ describe("Handling context properties with spaces in their name", () => { }) expect(output).toBe("testcase 1") }) + + it("should allow the use of a", async () => { + const output = await processString("{{ a }}", { a: 1 }) + expect(output).toEqual("1") + }) }) describe("attempt some complex problems", () => {