Merge pull request #15549 from Budibase/BUDI-9011
Budi 9011 - DecodeId helper
This commit is contained in:
commit
7174c868a3
|
@ -36,6 +36,7 @@ export const HelperFunctionNames = {
|
||||||
ALL: "all",
|
ALL: "all",
|
||||||
LITERAL: "literal",
|
LITERAL: "literal",
|
||||||
JS: "js",
|
JS: "js",
|
||||||
|
DECODE_ID: "decodeId",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LITERAL_MARKER = "%LITERAL%"
|
export const LITERAL_MARKER = "%LITERAL%"
|
||||||
|
|
|
@ -25,13 +25,29 @@ function isObject(value: string | any[]) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const HELPERS = [
|
export const HELPERS = [
|
||||||
// external helpers
|
// external helpers
|
||||||
new Helper(HelperFunctionNames.OBJECT, (value: any) => {
|
new Helper(HelperFunctionNames.OBJECT, (value: any) => {
|
||||||
return new Handlebars.SafeString(JSON.stringify(value))
|
return new Handlebars.SafeString(JSON.stringify(value))
|
||||||
}),
|
}),
|
||||||
// javascript helper
|
// javascript helper
|
||||||
new Helper(HelperFunctionNames.JS, processJS, false),
|
new Helper(HelperFunctionNames.JS, processJS, false),
|
||||||
|
new Helper(HelperFunctionNames.DECODE_ID, (_id: string | { _id: string }) => {
|
||||||
|
if (!_id) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
// have to replace on the way back as we swapped out the double quotes
|
||||||
|
// when encoding, but JSON can't handle the single quotes
|
||||||
|
const id = typeof _id === "string" ? _id : _id._id
|
||||||
|
const decoded: string = decodeURIComponent(id).replace(/'/g, '"')
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(decoded)
|
||||||
|
return Array.isArray(parsed) ? parsed : [parsed]
|
||||||
|
} catch (err) {
|
||||||
|
// wasn't json - likely was handlebars for a many to many
|
||||||
|
return [_id]
|
||||||
|
}
|
||||||
|
}),
|
||||||
// this help is applied to all statements
|
// this help is applied to all statements
|
||||||
new Helper(
|
new Helper(
|
||||||
HelperFunctionNames.ALL,
|
HelperFunctionNames.ALL,
|
||||||
|
|
|
@ -517,3 +517,44 @@ describe("helper overlap", () => {
|
||||||
expect(output).toEqual("a")
|
expect(output).toEqual("a")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("Test the decodeId helper", () => {
|
||||||
|
it("should decode a valid encoded ID", async () => {
|
||||||
|
const encodedId = encodeURIComponent("[42]") // "%5B42%5D"
|
||||||
|
const output = await processString("{{ decodeId id }}", { id: encodedId })
|
||||||
|
expect(output).toBe("42")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Should return an unchanged string if the string isn't encoded", async () => {
|
||||||
|
const unencodedId = "forty-two"
|
||||||
|
const output = await processString("{{ decodeId id }}", { id: unencodedId })
|
||||||
|
expect(output).toBe("forty-two")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Should return a string of comma-separated IDs when passed multiple IDs in a URI encoded array", async () => {
|
||||||
|
const encodedIds = encodeURIComponent("[1,2,3]") // "%5B1%2C2%2C3%5D"
|
||||||
|
const output = await processString("{{ decodeId id }}", { id: encodedIds })
|
||||||
|
expect(output).toBe("1,2,3")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Handles empty array gracefully", async () => {
|
||||||
|
const output = await processString("{{ decodeId value }}", {
|
||||||
|
value: [],
|
||||||
|
})
|
||||||
|
expect(output).toBe("[[]]")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Handles undefined gracefully", async () => {
|
||||||
|
const output = await processString("{{ decodeId value }}", {
|
||||||
|
value: undefined,
|
||||||
|
})
|
||||||
|
expect(output).toBe("")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Handles null gracefully", async () => {
|
||||||
|
const output = await processString("{{ decodeId value }}", {
|
||||||
|
value: undefined,
|
||||||
|
})
|
||||||
|
expect(output).toBe("")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue