@schukai/monster 3.15.0 → 3.16.0
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
+
import {getLocaleOfDocument} from "../dom/locale.mjs";
|
8
9
|
import {Base} from "../types/base.mjs";
|
9
10
|
import {getGlobal, getGlobalObject} from "../types/global.mjs";
|
10
11
|
import {ID} from "../types/id.mjs";
|
@@ -169,6 +170,11 @@ function transform(value) {
|
|
169
170
|
let element;
|
170
171
|
let attribute;
|
171
172
|
let translations;
|
173
|
+
let date;
|
174
|
+
let locale;
|
175
|
+
let timestamp;
|
176
|
+
let map;
|
177
|
+
let keyValue;
|
172
178
|
|
173
179
|
switch (this.command) {
|
174
180
|
case "static":
|
@@ -405,25 +411,25 @@ function transform(value) {
|
|
405
411
|
if (key === undefined) {
|
406
412
|
throw new Error("missing key parameter");
|
407
413
|
}
|
408
|
-
|
414
|
+
|
409
415
|
// add empty strings
|
410
|
-
if (isString(key)&&key.trim()==="") {
|
411
|
-
concat += key;
|
416
|
+
if (isString(key) && key.trim() === "") {
|
417
|
+
concat += key;
|
412
418
|
continue;
|
413
419
|
}
|
414
|
-
|
420
|
+
|
415
421
|
if (!pf2.exists(key)) {
|
416
422
|
concat += key;
|
417
423
|
continue;
|
418
424
|
}
|
419
425
|
let v = pf2.getVia(key);
|
420
|
-
if(!isPrimitive(v)) {
|
426
|
+
if (!isPrimitive(v)) {
|
421
427
|
throw new Error("value is not primitive");
|
422
428
|
}
|
423
429
|
|
424
430
|
concat += v;
|
425
431
|
}
|
426
|
-
|
432
|
+
|
427
433
|
return concat;
|
428
434
|
case "path":
|
429
435
|
key = args.shift();
|
@@ -491,6 +497,117 @@ function transform(value) {
|
|
491
497
|
|
492
498
|
throw new Error("type not supported");
|
493
499
|
|
500
|
+
|
501
|
+
case "map":
|
502
|
+
map = new Map();
|
503
|
+
while (args.length > 0) {
|
504
|
+
keyValue = args.shift();
|
505
|
+
if (keyValue === undefined) {
|
506
|
+
throw new Error("missing key parameter");
|
507
|
+
}
|
508
|
+
|
509
|
+
keyValue = keyValue.split("=");
|
510
|
+
map.set(keyValue[0], keyValue[1]);
|
511
|
+
}
|
512
|
+
|
513
|
+
return map.get(value);
|
514
|
+
|
515
|
+
case "timestamp":
|
516
|
+
date = new Date(value);
|
517
|
+
timestamp = date.getTime();
|
518
|
+
if (isNaN(timestamp)) {
|
519
|
+
throw new Error("invalid date");
|
520
|
+
}
|
521
|
+
return timestamp;
|
522
|
+
|
523
|
+
case "time":
|
524
|
+
date = new Date(value);
|
525
|
+
if (isNaN(date.getTime())) {
|
526
|
+
throw new Error("invalid date");
|
527
|
+
}
|
528
|
+
|
529
|
+
try {
|
530
|
+
locale = getLocaleOfDocument();
|
531
|
+
return date.toLocaleTimeString(locale);
|
532
|
+
|
533
|
+
} catch (e) {
|
534
|
+
throw new Error("unsupported locale or missing format (" + e.message + ")");
|
535
|
+
}
|
536
|
+
|
537
|
+
|
538
|
+
case "year":
|
539
|
+
date = new Date(value);
|
540
|
+
if (isNaN(date.getTime())) {
|
541
|
+
throw new Error("invalid date");
|
542
|
+
}
|
543
|
+
|
544
|
+
return date.getFullYear();
|
545
|
+
|
546
|
+
case "month":
|
547
|
+
date = new Date(value);
|
548
|
+
if (isNaN(date.getTime())) {
|
549
|
+
throw new Error("invalid date");
|
550
|
+
}
|
551
|
+
|
552
|
+
return date.getMonth() + 1;
|
553
|
+
|
554
|
+
case "day":
|
555
|
+
date = new Date(value);
|
556
|
+
if (isNaN(date.getTime())) {
|
557
|
+
throw new Error("invalid date");
|
558
|
+
}
|
559
|
+
|
560
|
+
return date.getDate();
|
561
|
+
|
562
|
+
case "weekday":
|
563
|
+
date = new Date(value);
|
564
|
+
if (isNaN(date.getTime())) {
|
565
|
+
throw new Error("invalid date");
|
566
|
+
}
|
567
|
+
|
568
|
+
return date.getDay();
|
569
|
+
|
570
|
+
case "hour":
|
571
|
+
case "hours":
|
572
|
+
date = new Date(value);
|
573
|
+
if (isNaN(date.getTime())) {
|
574
|
+
throw new Error("invalid date");
|
575
|
+
}
|
576
|
+
|
577
|
+
return date.getHours();
|
578
|
+
|
579
|
+
case "minute":
|
580
|
+
case "minutes":
|
581
|
+
date = new Date(value);
|
582
|
+
if (isNaN(date.getTime())) {
|
583
|
+
throw new Error("invalid date");
|
584
|
+
}
|
585
|
+
|
586
|
+
return date.getMinutes();
|
587
|
+
|
588
|
+
case "second":
|
589
|
+
case "seconds":
|
590
|
+
date = new Date(value);
|
591
|
+
if (isNaN(date.getTime())) {
|
592
|
+
throw new Error("invalid date");
|
593
|
+
}
|
594
|
+
|
595
|
+
return date.getSeconds();
|
596
|
+
|
597
|
+
case "date":
|
598
|
+
date = new Date(value);
|
599
|
+
if (isNaN(date.getTime())) {
|
600
|
+
throw new Error("invalid date");
|
601
|
+
}
|
602
|
+
|
603
|
+
try {
|
604
|
+
locale = getLocaleOfDocument();
|
605
|
+
return date.toLocaleDateString(locale);
|
606
|
+
|
607
|
+
} catch (e) {
|
608
|
+
throw new Error("unsupported locale or missing format (" + e.message + ")");
|
609
|
+
}
|
610
|
+
|
494
611
|
case "i18n":
|
495
612
|
case "translation":
|
496
613
|
translations = getDocumentTranslations();
|
package/source/types/version.mjs
CHANGED
@@ -27,6 +27,17 @@ describe('Transformer', function () {
|
|
27
27
|
describe('Transformer.run()', function () {
|
28
28
|
|
29
29
|
[
|
30
|
+
['map:a=4:b=5:c=6', "a", "4"],
|
31
|
+
['date', "2023-02-14", "14.2.2023"],
|
32
|
+
['year', "2023-02-14", 2023],
|
33
|
+
['month', "2023-02-14", 2],
|
34
|
+
['day', "2023-02-14", 14],
|
35
|
+
['weekday', "2023-02-14", 2],
|
36
|
+
['minutes', "2023-02-14 06:12:21", 12],
|
37
|
+
['seconds', "2023-02-14 06:12:21", 21],
|
38
|
+
['hours', "2023-02-14 06:12:21", 6],
|
39
|
+
['time', "2023-02-14 06:12:21", "06:12:21"],
|
40
|
+
['timestamp', "2023-02-14", 1676332800000],
|
30
41
|
['concat:a.b.c:test:a.b.d', {a: {b: {c: 4, d: 6}}}, "4test6"],
|
31
42
|
['concat:a.b.c:\\ \\ :a.b.d', {a: {b: {c: 4, d: 6}}}, "4 6"],
|
32
43
|
['concat:a.b.c:,:a.b.d', {a: {b: {c: 4, d: 6}}}, "4,6"],
|
package/test/cases/monster.mjs
CHANGED