btrz-liquid-templates 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,731 @@
1
+ describe("index.js", () => {
2
+ const {expect} = require("chai");
3
+ let data = null;
4
+ beforeEach(() => {
5
+ data = {
6
+ brand: {
7
+ logos: {
8
+ default: "https://s3.amazonaws.com/btrz-images-test2/b5416770-795b-11ec-b752-8b6ef2019164.png"
9
+ }
10
+ },
11
+ transaction: {
12
+ _id: "5c9f8f8f8f8f8f8f8f8f8f8",
13
+ payments: [
14
+ {
15
+ provider: "inperson",
16
+ type: "cash",
17
+ displayName: "Cash",
18
+ status: "success",
19
+ amount: 3.86,
20
+ rollbackPayload: null,
21
+ acceptedCurrency: {
22
+ isocode:"GTQ",
23
+ symbol:"Q",
24
+ buy:7.77,
25
+ sell:7.57,
26
+ channels:[]
27
+ },
28
+ displayAmount: "30.00",
29
+ displayCurrency: {
30
+ isocode:"GTQ",
31
+ symbol:"Q",
32
+ buy:7.77,
33
+ sell:7.57
34
+ },
35
+ acceptedCurrencyAmount: 30,
36
+ exchangeDetails: {
37
+ exchangeCurrency:{},
38
+ acceptedCurrencyAmount: 3000000,
39
+ acceptedCurrencyAmountReceived: 3000000,
40
+ acceptedCurrencyAmountReturned: 0,
41
+ exchangeCurrencyAmount: 386100,
42
+ exchangeCurrencyAmountReceived: 386100,
43
+ exchangeCurrencyAmountReturned: 0
44
+ }
45
+ },
46
+ {
47
+ amount: 20,
48
+ displayCurrency: {
49
+ isocode:"USD",
50
+ symbol:"$",
51
+ buy:7.77,
52
+ sell:7.57
53
+ },
54
+ },
55
+ {
56
+ amount: 30
57
+ }
58
+ ],
59
+ updatedAt: {
60
+ value: "2021-12-20T16:08:00.488Z",
61
+ offset: 0
62
+ }
63
+ },
64
+ providerPreferences: {
65
+ preferences: {
66
+ colors: {
67
+ brandBackground: "#ee732f"
68
+ },
69
+ timeFormat: "h:MM TT",
70
+ timeZone: {
71
+ name: "(UTC-5:00) New York (United States), Toronto (Canada)",
72
+ daylight: true,
73
+ tz: "America/Toronto"
74
+ },
75
+ dateFormat: "mm/dd/yyyy",
76
+ multiCurrency: true,
77
+ multiline: `something here
78
+ and some more here
79
+ and some more here`,
80
+ supportedCurrencies: [
81
+ {
82
+ isocode: "USD",
83
+ symbol: "$",
84
+ enabled: true,
85
+ printSymbol: true
86
+ },
87
+ {
88
+ isocode: "CAD",
89
+ symbol: "$",
90
+ enabled: true,
91
+ printSymbol: true
92
+
93
+ }
94
+ ]
95
+ }
96
+ },
97
+ tickets: [
98
+ {
99
+ fare: "adult",
100
+ total: 2800000
101
+ },
102
+ {
103
+ fare: "child",
104
+ },
105
+ {
106
+ fare: "person",
107
+ }
108
+ ],
109
+ ticket: {
110
+ fare: "adult",
111
+ total: 2836000,
112
+ displayTotalStr: "28.36",
113
+ "displayTotalStr2": "289",
114
+ "displayTotalStr3": "289.00",
115
+ arrivalTimestamp: "2022-01-19T13:00:00.000Z",
116
+ departureTimestamp: "2022-01-20T08:00:00.000Z",
117
+ taxes: [],
118
+ ssrs: [
119
+ {
120
+ subTotal: 2800000,
121
+ },
122
+ {
123
+ subTotal: 3100000,
124
+ }
125
+ ],
126
+ displayCurrency: {
127
+ isocode: "CAD",
128
+ symbol: "$",
129
+ buy: 1,
130
+ sell: 1,
131
+ },
132
+ createdAt: {
133
+ value: "2021-12-21T16:38:00.488Z",
134
+ offset: 0
135
+ }
136
+ },
137
+ fromStation: {
138
+ timeZone: "America/Toronto"
139
+ },
140
+ toStation: {
141
+ timeZone: "America/Toronto"
142
+ },
143
+ invoiceProvider: {
144
+ currencies:[
145
+ {
146
+ isocode: "GTQ",
147
+ symbol: "Q",
148
+ printSymbol: true
149
+ }
150
+ ]
151
+ },
152
+ localizer: {
153
+ get: (key) => {
154
+ if (key === "html") {
155
+ return `<h1>Hello</h1><br/>
156
+ something
157
+ <b>html</b>
158
+ <i>Italix</i>`;
159
+ }
160
+ if (key === "text") {
161
+ return "text here please";
162
+ }
163
+ if ( key === "french") {
164
+ return "Date / heure d'arrivée:";
165
+ }
166
+ if ( key === "double") {
167
+ return `This " is " awesome`;
168
+ }
169
+ if (key === "complex") {
170
+ return `Some ' complex ' string" here" \``
171
+ }
172
+ if (key === "textWithTab") {
173
+ return " Some text with tabs here";
174
+ }
175
+ return `=>${key}`;
176
+ }
177
+ },
178
+ lang: "es-ar"
179
+ }
180
+ });
181
+
182
+ it("should load images via https", async () => {
183
+ const tpl = require("../src/index");
184
+ const template = `{
185
+ "content": [
186
+ {
187
+ "image" : "{% httpImg brand.logos.default %}"
188
+ }
189
+ ]
190
+ }`;
191
+ const documentDefinition = await tpl.processToObj(template, data);
192
+ expect(documentDefinition).to.be.eql({
193
+ "content": [
194
+ {
195
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAAeBAMAAAD3Fx6aAAAAG1BMVEX///8/nz9fr1/f79+/379/v3+fz58fjx8AgADFQF6MAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAApBJREFUSInFlj1v2zAQhmlTtjsabYBq9BAEHtUP2BrVoGg1esigUUGGenRSwNBYx0l0P7u8O1I6Sg0a0DbMgZZeyfdQ5PHlKfVKq4BbXXaf6Dhr+lM1R4fn7pMcsqY/OR3KzpOCuMWJ6Rj9yzXA5mx0nOGXM9KHsDsjPYKafhcf4aHs0/VtVV+5/3yuLrrSgXTN9JHbfRFl4lfqExyEaVt8I4fv5nImpLHL2zqYHvHMx273+fQBh18R/SfRW+kI9CFteBvSxPTpcesJOeBNJqQj0HPYK5zN+k4tKnjy6WY9ton+hVDzImz1rRJSQ+/m7Vvp+prmUPPkjiiOyLqUnbCAKdJrkweexG0NvwPotj0S9pHEOSIFvcAsw9V5aoxBStTGPKpAOn70gKYfCSuPHgMFjvB7eYk8idq6GUcI/dLcvLPzSL+C7lYUf3O2ZCnxMEJsqT1lZpRRtv2R9KiRAd+ZKuVL2PpO/TY6MvSPGJd8/W/62KebYfoSBqho6cLoFK4U9P0r9KRPp/Vf2nwNpBvMJpxuPn12ED01xLWcPkkXhY+je7XQMsBpPPrEpE3eeoefdSI20yMfFwc4TY+eYrb16doldkv3JOMTAU7j0VPjFhO7bZZXibffKy769MOdo3uS8cZ9EFxm3d44LX9DjI7iOS1N7ABPMUuX0jDIaSTdJPEU5/ODomjktKWyfQo7HNWcnZboUipCTFbSFzGNfw5wgycsntR8ZlFvhvOc4XG6aehCGoWuunRatIuJvcZvMbZbJ7Z3b2UNXUipNOtQOm42bSuWUikKmtn+E8svqqW30jHoVDJyVQkX7nJley4hd5mgt9LB9Pr+xt6bivr+ki+/VVjcco/l83uEt/RG+j/9L0R48odC2pi1AAAAAElFTkSuQmCC"
196
+ }
197
+ ]
198
+ });
199
+ });
200
+
201
+ it("should apply tag after translations", async () => {
202
+ const tpl = require("../src/index");
203
+ const template = `{
204
+ "content": [
205
+ "{% t 'french' | upcase %}",
206
+ "{% t 'double' | upcase %}",
207
+ "{% t 'complex' | upcase %}"
208
+ ]
209
+ }`;
210
+ const documentDefinition = await tpl.processToObj(template, data);
211
+ expect(documentDefinition).to.be.eql({
212
+ "content": [
213
+ "DATE / HEURE D'ARRIVÉE:",
214
+ "THIS `` IS `` AWESOME",
215
+ "SOME ' COMPLEX ' STRING`` HERE`` `"
216
+ ]
217
+ });
218
+ });
219
+
220
+ it("should return money even when values is zero", async () => {
221
+ const tpl = require("../src/index");
222
+ const template = `{
223
+ "content": [
224
+ {%for payment in transaction.payments %}
225
+ "{%- curcySymbol payment -%}",
226
+ "{%- curcyIso payment -%}",
227
+ {% endfor %}
228
+ "{%- money ticket total -%}",
229
+ "{%- curcySymbol ticket -%}",
230
+ "{%- curcyIso ticket -%}",
231
+ "{%- moneyReduce ticket.ssrs subTotal -%}"
232
+ ]
233
+ }`;
234
+ data.ticket.total = 0;
235
+ data.ticket.ssrs[0].subTotal = 0;
236
+ const documentDefinition = await tpl.processToObj(template, data);
237
+ expect(documentDefinition).to.be.eql({
238
+ "content": [
239
+ "Q",
240
+ "GTQ",
241
+ "$",
242
+ "USD",
243
+ "$",
244
+ "USD",
245
+ "0.00",
246
+ "$",
247
+ "CAD",
248
+ "31.00",
249
+ ]
250
+ });
251
+ });
252
+
253
+ it("should return currency in letters", async () => {
254
+ const tpl = require("../src/index");
255
+ const template = `{
256
+ "content": [
257
+ "{% toLetters ticket.displayTotalStr %}",
258
+ "{% toLetters ticket.displayTotalStr2 %}",
259
+ "{% toLetters ticket.displayTotalStr3 %}",
260
+ "{% curcyName ticket %}",
261
+ "{% curcyName invoiceProvider.currencies[0] %}"
262
+ ]
263
+ }`;
264
+ const documentDefinition = await tpl.processToObj(template, data);
265
+ expect(documentDefinition).to.be.eql({
266
+ "content": [
267
+ "Veintiocho =>with treinta y seis =>cents",
268
+ "Doscientos ochenta y nueve",
269
+ "Doscientos ochenta y nueve",
270
+ "=>CAD",
271
+ "=>GTQ"
272
+ ]
273
+ })
274
+ });
275
+
276
+ it("should return alines with the correct colour", async () => {
277
+ const tpl = require("../src/index");
278
+ const template = `{
279
+ "content": [
280
+ {%- hline 356 2 60,60,60 -%},
281
+ {%- hline 356 2 #404040 -%},
282
+ {%- hline -%},
283
+ {%- hline 100 2 providerPreferences.preferences.colors.brandBackground -%}
284
+ ]
285
+ }`;
286
+ const documentDefinition = await tpl.processToObj(template, data);
287
+ expect(documentDefinition).to.be.eql({
288
+ "content": [
289
+ {
290
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(60,60,60);stroke-width:2' /></svg>",
291
+ "width": 356
292
+ },
293
+ {
294
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(64,64,64);stroke-width:2' /></svg>",
295
+ "width": 356
296
+ },
297
+ {
298
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(0,0,0);stroke-width:1' /></svg>",
299
+ "width": 500
300
+ },
301
+ {
302
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(238,115,47);stroke-width:2' /></svg>",
303
+ "width": 100
304
+ }
305
+ ]
306
+ });
307
+ });
308
+
309
+ it("should return the error JSON in the err.data property", async () => {
310
+ const tpl = require("../src/index");
311
+ const template = `{
312
+ "content": [{},
313
+ ]
314
+ }`;
315
+ try {
316
+ await tpl.processToObj(template, data);
317
+ expect(1).to.be.eql(2);
318
+ } catch (err) {
319
+ //spaces are important in this test
320
+ expect(err.data).to.be.eql(`{
321
+ "content": [{},
322
+ ]
323
+ }`);
324
+ }
325
+ });
326
+
327
+ it.skip("should parse dates from ISO dates", async () => {
328
+ const tpl = require("../src/index");
329
+ const template = `{
330
+ "content": [
331
+ "{%- humanDateTime ticket arrivalTimestamp %}",
332
+ "{%- humanDate ticket arrivalTimestamp %}",
333
+ "{%- dateTime ticket arrivalTimestamp %}",
334
+ "{%- dateTime ticket arrivalTimestamp mm/dd/yyyy hh:MM:ss %}",
335
+ "{%- dateF ticket arrivalTimestamp %}",
336
+ "{%- timeF ticket arrivalTimestamp %}",
337
+ "{%- humanDateTime ticket arrivalTimestamp false%}",
338
+ "{%- humanDate ticket arrivalTimestamp false%}",
339
+ "{%- dateF ticket arrivalTimestamp false %}",
340
+ "{%- timeF ticket arrivalTimestamp false %}",
341
+ "{%- arrivalDateTime ticket %}",
342
+ "{%- departureDateTime ticket %}",
343
+ "{%- humanArrivalDateTime ticket %}",
344
+ "{%- humanDepartureDateTime ticket %}",
345
+ "{%- arrivalDate ticket %}",
346
+ "{%- arrivalTime ticket %}",
347
+ "{%- departureDate ticket %}",
348
+ "{%- departureTime ticket %}",
349
+ "{%- humanArrivalDate ticket %}",
350
+ "{%- humanDepartureDate ticket %}"
351
+ ]
352
+ }`;
353
+ const documentDefinition = await tpl.processToObj(template, data);
354
+ expect(documentDefinition).to.be.eql({
355
+ "content": [
356
+ "Mié Ene 19, 2022 8:00 AM",
357
+ "Mié Ene 19, 2022",
358
+ "01/19/2022 8:00 AM",
359
+ "01/19/2022 08:00:00",
360
+ "01/19/2022",
361
+ "8:00 AM",
362
+ "Mié Ene 19, 2022 8:00 AM",
363
+ "Mié Ene 19, 2022",
364
+ "01/19/2022",
365
+ "8:00 AM",
366
+ "01/19/2022 8:00 AM",
367
+ "01/20/2022 3:00 AM",
368
+ "Mié Ene 19, 2022 8:00 AM",
369
+ "Jue Ene 20, 2022 3:00 AM",
370
+ "01/19/2022",
371
+ "8:00 AM",
372
+ "01/20/2022",
373
+ "3:00 AM",
374
+ "Mié Ene 19, 2022",
375
+ "Jue Ene 20, 2022",
376
+ ]
377
+ });
378
+ });
379
+
380
+ it("should parse barcode", async () => {
381
+ const tpl = require("../src/index");
382
+ const template = `{
383
+ "content": [
384
+ {%- barcode -%},
385
+ {%- barcode 1234 code128 10 2 10,5,2,4 -%},
386
+ {%- barcode 1234 code128 10 2 10,5 -%}
387
+ ]
388
+ }`;
389
+ const documentDefinition = await tpl.processToObj(template, data);
390
+ expect(documentDefinition).to.be.eql({
391
+ "content": [
392
+ {
393
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3gAAAB4CAYAAACghlDTAAAAAklEQVR4AewaftIAAAPmSURBVO3BsW0EQQwEsJH673mcbnSA8ZlAcpI0H9rmNTN5tc2XmcmrbX4xM/nSNr+Ymbza5jUzebXNl5nJq23+Y2byi7Z5zUxebfOambza5svM5Bdt85qZvNrmy8zk1TZfZiZf2uY1M/mPtvkyM3m1zS9mJq+2ec1MvrTNl5nJq23+Y2byi7Z5zUxebfOamXxpm1/MTF5t85qZvNrmy8zk1TZfZia/aJvXzOTVNl9mJq+2ec1MvrTNl5nJq21+MTP50ja/mJm82uY1M3m1zZeZyattvsxMftE2r5nJq22+zExebfNlZvKlbV4zk/9omy8zk1fbfJmZfGmb18zk1Ta/mJm82uYXM5MvbfOambza5svM5Bdt85qZvNrmy8zk1TZfZiavtnnNTL60zZeZyattfjEz+dI2v5iZvNrmNTN5tc2XmcmXtvkyM/nSNl9mJl/a5jUz+dI2/zEz+Y+2ec1MXm3zmpl82QAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACX8+ALXvFMrcDQAAAABJRU5ErkJggg==",
394
+ "width": 200,
395
+ "margin": [0,0,0,0]
396
+ },{
397
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAAAoCAYAAADufVZCAAAAAklEQVR4AewaftIAAADcSURBVO3BsY0EQRDDQKrzz1nvjrXA4R0ZrApQPrTllYRXW74k4Rdt+ZKEV1teSfjSli9J+EVbviTh1ZZfJOHVllcSvrTlF0l4teVLEl5t+ZKE/2jLKwlf2vKLJPyiLa8kvNrySsKXQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMPxSXLk+8Qj8BAAAAAElFTkSuQmCC",
398
+ "width": 2,
399
+ "margin": [10,5,2,4]
400
+ },{
401
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAAAoCAYAAADufVZCAAAAAklEQVR4AewaftIAAADcSURBVO3BsY0EQRDDQKrzz1nvjrXA4R0ZrApQPrTllYRXW74k4Rdt+ZKEV1teSfjSli9J+EVbviTh1ZZfJOHVllcSvrTlF0l4teVLEl5t+ZKE/2jLKwlf2vKLJPyiLa8kvNrySsKXQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMPxSXLk+8Qj8BAAAAAElFTkSuQmCC",
402
+ "width": 2,
403
+ "margin": [0,0,0,0]
404
+ }
405
+ ]
406
+ });
407
+ });
408
+
409
+ it("should not break if h has not data", async () => {
410
+ const tpl = require("../src/index");
411
+ const template = `{
412
+ "content": [
413
+ {%- h lexiconKeys.toc -%}
414
+ ]
415
+ }`;
416
+ const d = {
417
+ }
418
+ const documentDefinition = await tpl.processToObj(template, d);
419
+ expect(documentDefinition).to.be.eql({
420
+ "content": [
421
+ {
422
+ "text": ""
423
+ }
424
+ ]
425
+ });
426
+ });
427
+
428
+ it("should return a parsed liquidTemplate", async () => {
429
+ const tpl = require("../src/index");
430
+ const template = `{
431
+ "content": [
432
+ "Transaction Id: {{transaction._id}}",
433
+ "{% t 'h' %}",
434
+ {%- for ticket in tickets -%}
435
+ {%- if forloop.last == false -%}
436
+ "{{ticket.fare}} and {%t ticket.fare %}",
437
+ {%- else -%}
438
+ "{{ticket.fare}} and {%t ticket.fare %}",
439
+ {%- endif -%}
440
+ {%- endfor -%}
441
+ {%- hline 356 2 60,60,60 -%},
442
+ {%- hline -%},
443
+ {%- barcode -%},
444
+ {%- barcode 1234 code128 10 2 10,5,2,4 -%},
445
+ {%- h 'html' -%},
446
+ "{%- money ticket total -%}",
447
+ "{%- curcySymbol ticket -%}",
448
+ "{%- curcyIso ticket -%}",
449
+ "{%- moneyReduce ticket.ssrs subTotal -%}",
450
+ "{%- humanDateTime ticket createdAt %}",
451
+ "{%- humanDate ticket createdAt %}",
452
+ "{%- dateTime ticket createdAt %}",
453
+ "{%- dateTime ticket createdAt mm/dd/yyyy hh:MM:ss %}",
454
+ "{%- dateF ticket createdAt %}",
455
+ "{%- timeF ticket createdAt %}",
456
+ {% txt providerPreferences.preferences.multiline %},
457
+ {% txt providerPreferences.preferences.multiline small %}
458
+ ]
459
+ }`;
460
+ const documentDefinition = await tpl.processToObj(template, data);
461
+ expect(documentDefinition).to.be.eql({
462
+ "content": [
463
+ "Transaction Id: 5c9f8f8f8f8f8f8f8f8f8f8",
464
+ "=>h","adult and =>adult","child and =>child","person and =>person",{
465
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(60,60,60);stroke-width:2' /></svg>",
466
+ "width": 356
467
+ },{
468
+ "svg": "<svg height='2' width='100'><line x1='0' y1='0' x2='1000' y2='0' style='stroke:rgb(0,0,0);stroke-width:1' /></svg>",
469
+ "width": 500
470
+ },{
471
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3gAAAB4CAYAAACghlDTAAAAAklEQVR4AewaftIAAAPmSURBVO3BsW0EQQwEsJH673mcbnSA8ZlAcpI0H9rmNTN5tc2XmcmrbX4xM/nSNr+Ymbza5jUzebXNl5nJq23+Y2byi7Z5zUxebfOambza5svM5Bdt85qZvNrmy8zk1TZfZiZf2uY1M/mPtvkyM3m1zS9mJq+2ec1MvrTNl5nJq23+Y2byi7Z5zUxebfOamXxpm1/MTF5t85qZvNrmy8zk1TZfZia/aJvXzOTVNl9mJq+2ec1MvrTNl5nJq21+MTP50ja/mJm82uY1M3m1zZeZyattvsxMftE2r5nJq22+zExebfNlZvKlbV4zk/9omy8zk1fbfJmZfGmb18zk1Ta/mJm82uYXM5MvbfOambza5svM5Bdt85qZvNrmy8zk1TZfZiavtnnNTL60zZeZyattfjEz+dI2v5iZvNrmNTN5tc2XmcmXtvkyM/nSNl9mJl/a5jUz+dI2/zEz+Y+2ec1MXm3zmpl82QAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACRsAAABO2AAAAHDCBgAAgBM2AAAAnLABAADghA0AAAAnbAAAADhhAwAAwAkbAAAATtgAAABwwgYAAIATNgAAAJywAQAA4IQNAAAAJ2wAAAA4YQMAAMAJGwAAAE7YAAAAcMIGAACAEzYAAACcsAEAAOCEDQAAACdsAAAAOGEDAADACX8+ALXvFMrcDQAAAABJRU5ErkJggg==",
472
+ "width": 200,
473
+ "margin": [0,0,0,0]
474
+ },{
475
+ "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAAAoCAYAAADufVZCAAAAAklEQVR4AewaftIAAADcSURBVO3BsY0EQRDDQKrzz1nvjrXA4R0ZrApQPrTllYRXW74k4Rdt+ZKEV1teSfjSli9J+EVbviTh1ZZfJOHVllcSvrTlF0l4teVLEl5t+ZKE/2jLKwlf2vKLJPyiLa8kvNrySsKXQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMQ9KMPxSXLk+8Qj8BAAAAAElFTkSuQmCC",
476
+ "width": 2,
477
+ "margin": [10,5,2,4]
478
+ },{"style": "header", "text": "Hello"},{"text": ""},{"text": "something"},{"text": ""},{"bold": true, "text": "html"},{"text": ""},{"italics": true, "text": "Italix"},
479
+ "28.36",
480
+ "$",
481
+ "CAD",
482
+ "59.00",
483
+ "Mar Dic 21, 2021 11:38 AM",
484
+ "Mar Dic 21, 2021",
485
+ "12/21/2021 11:38 AM",
486
+ "12/21/2021 11:38:00",
487
+ "12/21/2021",
488
+ "11:38 AM",
489
+ {"text": "something here"},{"text": "and some more here"},{"text": "and some more here"},
490
+ {"text": "something here", "style": "small"},{"text": "and some more here", "style": "small"},{"text": "and some more here", "style": "small"}
491
+ ]
492
+ });
493
+ });
494
+
495
+ it("should format dateTime outside and inside a for loop", async () => {
496
+ data.things = [
497
+ {
498
+ id: 1,
499
+ createdAt: {
500
+ value: "2021-12-21T16:38:00.488Z",
501
+ offset: 0
502
+ }
503
+ },
504
+ {
505
+ id: 2,
506
+ createdAt: {
507
+ value: "2021-12-22T16:32:22.488Z",
508
+ offset: 0
509
+ }
510
+ }
511
+ ];
512
+ const tpl = require("../src/index");
513
+ const template = `{
514
+ "content": [
515
+ {%- for thing in things -%}
516
+ "{{thing.id}}",
517
+ "{%- dateTime thing createdAt %}",
518
+ "{%- dateTime thing wrongProp %}",
519
+ {%- endfor -%}
520
+ "{%- dateTime transaction updatedAt %}",
521
+ "{%- dateTime transaction wrongProp %}"
522
+ ]
523
+ }`;
524
+ const documentDefinition = await tpl.processToObj(template, data);
525
+ expect(documentDefinition).to.be.eql({
526
+ "content": [
527
+ "1",
528
+ "12/21/2021 11:38 AM",
529
+ "PNA",
530
+ "2",
531
+ "12/22/2021 11:32 AM",
532
+ "PNA",
533
+ "12/20/2021 11:08 AM",
534
+ "PNA",
535
+ ]
536
+ });
537
+ });
538
+
539
+ it("should format dateF outside and inside a for loop", async () => {
540
+ data.things = [
541
+ {
542
+ data: {createdAt: {value: "2021-12-21T16:38:22.488Z", offset: 0}}
543
+ },
544
+ {
545
+ data: {createdAt: {value: "2021-12-22T16:32:22.488Z", offset: 0}}
546
+ }
547
+ ];
548
+ const tpl = require("../src/index");
549
+ const template = `{
550
+ "content": [
551
+ {%- for thing in things -%}
552
+ "{%- dateF thing.data createdAt %}",
553
+ "{%- dateF thing.data wrongProp %}",
554
+ {%- endfor -%}
555
+ "{%- dateF transaction updatedAt %}",
556
+ "{%- dateF transaction wrongProp %}"
557
+ ]
558
+ }`;
559
+ const documentDefinition = await tpl.processToObj(template, data);
560
+ expect(documentDefinition).to.be.eql({
561
+ "content": [
562
+ "12/21/2021",
563
+ "PNA",
564
+ "12/22/2021",
565
+ "PNA",
566
+ "12/20/2021",
567
+ "PNA",
568
+ ]
569
+ });
570
+ });
571
+
572
+ it("should correctly format 24-hour time strings (HH)", async () => {
573
+ data.things = [
574
+ {
575
+ data: {time: "20:53"}
576
+ },
577
+ {
578
+ data: {time: "10:53"}
579
+ }
580
+ ];
581
+ const tpl = require("../src/index");
582
+ const template = `{
583
+ "content": [
584
+ {%- for thing in things -%}
585
+ "{%- timeF thing.data time %}",
586
+ "{%- timeF thing.data wrongProp %}",
587
+ {%- endfor -%}
588
+ "{%- timeF transaction updatedAt %}",
589
+ "{%- timeF transaction wrongProp %}"
590
+ ]
591
+ }`;
592
+ const documentDefinition = await tpl.processToObj(template, data);
593
+ expect(documentDefinition).to.be.eql({
594
+ "content": [
595
+ "8:53 PM",
596
+ "PNA",
597
+ "10:53 AM",
598
+ "PNA",
599
+ "11:08 AM",
600
+ "PNA",
601
+ ]
602
+ });
603
+ });
604
+
605
+ it("should format timeF outside and inside a for loop", async () => {
606
+ data.things = [
607
+ {
608
+ data: {createdAt: {value: "2021-12-21T16:38:22.488Z", offset: 0}}
609
+ },
610
+ {
611
+ data: {createdAt: {value: "2021-12-22T16:32:22.488Z", offset: 0}}
612
+ }
613
+ ];
614
+ const tpl = require("../src/index");
615
+ const template = `{
616
+ "content": [
617
+ {%- for thing in things -%}
618
+ "{%- timeF thing.data createdAt %}",
619
+ "{%- timeF thing.data wrongProp %}",
620
+ {%- endfor -%}
621
+ "{%- timeF transaction updatedAt %}",
622
+ "{%- timeF transaction wrongProp %}"
623
+ ]
624
+ }`;
625
+ const documentDefinition = await tpl.processToObj(template, data);
626
+ expect(documentDefinition).to.be.eql({
627
+ "content": [
628
+ "11:38 AM",
629
+ "PNA",
630
+ "11:32 AM",
631
+ "PNA",
632
+ "11:08 AM",
633
+ "PNA",
634
+ ]
635
+ });
636
+ });
637
+
638
+
639
+ it("should return a french translation", async () => {
640
+ const tpl = require("../src/index");
641
+ const template = `{
642
+ "content": [
643
+ "{% t 'french' %}"
644
+ ]
645
+ }`;
646
+ const documentDefinition = await tpl.processToObj(template, data);
647
+ expect(documentDefinition).to.be.eql({
648
+ "content": [
649
+ "Date / heure d'arrivée:"
650
+ ]
651
+ });
652
+ });
653
+
654
+ it("should return a text tag with escaped tabs", async () => {
655
+ const tpl = require("../src/index");
656
+ const template = `{
657
+ "content": [
658
+ {%- h 'textWithTab' -%}
659
+ ]
660
+ }`;
661
+ const documentDefinition = await tpl.processToObj(template, data);
662
+ expect(documentDefinition).to.be.eql({
663
+ "content": [
664
+ {
665
+ "text": "\tSome text \t with tabs \t here"
666
+ }
667
+ ]
668
+ });
669
+ });
670
+
671
+ it("should return a translation with double quotes replaced with `` ", async () => {
672
+ const tpl = require("../src/index");
673
+ const template = `{
674
+ "content": [
675
+ "{% t 'double' %}"
676
+ ]
677
+ }`;
678
+ const documentDefinition = await tpl.processToObj(template, data);
679
+ expect(documentDefinition).to.be.eql({
680
+ "content": [
681
+ "This `` is `` awesome"
682
+ ]
683
+ });
684
+ });
685
+
686
+ it("should allow to loop over an object keys", async () => {
687
+ const tpl = require("../src/index");
688
+ const template = `{
689
+ "content": [
690
+ {% assign objectKeys = ticket.displayCurrency | keys %}
691
+ {% for aKey in objectKeys %}
692
+ {% if forloop.first %}
693
+ {
694
+ {% endif %}
695
+ {% unless forloop.last %}
696
+ "key{{forloop.index}}": "{{aKey}}",
697
+ {% else %}
698
+ "key{{forloop.index}}": "{{aKey}}"}
699
+ {% endunless %}
700
+ {% endfor %}
701
+ ]
702
+ }`
703
+ const documentDefinition = await tpl.processToObj(template, data);
704
+ expect(documentDefinition).to.be.eql({
705
+ "content": [
706
+ {
707
+ "key1": "isocode",
708
+ "key2": "symbol",
709
+ "key3": "buy",
710
+ "key4": "sell",
711
+ }
712
+ ]
713
+ });
714
+ });
715
+
716
+ it("should return ESCP code", async () => {
717
+ const tpl = require("../src/index");
718
+ const template = `
719
+ {% escInitQzTray %}
720
+ {% lineSpacing2 %}
721
+ {% fontSelection %}
722
+ {% escBold %}
723
+ Shift location closure #: {{transaction._id}}
724
+ {% escNewLine %}
725
+ {% escCancelBold %}
726
+ {% escCondensed %}
727
+ {% escEject %}`
728
+ const documentDefinition = await tpl.processToEscp(template, data);
729
+ expect(documentDefinition).to.be.eql(`\\x1B\\x69\\x61\\x00\\x1B\\x40,\\x1B\\x33\\x24,\\x1B\\x4D,\\x1B\\x45,Shift location closure #: 5c9f8f8f8f8f8f8f8f8f8f8,\\n,\\x1B\\x46,\\x1B\\x0F,\\x0C`);
730
+ });
731
+ });