1
0
Fork 0
mirror of synced 2024-06-14 08:24:48 +12:00

Fix for issue discovered by test case.

This commit is contained in:
mike12345567 2022-05-04 17:36:30 +01:00
parent 4982cad56d
commit 948b7e6d00
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 = [
// 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

View file

@ -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})`
}

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", () => {
it("should return false for an invalid template string", () => {
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")
})
it("should allow the use of a", async () => {
const output = await processString("{{ a }}", { a: 1 })
expect(output).toEqual("1")
})
})
describe("attempt some complex problems", () => {