From bd3bdfa9aac9f17f7a39bcec1fc1b17f0a090de2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 29 Jan 2021 20:03:09 +0000 Subject: [PATCH] Work in progress, fixing issue with some helpers not getting translated, now running into rollup issue. --- .../builder/src/builderStore/dataBinding.js | 55 +++++++++---------- packages/string-templates/rollup.config.js | 1 + packages/string-templates/src/index.js | 4 +- .../string-templates/test/helpers.spec.js | 7 +++ 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/packages/builder/src/builderStore/dataBinding.js b/packages/builder/src/builderStore/dataBinding.js index 76a5717b9d..3e469fbe2e 100644 --- a/packages/builder/src/builderStore/dataBinding.js +++ b/packages/builder/src/builderStore/dataBinding.js @@ -171,51 +171,46 @@ export const getSchemaForDatasource = datasource => { } /** - * Converts a readable data binding into a runtime data binding + * utility function for the readableToRuntimeBinding and runtimeToReadableBinding. */ -export function readableToRuntimeBinding(bindableProperties, textWithBindings) { +function bindingReplacement(bindableProperties, textWithBindings, convertTo) { + const convertFrom = convertTo === "runtimeBinding" ? "readableBinding" : "runtimeBinding" if (typeof textWithBindings !== "string") { return textWithBindings } + const convertFromProps = bindableProperties + .map(el => el[convertFrom]) + .sort((a, b) => { + return b.length - a.length + }) const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_TEMPLATE) || [] let result = textWithBindings for (let boundValue of boundValues) { - const binding = bindableProperties.find(({ readableBinding }) => { - return boundValue.includes(readableBinding) - }) - let newBoundValue = INVALID_BINDING - if (binding) { - newBoundValue = boundValue.replace( - binding.readableBinding, - binding.runtimeBinding - ) + let newBoundValue = boundValue + for (let from of convertFromProps) { + if (newBoundValue.includes(from)) { + const binding = bindableProperties.find(el => el[convertFrom] === from) + newBoundValue = newBoundValue.replace( + from, + binding[convertTo], + ) + } } result = result.replace(boundValue, newBoundValue) } return result } +/** + * Converts a readable data binding into a runtime data binding + */ +export function readableToRuntimeBinding(bindableProperties, textWithBindings) { + return bindingReplacement(bindableProperties, textWithBindings, "runtimeBinding") +} + /** * Converts a runtime data binding into a readable data binding */ export function runtimeToReadableBinding(bindableProperties, textWithBindings) { - if (typeof textWithBindings !== "string") { - return textWithBindings - } - const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_TEMPLATE) || [] - let result = textWithBindings - for (let boundValue of boundValues) { - const binding = bindableProperties.find(({ runtimeBinding }) => { - return boundValue.includes(runtimeBinding) - }) - let newBoundValue = INVALID_BINDING - if (binding) { - newBoundValue = boundValue.replace( - binding.runtimeBinding, - binding.readableBinding - ) - } - result = result.replace(boundValue, newBoundValue) - } - return result + return bindingReplacement(bindableProperties, textWithBindings, "readableBinding") } diff --git a/packages/string-templates/rollup.config.js b/packages/string-templates/rollup.config.js index 5a71316fb5..59d2f6acdc 100644 --- a/packages/string-templates/rollup.config.js +++ b/packages/string-templates/rollup.config.js @@ -17,6 +17,7 @@ export default { ], plugins: [ resolve({ + mainFields: ["module", "main"], preferBuiltins: true, browser: true, }), diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index 8318454b0b..8c99990ad7 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -116,7 +116,9 @@ module.exports.isValid = string => { hbsInstance.compile(processors.preprocess(string, false))(context) return true } catch (err) { - return false + // special case for maths functions - don't have inputs yet + return !!(err && err.message.includes("isNumber")) + } } diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index dcddd8b9e6..7dbb36fa40 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -1,5 +1,6 @@ const { processString, + isValid, } = require("../src/index") describe("test the custom helpers we have applied", () => { @@ -289,4 +290,10 @@ describe("Cover a few complex use cases", () => { }) expect(output).toBe("e") }) + + it("should make sure case is valid", () => { + const validity = isValid("{{ avg [c355ec2b422e54f988ae553c8acd811ea].[a] [c355ec2b422e54f988ae553c8acd811ea].[b] }}") + + expect(validity).toBe(true) + }) }) \ No newline at end of file