handles encoded array of 1 or many IDs

This commit is contained in:
mikesealey 2025-02-13 14:31:39 +00:00
parent a434973e6a
commit 25489c25d4
1 changed files with 15 additions and 2 deletions

View File

@ -32,8 +32,21 @@ const HELPERS = [
}), }),
// javascript helper // javascript helper
new Helper(HelperFunctionNames.JS, processJS, false), new Helper(HelperFunctionNames.JS, processJS, false),
new Helper(HelperFunctionNames.DECODE_ID, value => { new Helper(HelperFunctionNames.DECODE_ID, (_id: string | { _id: string }) => {
return value 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(