Adding a UUID helper to simplify things, question comes up fairly regularly.

This commit is contained in:
mike12345567 2023-12-13 17:56:16 +00:00
parent 27b8cc7476
commit 6173497afa
9 changed files with 72 additions and 745 deletions

View File

@ -1180,6 +1180,14 @@
"description": "<p>Stringify an object using <code>JSON.stringify</code>.</p>\n" "description": "<p>Stringify an object using <code>JSON.stringify</code>.</p>\n"
} }
}, },
"uuid": {
"uuid": {
"args": [],
"numArgs": 0,
"example": "{{ uuid }} -> f34ebc66-93bd-4f7c-b79b-92b5569138bc",
"description": "<p>Generates a UUID, using the V4 method (identical to the browser crypto.randomUUID function).</p>\n"
}
},
"date": { "date": {
"date": { "date": {
"args": [ "args": [

View File

@ -25,7 +25,7 @@
"manifest": "node ./scripts/gen-collection-info.js" "manifest": "node ./scripts/gen-collection-info.js"
}, },
"dependencies": { "dependencies": {
"@budibase/handlebars-helpers": "^0.11.9", "@budibase/handlebars-helpers": "^0.11.11",
"dayjs": "^1.10.8", "dayjs": "^1.10.8",
"handlebars": "^4.7.6", "handlebars": "^4.7.6",
"lodash": "4.17.21", "lodash": "4.17.21",

View File

@ -20,6 +20,7 @@ const COLLECTIONS = [
"string", "string",
"comparison", "comparison",
"object", "object",
"uuid",
] ]
const FILENAME = join(__dirname, "..", "manifest.json") const FILENAME = join(__dirname, "..", "manifest.json")
const outputJSON = {} const outputJSON = {}

View File

@ -16,6 +16,7 @@ const EXTERNAL_FUNCTION_COLLECTIONS = [
"comparison", "comparison",
"object", "object",
"regex", "regex",
"uuid",
] ]
const ADDED_HELPERS = { const ADDED_HELPERS = {

View File

@ -71,6 +71,9 @@ module.exports.processors = [
) { ) {
insideStatement = `(${insideStatement})` insideStatement = `(${insideStatement})`
} }
if (statement === "{{ uuid }}") {
return statement
}
return `{{ all ${insideStatement} }}` return `{{ all ${insideStatement} }}`
}), }),
] ]

View File

@ -0,0 +1,2 @@
module.exports.UUID_REGEX =
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i

View File

@ -1,6 +1,7 @@
const { processString, processObject, isValid } = require("../src/index.cjs") const { processString, processObject, isValid } = require("../src/index.cjs")
const tableJson = require("./examples/table.json") const tableJson = require("./examples/table.json")
const dayjs = require("dayjs") const dayjs = require("dayjs")
const { UUID_REGEX } = require("./constants")
describe("test the custom helpers we have applied", () => { describe("test the custom helpers we have applied", () => {
it("should be able to use the object helper", async () => { it("should be able to use the object helper", async () => {
@ -477,3 +478,10 @@ describe("Cover a few complex use cases", () => {
expect(output.dataProvider).toBe("%5B%221%22%2C%221%22%5D") expect(output.dataProvider).toBe("%5B%221%22%2C%221%22%5D")
}) })
}) })
describe("uuid", () => {
it("should be able to generate a UUID", async () => {
const output = await processString("{{ uuid }}", {})
expect(output).toMatch(UUID_REGEX)
})
})

View File

@ -1,4 +1,5 @@
const { processStringSync, encodeJSBinding } = require("../src/index.cjs") const { processStringSync, encodeJSBinding } = require("../src/index.cjs")
const { UUID_REGEX } = require("./constants")
const processJS = (js, context) => { const processJS = (js, context) => {
return processStringSync(encodeJSBinding(js), context) return processStringSync(encodeJSBinding(js), context)
@ -140,4 +141,9 @@ describe("check JS helpers", () => {
const output = processJS(`return helpers.toInt(4.3)`) const output = processJS(`return helpers.toInt(4.3)`)
expect(output).toBe(4) expect(output).toBe(4)
}) })
it("should be able to use uuid", () => {
const output = processJS(`return helpers.uuid()`)
expect(output).toMatch(UUID_REGEX)
})
}) })

786
yarn.lock

File diff suppressed because it is too large Load Diff