Fix for issue discovered by test case.

This commit is contained in:
mike12345567 2022-05-04 17:36:30 +01:00
parent fb1f941c76
commit aac6bc48a7
4 changed files with 28 additions and 6 deletions

View File

@ -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 = [ const HELPERS = [
// external helpers // external helpers
new Helper(HelperFunctionNames.OBJECT, value => { new Helper(HelperFunctionNames.OBJECT, value => {
@ -22,11 +32,7 @@ const HELPERS = [
new Helper(HelperFunctionNames.JS, processJS, false), new Helper(HelperFunctionNames.JS, processJS, false),
// this help is applied to all statements // this help is applied to all statements
new Helper(HelperFunctionNames.ALL, (value, { __opts }) => { new Helper(HelperFunctionNames.ALL, (value, { __opts }) => {
if ( if (isObject(value)) {
value != null &&
typeof value === "object" &&
(value.toString() === "[object Object]" || Array.isArray(value))
) {
return new SafeString(JSON.stringify(value)) return new SafeString(JSON.stringify(value))
} }
// null/undefined values produce bad results // null/undefined values produce bad results

View File

@ -64,9 +64,10 @@ module.exports.processors = [
return statement return statement
} }
} }
const testHelper = possibleHelper.trim().toLowerCase()
if ( if (
!noHelpers && !noHelpers &&
HelperNames().some(option => option.includes(possibleHelper)) HelperNames().some(option => testHelper === option.toLowerCase())
) { ) {
insideStatement = `(${insideStatement})` insideStatement = `(${insideStatement})`
} }

View File

@ -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", () => { describe("check the utility functions", () => {
it("should return false for an invalid template string", () => { it("should return false for an invalid template string", () => {
const valid = isValid("{{ table1.thing prop }}") const valid = isValid("{{ table1.thing prop }}")

View File

@ -30,6 +30,11 @@ describe("Handling context properties with spaces in their name", () => {
}) })
expect(output).toBe("testcase 1") 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", () => { describe("attempt some complex problems", () => {