Fix for issue discovered by test case.
This commit is contained in:
parent
4982cad56d
commit
948b7e6d00
|
@ -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
|
||||||
|
|
|
@ -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})`
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 }}")
|
||||||
|
|
|
@ -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", () => {
|
||||||
|
|
Loading…
Reference in New Issue