Merge pull request #14214 from Budibase/fix/14201

Add all HBS helpers to manifest
This commit is contained in:
Michael Drury 2024-07-23 17:37:12 +01:00 committed by GitHub
commit 0fb9c78551
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 51 additions and 41 deletions

View File

@ -1,4 +1,7 @@
import { HelperFunctionBuiltin } from "../src/helpers/constants" import {
HelperFunctionBuiltin,
EXTERNAL_FUNCTION_COLLECTIONS,
} from "../src/helpers/constants"
import { readFileSync, writeFileSync } from "fs" import { readFileSync, writeFileSync } from "fs"
import { marked } from "marked" import { marked } from "marked"
import { join, dirname } from "path" import { join, dirname } from "path"
@ -14,21 +17,6 @@ type HelperInfo = {
tags?: any[] tags?: any[]
} }
/**
* full list of supported helpers can be found here:
* https://github.com/budibase/handlebars-helpers
*/
const COLLECTIONS = [
"math",
"array",
"number",
"url",
"string",
"comparison",
"object",
"uuid",
]
const FILENAME = join(__dirname, "..", "src", "manifest.json") const FILENAME = join(__dirname, "..", "src", "manifest.json")
const outputJSON: any = {} const outputJSON: any = {}
const ADDED_HELPERS = { const ADDED_HELPERS = {
@ -140,7 +128,7 @@ const excludeFunctions: Record<string, string[]> = { string: ["raw"] }
*/ */
function run() { function run() {
const foundNames: string[] = [] const foundNames: string[] = []
for (let collection of COLLECTIONS) { for (let collection of EXTERNAL_FUNCTION_COLLECTIONS) {
const collectionFile = readFileSync( const collectionFile = readFileSync(
`${dirname( `${dirname(
require.resolve("@budibase/handlebars-helpers") require.resolve("@budibase/handlebars-helpers")

View File

@ -58,7 +58,7 @@ function buildList(parts: string[], value: any) {
if (!value) { if (!value) {
return parts.length > 1 ? `${build()}` : build() return parts.length > 1 ? `${build()}` : build()
} else { } else {
return parts.length === 0 ? value : `${value}, ${build()}` return parts.length === 0 ? value : `${build()}, ${value}`
} }
} }

View File

@ -15,6 +15,22 @@ export const HelperFunctionBuiltin = [
"with", "with",
] ]
/**
* full list of supported helpers can be found here:
* https://github.com/Budibase/handlebars-helpers
*/
export const EXTERNAL_FUNCTION_COLLECTIONS = [
"math",
"array",
"number",
"url",
"string",
"comparison",
"object",
"regex",
"uuid",
]
export const HelperFunctionNames = { export const HelperFunctionNames = {
OBJECT: "object", OBJECT: "object",
ALL: "all", ALL: "all",

View File

@ -2,26 +2,12 @@
import helpers from "@budibase/handlebars-helpers" import helpers from "@budibase/handlebars-helpers"
import { date, duration } from "./date" import { date, duration } from "./date"
import { HelperFunctionBuiltin } from "./constants" import {
HelperFunctionBuiltin,
EXTERNAL_FUNCTION_COLLECTIONS,
} from "./constants"
import Handlebars from "handlebars" import Handlebars from "handlebars"
/**
* full list of supported helpers can be found here:
* https://github.com/Budibase/handlebars-helpers
*/
const EXTERNAL_FUNCTION_COLLECTIONS = [
"math",
"array",
"number",
"url",
"string",
"comparison",
"object",
"regex",
"uuid",
]
const ADDED_HELPERS = { const ADDED_HELPERS = {
date: date, date: date,
duration: duration, duration: duration,
@ -40,7 +26,7 @@ export function registerAll(handlebars: typeof Handlebars) {
let hbsHelperInfo = helpers[collection]() let hbsHelperInfo = helpers[collection]()
for (let entry of Object.entries(hbsHelperInfo)) { for (let entry of Object.entries(hbsHelperInfo)) {
const name = entry[0] const name = entry[0]
// skip built in functions and ones seen already // skip built-in functions and ones seen already
if ( if (
HelperFunctionBuiltin.indexOf(name) !== -1 || HelperFunctionBuiltin.indexOf(name) !== -1 ||
externalNames.indexOf(name) !== -1 externalNames.indexOf(name) !== -1

View File

@ -1312,6 +1312,26 @@
"requiresBlock": false "requiresBlock": false
} }
}, },
"regex": {
"toRegex": {
"args": [
"str"
],
"numArgs": 1,
"example": "{{toRegex 'foo'}} -> /foo/",
"description": "<p>Convert the given string to a regular expression.</p>\n",
"requiresBlock": false
},
"test": {
"args": [
"str"
],
"numArgs": 1,
"example": "{{test 'foobar' (toRegex 'foo')}} -> true",
"description": "<p>Returns true if the given <code>str</code> matches the given regex. A regex can be passed on the context, or using the <a href=\"#toregex\">toRegex</a> helper as a subexpression.</p>\n",
"requiresBlock": false
}
},
"uuid": { "uuid": {
"uuid": { "uuid": {
"args": [], "args": [],

View File

@ -93,10 +93,10 @@ describe("Test that the string processing works correctly", () => {
it("should handle a complex statement", () => { it("should handle a complex statement", () => {
const response = convertToJS( const response = convertToJS(
"This is the average: {{ join ( avg val1 val2 val3 ) val4 }}" "This is the average: {{ join val1 ( avg val2 val3 val4 ) }}"
) )
checkLines(response, [ checkLines(response, [
'const var1 = helpers.join(helpers.avg($("val1"), $("val2"), $("val3")), $("val4"));', 'const var1 = helpers.join($("val1"), helpers.avg($("val2"), $("val3"), $("val4")));',
"return `This is the average: ${var1}`;", "return `This is the average: ${var1}`;",
]) ])
}) })
@ -119,10 +119,10 @@ describe("Test that the string processing works correctly", () => {
it("should handle multiple complex statements", () => { it("should handle multiple complex statements", () => {
const response = convertToJS( const response = convertToJS(
"average: {{ avg ( abs val1 ) val2 }} add: {{ add 1 2 }}" "average: {{ avg val1 ( abs val2 ) }} add: {{ add 1 2 }}"
) )
checkLines(response, [ checkLines(response, [
'const var1 = helpers.avg(helpers.abs($("val1")), $("val2"));', 'const var1 = helpers.avg($("val1"), helpers.abs($("val2")));',
"const var2 = helpers.add(1, 2);", "const var2 = helpers.add(1, 2);",
"return `average: ${var1} add: ${var2}`;", "return `average: ${var1} add: ${var2}`;",
]) ])