Work in progress, fixing issue with some helpers not getting translated, now running into rollup issue.

This commit is contained in:
mike12345567 2021-01-29 20:03:09 +00:00
parent 690b63b146
commit bd3bdfa9aa
4 changed files with 36 additions and 31 deletions

View File

@ -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")
}

View File

@ -17,6 +17,7 @@ export default {
],
plugins: [
resolve({
mainFields: ["module", "main"],
preferBuiltins: true,
browser: true,
}),

View File

@ -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"))
}
}

View File

@ -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)
})
})