@schukai/monster 3.18.0 → 3.20.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schukai/monster",
3
- "version": "3.18.0",
3
+ "version": "3.20.0",
4
4
  "description": "Monster is a simple library for creating fast, robust and lightweight websites.",
5
5
  "keywords": [
6
6
  "framework",
@@ -186,33 +186,33 @@ function transform(value) {
186
186
  case "tolowercase":
187
187
  validateString(value);
188
188
  return value.toLowerCase();
189
-
189
+
190
190
  case "contains":
191
191
  if (isString(value)) {
192
- return value.includes(args[0]);
192
+ return value.includes(args[0]);
193
193
  }
194
-
194
+
195
195
  if (isArray(value)) {
196
196
  return value.includes(args[0]);
197
197
  }
198
-
198
+
199
199
  if (isObject(value)) {
200
200
  return value.hasOwnProperty(args[0]);
201
201
  }
202
-
202
+
203
+ return false;
204
+
205
+ case "has-entries":
206
+ case "hasentries":
207
+ if (isObject(value)) {
208
+ return Object.keys(value).length > 0;
209
+ }
210
+
211
+ if (isArray(value)) {
212
+ return value.length > 0;
213
+ }
214
+
203
215
  return false;
204
-
205
- case "has-entries":
206
- case "hasentries":
207
- if (isObject(value)) {
208
- return Object.keys(value).length > 0;
209
- }
210
-
211
- if (isArray(value)) {
212
- return value.length > 0;
213
- }
214
-
215
- return false;
216
216
 
217
217
  case "isundefined":
218
218
  case "is-undefined":
@@ -576,6 +576,70 @@ function transform(value) {
576
576
 
577
577
  return map.get(value);
578
578
 
579
+ case "equals":
580
+
581
+ if (args.length === 0) {
582
+ throw new Error("missing value parameter");
583
+ }
584
+
585
+ validatePrimitive(value);
586
+
587
+ const equalsValue = args.shift();
588
+
589
+ /**
590
+ * The history of “typeof null”
591
+ * https://2ality.com/2013/10/typeof-null.html
592
+ * In JavaScript, typeof null is 'object', which incorrectly suggests
593
+ * that null is an object.
594
+ */
595
+ if (value === null) {
596
+ if (equalsValue === "null") {
597
+ return true;
598
+ }
599
+ return false;
600
+ }
601
+
602
+ const typeOfValue = typeof value;
603
+
604
+ switch (typeOfValue) {
605
+ case "string":
606
+ return value === equalsValue;
607
+ case "number":
608
+ return value === parseFloat(equalsValue);
609
+ case "boolean":
610
+ return value === (equalsValue === "true" || equalsValue === "on");
611
+ case "undefined":
612
+ return equalsValue === "undefined";
613
+ default:
614
+ throw new Error("type not supported");
615
+ }
616
+
617
+ case "money":
618
+ case "currency":
619
+
620
+ try {
621
+ locale = getLocaleOfDocument();
622
+ } catch (e) {
623
+ throw new Error("unsupported locale or missing format (" + e.message + ")");
624
+ }
625
+
626
+ const currency = value.substring(0, 3);
627
+ if (!currency) {
628
+ throw new Error("missing currency parameter");
629
+ }
630
+
631
+ const maximumFractionDigits = args?.[0] || 2;
632
+ const roundingIncrement = args?.[1] || 5;
633
+
634
+ const nf = new Intl.NumberFormat(locale, {
635
+ style: "currency",
636
+ currency: currency,
637
+ maximumFractionDigits: maximumFractionDigits,
638
+ roundingIncrement: roundingIncrement,
639
+ });
640
+
641
+ return nf.format(value.substring(3));
642
+
579
643
  case "timestamp":
580
644
  date = new Date(value);
581
645
  timestamp = date.getTime();
@@ -598,6 +662,34 @@ function transform(value) {
598
662
  throw new Error("unsupported locale or missing format (" + e.message + ")");
599
663
  }
600
664
 
665
+ case "datetime":
666
+ date = new Date(value);
667
+ if (isNaN(date.getTime())) {
668
+ throw new Error("invalid date");
669
+ }
670
+
671
+ try {
672
+ locale = getLocaleOfDocument();
673
+ return date.toLocaleString(locale);
674
+
675
+ } catch (e) {
676
+ throw new Error("unsupported locale or missing format (" + e.message + ")");
677
+ }
678
+
679
+ case "date":
680
+ date = new Date(value);
681
+ if (isNaN(date.getTime())) {
682
+ throw new Error("invalid date");
683
+ }
684
+
685
+ try {
686
+ locale = getLocaleOfDocument();
687
+ return date.toLocaleDateString(locale);
688
+
689
+ } catch (e) {
690
+ throw new Error("unsupported locale or missing format (" + e.message + ")");
691
+ }
692
+
601
693
 
602
694
  case "year":
603
695
  date = new Date(value);
@@ -658,20 +750,6 @@ function transform(value) {
658
750
 
659
751
  return date.getSeconds();
660
752
 
661
- case "date":
662
- date = new Date(value);
663
- if (isNaN(date.getTime())) {
664
- throw new Error("invalid date");
665
- }
666
-
667
- try {
668
- locale = getLocaleOfDocument();
669
- return date.toLocaleDateString(locale);
670
-
671
- } catch (e) {
672
- throw new Error("unsupported locale or missing format (" + e.message + ")");
673
- }
674
-
675
753
  case "i18n":
676
754
  case "translation":
677
755
  translations = getDocumentTranslations();
@@ -142,7 +142,7 @@ function getMonsterVersion() {
142
142
  }
143
143
 
144
144
  /** don't touch, replaced by make with package.json version */
145
- monsterVersion = new Version("3.18.0");
145
+ monsterVersion = new Version("3.20.0");
146
146
 
147
147
  return monsterVersion;
148
148
  }
@@ -28,6 +28,30 @@ describe('Transformer', function () {
28
28
  describe('Transformer.run()', function () {
29
29
 
30
30
  [
31
+ ['equals:a', "a", true],
32
+ ['equals:a', "b", false],
33
+ ['equals:3', 3, true],
34
+ ['equals:3', 6, false],
35
+ ['equals:on', true, true],
36
+ ['equals:true', true, true],
37
+ ['equals:on', false, false],
38
+ ['equals:true', false, false],
39
+ ['equals:off', false, true],
40
+ ['equals:false', false, true],
41
+ ['equals:off', false, true],
42
+ ['equals:false', true, false],
43
+ ['equals:3', 3, true],
44
+ ['equals:3', 6, false],
45
+ ['equals:undefined', undefined, true],
46
+ ['equals:undefined', 3, false],
47
+ ['equals:null', null, true],
48
+ ['equals:null', 3, false],
49
+ ['equals:3', 6, false],
50
+ ['currency', "EUR0", "0,00 €"],
51
+ ['currency:1:2', "EUR14.25", "14,2 €"],
52
+ ['currency', "EUR14.25", "14,25 €"],
53
+ ['datetime', "2023-02-14 14:12:10", "14.2.2023, 14:12:10"],
54
+ ['datetime', "2023-02-14 08:02:01", "14.2.2023, 08:02:01"],
31
55
  ['has-entries', {}, false],
32
56
  ['has-entries', {a:4}, true],
33
57
  ['has-entries', [], false],
@@ -7,7 +7,7 @@ describe('Monster', function () {
7
7
  let monsterVersion
8
8
 
9
9
  /** don´t touch, replaced by make with package.json version */
10
- monsterVersion = new Version("3.18.0")
10
+ monsterVersion = new Version("3.20.0")
11
11
 
12
12
  let m = getMonsterVersion();
13
13