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 df1248f155
commit 41fbc39618
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") { if (typeof textWithBindings !== "string") {
return textWithBindings 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) || [] const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_TEMPLATE) || []
let result = textWithBindings let result = textWithBindings
for (let boundValue of boundValues) { for (let boundValue of boundValues) {
const binding = bindableProperties.find(({ readableBinding }) => { let newBoundValue = boundValue
return boundValue.includes(readableBinding) for (let from of convertFromProps) {
}) if (newBoundValue.includes(from)) {
let newBoundValue = INVALID_BINDING const binding = bindableProperties.find(el => el[convertFrom] === from)
if (binding) { newBoundValue = newBoundValue.replace(
newBoundValue = boundValue.replace( from,
binding.readableBinding, binding[convertTo],
binding.runtimeBinding
) )
} }
}
result = result.replace(boundValue, newBoundValue) result = result.replace(boundValue, newBoundValue)
} }
return result 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 * Converts a runtime data binding into a readable data binding
*/ */
export function runtimeToReadableBinding(bindableProperties, textWithBindings) { export function runtimeToReadableBinding(bindableProperties, textWithBindings) {
if (typeof textWithBindings !== "string") { return bindingReplacement(bindableProperties, textWithBindings, "readableBinding")
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
} }

View File

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

View File

@ -116,7 +116,9 @@ module.exports.isValid = string => {
hbsInstance.compile(processors.preprocess(string, false))(context) hbsInstance.compile(processors.preprocess(string, false))(context)
return true return true
} catch (err) { } 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 { const {
processString, processString,
isValid,
} = require("../src/index") } = require("../src/index")
describe("test the custom helpers we have applied", () => { describe("test the custom helpers we have applied", () => {
@ -289,4 +290,10 @@ describe("Cover a few complex use cases", () => {
}) })
expect(output).toBe("e") expect(output).toBe("e")
}) })
it("should make sure case is valid", () => {
const validity = isValid("{{ avg [c355ec2b422e54f988ae553c8acd811ea].[a] [c355ec2b422e54f988ae553c8acd811ea].[b] }}")
expect(validity).toBe(true)
})
}) })