budibase/packages/string-templates/src/helpers/external.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-03-14 17:41:30 +01:00
import helpers from "@budibase/handlebars-helpers"
2024-02-21 19:40:50 +01:00
import { date, duration } from "./date"
import { HelperFunctionBuiltin } from "./constants"
/**
* full list of supported helpers can be found here:
2021-01-31 12:46:45 +01:00
* https://github.com/Budibase/handlebars-helpers
*/
const EXTERNAL_FUNCTION_COLLECTIONS = [
"math",
"array",
"number",
"url",
"string",
"comparison",
"object",
"regex",
"uuid",
]
const ADDED_HELPERS = {
2021-02-04 11:25:04 +01:00
date: date,
duration: duration,
}
2024-02-21 19:40:50 +01:00
export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
export const addedHelpers = ADDED_HELPERS
2024-02-22 11:52:50 +01:00
export function registerAll(handlebars: typeof Handlebars) {
for (let [name, helper] of Object.entries(ADDED_HELPERS)) {
handlebars.registerHelper(name, helper)
}
let externalNames = []
for (let collection of EXTERNAL_FUNCTION_COLLECTIONS) {
// collect information about helper
let hbsHelperInfo = helpers[collection]()
2021-01-21 19:08:04 +01:00
for (let entry of Object.entries(hbsHelperInfo)) {
const name = entry[0]
// skip built in functions and ones seen already
2021-01-21 18:30:51 +01:00
if (
HelperFunctionBuiltin.indexOf(name) !== -1 ||
externalNames.indexOf(name) !== -1
) {
continue
}
externalNames.push(name)
}
// attach it to our handlebars instance
helpers[collection]({
2021-01-21 18:30:51 +01:00
handlebars,
})
}
// add date external functionality
2024-02-21 19:40:50 +01:00
externalHelperNames = externalNames.concat(Object.keys(ADDED_HELPERS))
}
2024-02-22 11:52:50 +01:00
export function unregisterAll(handlebars: typeof Handlebars) {
for (let name of Object.keys(ADDED_HELPERS)) {
handlebars.unregisterHelper(name)
}
2024-02-21 19:40:50 +01:00
for (let name of externalHelperNames) {
handlebars.unregisterHelper(name)
}
2024-02-21 19:40:50 +01:00
externalHelperNames = []
}
2024-02-22 11:52:50 +01:00
export let externalHelperNames: any[] = []