Some minor fixes for edge cases.

This commit is contained in:
mike12345567 2022-07-29 09:50:53 +01:00
parent 4f2a623eb0
commit 15b275c0f9
3 changed files with 28 additions and 9 deletions

View File

@ -62,7 +62,7 @@
const updateValue = val => { const updateValue = val => {
valid = isValid(readableToRuntimeBinding(bindings, val)) valid = isValid(readableToRuntimeBinding(bindings, val))
console.log(decodeJSBinding(readableToRuntimeBinding(bindings, val))) console.log(readableToRuntimeBinding(bindings, val))
if (valid) { if (valid) {
dispatch("change", val) dispatch("change", val)
} }

View File

@ -32,9 +32,9 @@ function buildList(parts, value) {
.join(", ") .join(", ")
} }
if (!value) { if (!value) {
return parts.length > 1 ? `...[${build()}]` : build() return parts.length > 1 ? `${build()}` : build()
} else { } else {
return parts.length === 0 ? value : `...[${value}, ${build()}]` return parts.length === 0 ? value : `${value}, ${build()}`
} }
} }
@ -50,12 +50,15 @@ function splitBySpace(layer) {
parts.push(layer.substring(started, index + 1).trim()) parts.push(layer.substring(started, index + 1).trim())
started = null started = null
last = index last = index
} else if (started == null && char === " ") { } else if (started == null && char === " " && last !== index - 1) {
parts.push(layer.substring(last, index).trim()) parts.push(layer.substring(last, index).trim())
last = index last = index
} }
} }
if (!layer.startsWith("[")) { if (
(!layer.startsWith("[") || parts.length === 0) &&
last !== layer.length - 1
) {
parts.push(layer.substring(last, layer.length).trim()) parts.push(layer.substring(last, layer.length).trim())
} }
return parts return parts

View File

@ -24,6 +24,14 @@ describe("Test that the string processing works correctly", () => {
]) ])
}) })
it("handle properties", () => {
const response = convertToJS("{{ [query].id }}")
checkLines(response, [
"const var1 = $(\"[query].id\");",
"return `${var1}`;",
])
})
it("should convert some basic HBS strings", () => { it("should convert some basic HBS strings", () => {
const response = convertToJS("Hello {{ name }}, welcome to {{ company }}!") const response = convertToJS("Hello {{ name }}, welcome to {{ company }}!")
checkLines(response, [ checkLines(response, [
@ -33,6 +41,14 @@ describe("Test that the string processing works correctly", () => {
]) ])
}) })
it("Should handle many square brackets in helpers", () => {
const response = convertToJS("Hello {{ avg [user].[_id] [user].[_rev] }}")
checkLines(response, [
"const var1 = helpers.avg($(\"[user].[_id]\"), $(\"[user].[_rev]\"));",
"return `Hello ${var1}`;"
])
})
it("should handle a helper block", () => { it("should handle a helper block", () => {
const response = convertToJS("This is the average: {{ avg array }}") const response = convertToJS("This is the average: {{ avg array }}")
checkLines(response, [ checkLines(response, [
@ -44,7 +60,7 @@ describe("Test that the string processing works correctly", () => {
it("should handle multi-variable helper", () => { it("should handle multi-variable helper", () => {
const response = convertToJS("This is the average: {{ join ( avg val1 val2 val3 ) }}") const response = convertToJS("This is the average: {{ join ( avg val1 val2 val3 ) }}")
checkLines(response, [ checkLines(response, [
"const var1 = helpers.join(helpers.avg(...[$(\"val1\"), $(\"val2\"), $(\"val3\")]));", "const var1 = helpers.join(helpers.avg($(\"val1\"), $(\"val2\"), $(\"val3\")));",
"return `This is the average: ${var1}`;", "return `This is the average: ${var1}`;",
]) ])
}) })
@ -52,7 +68,7 @@ describe("Test that the string processing works correctly", () => {
it("should handle a complex statement", () => { it("should handle a complex statement", () => {
const response = convertToJS("This is the average: {{ join ( avg val1 val2 val3 ) val4 }}") const response = convertToJS("This is the average: {{ join ( avg val1 val2 val3 ) val4 }}")
checkLines(response, [ checkLines(response, [
"const var1 = helpers.join(...[helpers.avg(...[$(\"val1\"), $(\"val2\"), $(\"val3\")]), $(\"val4\")]);", "const var1 = helpers.join(helpers.avg($(\"val1\"), $(\"val2\"), $(\"val3\")), $(\"val4\"));",
"return `This is the average: ${var1}`;", "return `This is the average: ${var1}`;",
]) ])
}) })
@ -76,8 +92,8 @@ describe("Test that the string processing works correctly", () => {
it("should handle multiple complex statements", () => { it("should handle multiple complex statements", () => {
const response = convertToJS("average: {{ avg ( abs val1 ) val2 }} add: {{ add 1 2 }}") const response = convertToJS("average: {{ avg ( abs val1 ) val2 }} add: {{ add 1 2 }}")
checkLines(response, [ checkLines(response, [
"const var1 = helpers.avg(...[helpers.abs($(\"val1\")), $(\"val2\")]);", "const var1 = helpers.avg(helpers.abs($(\"val1\")), $(\"val2\"));",
"const var2 = helpers.add(...[1, 2]);", "const var2 = helpers.add(1, 2);",
"return `average: ${var1} add: ${var2}`;", "return `average: ${var1} add: ${var2}`;",
]) ])
}) })