From 5f40ea49d8127b2ebe569c9247ed6cae818a4d06 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 2 Feb 2021 14:22:10 +0000 Subject: [PATCH] Adding object and regex helpers as well as a complex test case for forIn. --- packages/string-templates/README.md | 20 +++++++++++-------- .../string-templates/src/helpers/external.js | 2 ++ .../string-templates/test/helpers.spec.js | 7 ++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/string-templates/README.md b/packages/string-templates/README.md index d9433b387b..b2a1ce476e 100644 --- a/packages/string-templates/README.md +++ b/packages/string-templates/README.md @@ -2,26 +2,30 @@ This package provides a common system for string templating across the Budibase Builder, client and server. The templating is provided through the use of [Handlebars](https://handlebarsjs.com/) an extension of Mustache which is capable of carrying out logic. We have also extended the base Handlebars functionality through the use -of a set of helpers provided through the [handlebars-helpers](https://github.com/helpers/handlebars-helpers) package. +of a set of helpers provided through the [handlebars-helpers](https://github.com/budibase/handlebars-helpers) package. We have not implemented all the helpers provided by the helpers package as some of them provide functionality we felt would not be beneficial. The following collections of helpers have been implemented: -1. [Math](https://github.com/helpers/handlebars-helpers/tree/master#math) - a set of useful helpers for +1. [Math](https://github.com/budibase/handlebars-helpers/tree/master#math) - a set of useful helpers for carrying out logic pertaining to numbers e.g. `avg`, `add`, `abs` and so on. -2. [Array](https://github.com/helpers/handlebars-helpers/tree/master#array) - some very specific helpers +2. [Array](https://github.com/budibase/handlebars-helpers/tree/master#array) - some very specific helpers for use with arrays, useful for example in automations. Helpers like `first`, `last`, `after` and `join` can be useful for getting particular portions of arrays or turning them into strings. -3. [Number](https://github.com/helpers/handlebars-helpers/tree/master#number) - unlike the math helpers these +3. [Number](https://github.com/budibase/handlebars-helpers/tree/master#number) - unlike the math helpers these are useful for converting numbers into useful formats for display, e.g. `bytes`, `addCommas` and `toPrecision`. -4. [URL](https://github.com/helpers/handlebars-helpers/tree/master#url) - very specific helpers for dealing with URLs, +4. [URL](https://github.com/budibase/handlebars-helpers/tree/master#url) - very specific helpers for dealing with URLs, such as `encodeURI`, `escape`, `stripQueryString` and `stripProtocol`. These are primarily useful for building up particular URLs to hit as say part of an automation. -5. [String](https://github.com/helpers/handlebars-helpers/tree/master#string) - these helpers are useful for building +5. [String](https://github.com/budibase/handlebars-helpers/tree/master#string) - these helpers are useful for building strings and preparing them for display, e.g. `append`, `camelcase`, `capitalize` and `ellipsis`. -6. [Comparison](https://github.com/helpers/handlebars-helpers/tree/master#comparison) - these helpers are mainly for +6. [Comparison](https://github.com/budibase/handlebars-helpers/tree/master#comparison) - these helpers are mainly for building strings when particular conditions are met, for example `and`, `or`, `gt`, `lt`, `not` and so on. This is a very extensive set of helpers but is mostly as would be expected from a set of logical operators. -7. [Date](https://github.com/helpers/helper-date) - last but certainly not least is a moment based date helper, which can +7. [Object](https://github.com/budibase/handlebars-helpers/tree/master#object) - useful operator for parsing objects, as well +as converting them to JSON strings. +8. [Regex](https://github.com/budibase/handlebars-helpers/tree/master#regex) - allows performing regex tests on strings that +can be used in conditional statements. +9. [Date](https://github.com/helpers/helper-date) - last but certainly not least is a moment based date helper, which can format ISO/timestamp based dates into something human-readable. An example of this would be `{{date dateProperty "DD-MM-YYYY"}}`. ## Date formatting diff --git a/packages/string-templates/src/helpers/external.js b/packages/string-templates/src/helpers/external.js index 6cdf976be5..3256134371 100644 --- a/packages/string-templates/src/helpers/external.js +++ b/packages/string-templates/src/helpers/external.js @@ -14,6 +14,8 @@ const EXTERNAL_FUNCTION_COLLECTIONS = [ "url", "string", "comparison", + "object", + "regex", ] const DATE_NAME = "date" diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index 6eadaabb05..a602b286e4 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -291,6 +291,12 @@ describe("Cover a few complex use cases", () => { expect(output).toBe("e") }) + it("should allow a complex forIn case", async () => { + const input = `{{#forIn (JSONparse '{"a":1, "b":2, "c":3}' )}}number: {{.}}\n{{/forIn}}` + const output = await processString(input, {}) + expect(output).toBe("number: 1\nnumber: 2\nnumber: 3\n") + }) + it("should make sure case is valid", () => { const validity = isValid("{{ avg [c355ec2b422e54f988ae553c8acd811ea].[a] [c355ec2b422e54f988ae553c8acd811ea].[b] }}") expect(validity).toBe(true) @@ -299,6 +305,5 @@ describe("Cover a few complex use cases", () => { it("should be able to solve an example from docs", async () => { const output = await processString(`{{first ( split "a-b-c" "-") 2}}`, {}) expect(output).toBe(`a,b`) - }) }) \ No newline at end of file