2021-04-07 11:56:06 +02:00
|
|
|
const { processString, processObject, isValid } = require("../src/index.cjs")
|
2021-05-27 12:45:17 +02:00
|
|
|
const tableJson = require("./examples/table.json")
|
2021-01-19 19:44:29 +01:00
|
|
|
|
|
|
|
describe("test the custom helpers we have applied", () => {
|
2021-01-20 14:32:15 +01:00
|
|
|
it("should be able to use the object helper", async () => {
|
|
|
|
const output = await processString("object is {{ object obj }}", {
|
2021-01-19 19:44:29 +01:00
|
|
|
obj: { a: 1 },
|
|
|
|
})
|
2021-03-16 19:16:56 +01:00
|
|
|
expect(output).toBe('object is {"a":1}')
|
2021-01-19 19:44:29 +01:00
|
|
|
})
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the math helpers", () => {
|
|
|
|
it("should be able to produce an absolute", async () => {
|
|
|
|
const output = await processString("{{abs a}}", {
|
|
|
|
a: -10,
|
|
|
|
})
|
|
|
|
expect(parseInt(output)).toBe(10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to add", async () => {
|
|
|
|
const output = await processString("{{add a b}}", {
|
|
|
|
a: 10,
|
|
|
|
b: 10,
|
|
|
|
})
|
|
|
|
expect(parseInt(output)).toBe(20)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to average", async () => {
|
|
|
|
const output = await processString("{{avg a b c}}", {
|
|
|
|
a: 1,
|
|
|
|
b: 2,
|
|
|
|
c: 3,
|
|
|
|
})
|
|
|
|
expect(parseInt(output)).toBe(2)
|
|
|
|
})
|
2021-01-27 19:09:04 +01:00
|
|
|
|
|
|
|
it("should be able to times", async () => {
|
|
|
|
const output = await processString("{{times a b}}", {
|
|
|
|
a: 5,
|
|
|
|
b: 5,
|
|
|
|
})
|
|
|
|
expect(parseInt(output)).toBe(25)
|
|
|
|
})
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the array helpers", () => {
|
|
|
|
const array = ["hi", "person", "how", "are", "you"]
|
|
|
|
it("should allow use of the after helper", async () => {
|
|
|
|
const output = await processString("{{after array 1}}", {
|
|
|
|
array,
|
|
|
|
})
|
|
|
|
expect(output).toBe("person,how,are,you")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the before helper", async () => {
|
|
|
|
const output = await processString("{{before array 2}}", {
|
|
|
|
array,
|
|
|
|
})
|
|
|
|
expect(output).toBe("hi,person,how")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the filter helper", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString(
|
|
|
|
'{{#filter array "person"}}THING{{else}}OTHER{{/filter}}',
|
|
|
|
{
|
|
|
|
array,
|
|
|
|
}
|
|
|
|
)
|
2021-01-25 18:08:21 +01:00
|
|
|
expect(output).toBe("THING")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the itemAt helper", async () => {
|
|
|
|
const output = await processString("{{itemAt array 1}}", {
|
|
|
|
array,
|
|
|
|
})
|
|
|
|
expect(output).toBe("person")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the join helper", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString('{{join array "-"}}', {
|
2021-01-25 18:08:21 +01:00
|
|
|
array,
|
|
|
|
})
|
|
|
|
expect(output).toBe("hi-person-how-are-you")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the sort helper", async () => {
|
|
|
|
const output = await processString("{{sort array}}", {
|
2021-03-16 19:16:56 +01:00
|
|
|
array: ["d", "a", "c", "e"],
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("a,c,d,e")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the unique helper", async () => {
|
|
|
|
const output = await processString("{{unique array}}", {
|
2021-03-16 19:16:56 +01:00
|
|
|
array: ["a", "a", "b"],
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("a,b")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the number helpers", () => {
|
|
|
|
it("should allow use of the addCommas helper", async () => {
|
|
|
|
const output = await processString("{{ addCommas number }}", {
|
2021-03-16 19:16:56 +01:00
|
|
|
number: 10000000,
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("10,000,000")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the phoneNumber helper", async () => {
|
|
|
|
const output = await processString("{{ phoneNumber number }}", {
|
|
|
|
number: 4490102030,
|
|
|
|
})
|
|
|
|
expect(output).toBe("(449) 010-2030")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the toPrecision helper", async () => {
|
|
|
|
const output = await processString("{{ toPrecision number 2 }}", {
|
|
|
|
number: 1.222222222,
|
|
|
|
})
|
|
|
|
expect(output).toBe("1.2")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the bytes helper", async () => {
|
|
|
|
const output = await processString("{{ bytes number }}", {
|
|
|
|
number: 1000000,
|
|
|
|
})
|
|
|
|
expect(output).toBe("1 MB")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the url helpers", () => {
|
|
|
|
const url = "http://example.com?query=1"
|
|
|
|
it("should allow use of the stripQueryString helper", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString("{{stripQuerystring url }}", {
|
2021-01-25 18:08:21 +01:00
|
|
|
url,
|
|
|
|
})
|
|
|
|
expect(output).toBe("http://example.com")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the stripProtocol helper", async () => {
|
|
|
|
const output = await processString("{{ stripProtocol url }}", {
|
|
|
|
url,
|
|
|
|
})
|
|
|
|
expect(output).toBe("//example.com/?query=1")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the urlParse helper", async () => {
|
|
|
|
const output = await processString("{{ object ( urlParse url ) }}", {
|
|
|
|
url,
|
|
|
|
})
|
2021-03-16 19:16:56 +01:00
|
|
|
expect(output).toBe(
|
|
|
|
'{"protocol":"http:","slashes":true,"auth":null,"host":"example.com",' +
|
|
|
|
'"port":null,"hostname":"example.com","hash":null,"search":"?query=1",' +
|
|
|
|
'"query":"query=1","pathname":"/","path":"/?query=1",' +
|
|
|
|
'"href":"http://example.com/?query=1"}'
|
|
|
|
)
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the date helpers", () => {
|
|
|
|
it("should allow use of the date helper", async () => {
|
|
|
|
const date = new Date(1611577535000)
|
|
|
|
const output = await processString("{{ date time 'YYYY-MM-DD' }}", {
|
|
|
|
time: date.toISOString(),
|
|
|
|
})
|
|
|
|
expect(output).toBe("2021-01-25")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the date helper with now time", async () => {
|
|
|
|
const date = new Date()
|
|
|
|
const output = await processString("{{ date now 'DD' }}", {})
|
2021-02-01 14:17:42 +01:00
|
|
|
expect(parseInt(output)).toBe(date.getDate())
|
2021-01-25 18:08:21 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("test the string helpers", () => {
|
|
|
|
it("should allow use of the append helper", async () => {
|
|
|
|
const output = await processString("{{ append filename '.txt' }}", {
|
|
|
|
filename: "yummy",
|
|
|
|
})
|
|
|
|
expect(output).toBe("yummy.txt")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the camelcase helper", async () => {
|
|
|
|
const output = await processString("{{ camelcase camel }}", {
|
|
|
|
camel: "testing this thing",
|
|
|
|
})
|
|
|
|
expect(output).toBe("testingThisThing")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the capitalize helper", async () => {
|
|
|
|
const output = await processString("{{ capitalize string }}", {
|
|
|
|
string: "this is a string",
|
|
|
|
})
|
|
|
|
expect(output).toBe("This is a string")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the capitalizeAll helper", async () => {
|
|
|
|
const output = await processString("{{ capitalizeAll string }}", {
|
|
|
|
string: "this is a string",
|
|
|
|
})
|
|
|
|
expect(output).toBe("This Is A String")
|
|
|
|
})
|
2021-01-21 12:30:53 +01:00
|
|
|
|
2021-01-25 18:08:21 +01:00
|
|
|
it("should allow use of the replace helper", async () => {
|
|
|
|
const output = await processString("{{ replace string 'Mike' name }}", {
|
|
|
|
string: "Hello my name is Mike",
|
|
|
|
name: "David",
|
|
|
|
})
|
|
|
|
expect(output).toBe("Hello my name is David")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the split helper", async () => {
|
|
|
|
const output = await processString("{{ first ( split string ' ' ) }}", {
|
|
|
|
string: "this is a string",
|
|
|
|
})
|
|
|
|
expect(output).toBe("this")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the remove helper", async () => {
|
|
|
|
const output = await processString("{{ remove string 'string' }}", {
|
|
|
|
string: "this is a string",
|
|
|
|
})
|
|
|
|
expect(output).toBe("this is a ")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the startsWith helper", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString(
|
|
|
|
"{{ #startsWith 'Hello' string }}Hi!{{ else }}Goodbye!{{ /startsWith }}",
|
|
|
|
{
|
|
|
|
string: "Hello my name is Mike",
|
|
|
|
}
|
|
|
|
)
|
2021-01-25 18:08:21 +01:00
|
|
|
expect(output).toBe("Hi!")
|
|
|
|
})
|
2021-01-21 16:50:46 +01:00
|
|
|
})
|
2021-01-25 18:08:21 +01:00
|
|
|
|
|
|
|
describe("test the comparison helpers", () => {
|
|
|
|
async function compare(func, a, b) {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString(
|
|
|
|
`{{ #${func} a b }}Success{{ else }}Fail{{ /${func} }}`,
|
|
|
|
{
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
}
|
|
|
|
)
|
2021-01-25 18:08:21 +01:00
|
|
|
expect(output).toBe("Success")
|
|
|
|
}
|
|
|
|
it("should allow use of the lt helper", async () => {
|
|
|
|
await compare("lt", 10, 15)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the gt helper", async () => {
|
|
|
|
await compare("gt", 15, 10)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the and helper", async () => {
|
|
|
|
await compare("and", true, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the or helper", async () => {
|
|
|
|
await compare("or", false, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of gte with a literal value", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString(
|
|
|
|
`{{ #gte a "50" }}s{{ else }}f{{ /gte }}`,
|
|
|
|
{
|
|
|
|
a: 51,
|
|
|
|
}
|
|
|
|
)
|
2021-01-25 18:08:21 +01:00
|
|
|
expect(output).toBe("s")
|
|
|
|
})
|
2021-01-25 19:14:45 +01:00
|
|
|
})
|
|
|
|
|
2021-04-30 17:17:57 +02:00
|
|
|
describe("Test the object/array helper", () => {
|
|
|
|
it("should allow plucking from an array of objects", async () => {
|
|
|
|
const context = {
|
|
|
|
items: [
|
|
|
|
{ price: 20 },
|
|
|
|
{ price: 30 },
|
|
|
|
]
|
|
|
|
}
|
2021-04-30 17:21:59 +02:00
|
|
|
const output = await processString("{{ literal ( sum ( pluck items 'price' ) ) }}", context)
|
2021-04-30 17:17:57 +02:00
|
|
|
expect(output).toBe(50)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-25 19:14:45 +01:00
|
|
|
describe("Test the literal helper", () => {
|
|
|
|
it("should allow use of the literal specifier for a number", async () => {
|
|
|
|
const output = await processString(`{{literal a}}`, {
|
|
|
|
a: 51,
|
|
|
|
})
|
|
|
|
expect(output).toBe(51)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow use of the literal specifier for an object", async () => {
|
|
|
|
const output = await processString(`{{literal a}}`, {
|
2021-03-16 19:16:56 +01:00
|
|
|
a: { b: 1 },
|
2021-01-25 19:14:45 +01:00
|
|
|
})
|
|
|
|
expect(output.b).toBe(1)
|
|
|
|
})
|
2021-03-16 19:16:56 +01:00
|
|
|
|
|
|
|
it("should allow use of the literal specifier for an object with dashes", async () => {
|
|
|
|
const output = await processString(`{{literal a}}`, {
|
|
|
|
a: { b: "i-have-dashes" },
|
|
|
|
})
|
|
|
|
expect(output.b).toBe("i-have-dashes")
|
|
|
|
})
|
2021-01-26 15:09:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("Cover a few complex use cases", () => {
|
|
|
|
it("should allow use of three different collection helpers", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const output = await processString(
|
|
|
|
`{{ join ( after ( split "My name is: Joe Smith" " " ) 3 ) " " }}`,
|
|
|
|
{}
|
|
|
|
)
|
2021-01-26 15:09:31 +01:00
|
|
|
expect(output).toBe("Joe Smith")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow a complex array case", async () => {
|
|
|
|
const output = await processString("{{ last ( sort ( unique array ) ) }}", {
|
2021-03-16 19:16:56 +01:00
|
|
|
array: ["a", "a", "d", "c", "e"],
|
2021-01-26 15:09:31 +01:00
|
|
|
})
|
|
|
|
expect(output).toBe("e")
|
|
|
|
})
|
2021-01-29 21:03:09 +01:00
|
|
|
|
2021-02-02 15:22:10 +01:00
|
|
|
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")
|
|
|
|
})
|
|
|
|
|
2021-01-29 21:03:09 +01:00
|
|
|
it("should make sure case is valid", () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const validity = isValid(
|
|
|
|
"{{ avg [c355ec2b422e54f988ae553c8acd811ea].[a] [c355ec2b422e54f988ae553c8acd811ea].[b] }}"
|
|
|
|
)
|
2021-01-29 21:03:09 +01:00
|
|
|
expect(validity).toBe(true)
|
|
|
|
})
|
2021-01-30 03:54:52 +01:00
|
|
|
|
2021-02-02 18:28:11 +01:00
|
|
|
it("should make sure object functions check out valid", () => {
|
|
|
|
const validity = isValid("{{ JSONstringify obj }}")
|
|
|
|
expect(validity).toBe(true)
|
|
|
|
})
|
|
|
|
|
2021-01-30 03:54:52 +01:00
|
|
|
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`)
|
|
|
|
})
|
2021-02-02 21:28:27 +01:00
|
|
|
|
|
|
|
it("should confirm a subtraction validity", () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const validity = isValid(
|
|
|
|
"{{ subtract [c390c23a7f1b6441c98d2fe2a51248ef3].[total profit] [c390c23a7f1b6441c98d2fe2a51248ef3].[total revenue] }}"
|
|
|
|
)
|
2021-02-02 21:28:27 +01:00
|
|
|
expect(validity).toBe(true)
|
|
|
|
})
|
2021-02-03 13:10:39 +01:00
|
|
|
|
2021-02-04 11:17:10 +01:00
|
|
|
it("test a very complex duration output", async () => {
|
|
|
|
const currentTime = new Date(1612432082000).toISOString(),
|
|
|
|
eventTime = new Date(1612432071000).toISOString()
|
|
|
|
const input = `{{duration ( subtract (date currentTime "X")(date eventTime "X")) "seconds"}}`
|
|
|
|
const output = await processString(input, {
|
|
|
|
currentTime,
|
|
|
|
eventTime,
|
|
|
|
})
|
|
|
|
expect(output).toBe("a few seconds")
|
|
|
|
})
|
|
|
|
|
2021-02-03 13:38:06 +01:00
|
|
|
it("should confirm a bunch of invalid strings", () => {
|
|
|
|
const invalids = ["{{ awd )", "{{ awdd () ", "{{ awdwad ", "{{ awddawd }"]
|
|
|
|
for (let invalid of invalids) {
|
|
|
|
const validity = isValid(invalid)
|
|
|
|
expect(validity).toBe(false)
|
|
|
|
}
|
2021-02-03 13:10:39 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("input a garbage string, expect it to be returned", async () => {
|
|
|
|
const input = `{{{{{{ } {{ ]] ] ] }}} {{ ] {{ { } { dsa { dddddd }}}}}}} }DDD`
|
|
|
|
const output = await processString(input, {})
|
|
|
|
expect(output).toBe(input)
|
|
|
|
})
|
2021-02-03 14:55:33 +01:00
|
|
|
|
|
|
|
it("getting a nice date from the user", async () => {
|
2021-03-16 19:16:56 +01:00
|
|
|
const input = { text: `{{ date user.subscriptionDue "DD-MM" }}` }
|
|
|
|
const context = JSON.parse(
|
|
|
|
`{"user":{"email":"test@test.com","roleId":"ADMIN","type":"user","tableId":"ta_users","subscriptionDue":"2021-01-12T12:00:00.000Z","_id":"ro_ta_users_us_test@test.com","_rev":"2-24cc794985eb54183ecb93e148563f3d"}}`
|
|
|
|
)
|
2021-02-03 14:55:33 +01:00
|
|
|
const output = await processObject(input, context)
|
|
|
|
expect(output.text).toBe("12-01")
|
|
|
|
})
|
2021-05-27 12:45:17 +02:00
|
|
|
|
|
|
|
it("should only invalidate a single string in an object", async () => {
|
|
|
|
const input = {
|
|
|
|
dataProvider:"{{ literal [c670254c9e74e40518ee5becff53aa5be] }}",
|
|
|
|
theme:"spectrum--lightest",
|
|
|
|
showAutoColumns:false,
|
|
|
|
quiet:true,
|
|
|
|
size:"spectrum--medium",
|
|
|
|
rowCount:8,
|
|
|
|
}
|
|
|
|
const output = await processObject(input, tableJson)
|
|
|
|
expect(output.dataProvider).not.toBe("Invalid Binding")
|
|
|
|
})
|
2021-03-16 19:16:56 +01:00
|
|
|
})
|