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

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-02-21 19:40:50 +01:00
import { date, duration } from "./date"
2024-03-21 17:09:20 +01:00
/* eslint-disable local-rules/no-budibase-imports */
2024-03-21 16:10:27 +01:00
// @ts-expect-error
import math from "@budibase/handlebars-helpers/lib/math"
// @ts-expect-error
import array from "@budibase/handlebars-helpers/lib/array"
// @ts-expect-error
import number from "@budibase/handlebars-helpers/lib/number"
// @ts-expect-error
import url from "@budibase/handlebars-helpers/lib/url"
// @ts-expect-error
import string from "@budibase/handlebars-helpers/lib/string"
// @ts-expect-error
import comparison from "@budibase/handlebars-helpers/lib/comparison"
// @ts-expect-error
import object from "@budibase/handlebars-helpers/lib/object"
// @ts-expect-error
import regex from "@budibase/handlebars-helpers/lib/regex"
// @ts-expect-error
import uuid from "@budibase/handlebars-helpers/lib/uuid"
2024-03-21 17:09:20 +01:00
/* eslint-enable local-rules/no-budibase-imports */
2024-03-21 11:28:06 +01:00
// https://github.com/evanw/esbuild/issues/56
2024-03-21 11:28:06 +01:00
const externalCollections = {
math,
array,
number,
url,
string,
comparison,
object,
regex,
uuid,
}
2024-02-21 19:40:50 +01:00
export const helpersToRemoveForJs = ["sortBy"]
2024-01-30 16:52:25 +01:00
const addedHelpers = {
date: date,
duration: duration,
}
2024-03-15 10:18:47 +01:00
let helpers: Record<string, any>
2024-02-21 19:40:50 +01:00
export function getJsHelperList() {
if (helpers) {
return helpers
}
helpers = {}
2024-03-21 11:28:06 +01:00
for (let collection of Object.values(externalCollections)) {
2024-03-21 16:10:27 +01:00
for (let [key, func] of Object.entries<any>(collection)) {
// Handlebars injects the hbs options to the helpers by default. We are adding an empty {} as a last parameter to simulate it
2024-03-15 09:35:38 +01:00
helpers[key] = (...props: any) => func(...props, {})
}
}
2024-03-15 09:35:38 +01:00
helpers = {
...helpers,
addedHelpers,
}
2024-01-25 17:24:47 +01:00
2024-01-31 10:34:49 +01:00
for (const toRemove of helpersToRemoveForJs) {
2024-01-30 16:52:25 +01:00
delete helpers[toRemove]
}
Object.freeze(helpers)
return helpers
}