@schukai/monster 4.38.8 → 4.38.10

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.
@@ -23,24 +23,24 @@ import { languages } from "./map/languages.mjs";
23
23
  * @return {number} The normalized number as a floating-point value. Returns NaN if the input is not a parsable number.
24
24
  */
25
25
  export function normalizeNumber(input, locale = navigator.language) {
26
- if (input === null || input === undefined) return NaN;
27
- if (typeof input === "number") return input; // If input is already a number, return it directly
28
- if (typeof input !== "string") return NaN;
29
-
30
- const cleaned = input.trim().replace(/\s/g, "");
31
- const decimalSeparator = getDecimalSeparator(locale);
32
- let normalized = cleaned;
33
-
34
- if (decimalSeparator === "," && cleaned.includes(".")) {
35
- normalized = cleaned.replace(/\./g, "").replace(",", ".");
36
- } else if (decimalSeparator === "." && cleaned.includes(",")) {
37
- normalized = cleaned.replace(/,/g, "").replace(".", ".");
38
- } else if (decimalSeparator === "," && cleaned.includes(",")) {
39
- normalized = cleaned.replace(",", ".");
40
- }
41
-
42
- const result = parseFloat(normalized);
43
- return Number.isNaN(result) ? NaN : result;
26
+ if (input === null || input === undefined) return NaN;
27
+ if (typeof input === "number") return input; // If input is already a number, return it directly
28
+ if (typeof input !== "string") return NaN;
29
+
30
+ const cleaned = input.trim().replace(/\s/g, "");
31
+ const decimalSeparator = getDecimalSeparator(locale);
32
+ let normalized = cleaned;
33
+
34
+ if (decimalSeparator === "," && cleaned.includes(".")) {
35
+ normalized = cleaned.replace(/\./g, "").replace(",", ".");
36
+ } else if (decimalSeparator === "." && cleaned.includes(",")) {
37
+ normalized = cleaned.replace(/,/g, "").replace(".", ".");
38
+ } else if (decimalSeparator === "," && cleaned.includes(",")) {
39
+ normalized = cleaned.replace(",", ".");
40
+ }
41
+
42
+ const result = parseFloat(normalized);
43
+ return Number.isNaN(result) ? NaN : result;
44
44
  }
45
45
 
46
46
  /**
@@ -50,9 +50,9 @@ export function normalizeNumber(input, locale = navigator.language) {
50
50
  * @return {string} The decimal separator used in the specified locale.
51
51
  */
52
52
  function getDecimalSeparator(locale = navigator.language) {
53
- const numberWithDecimal = 1.1;
54
- const formatted = new Intl.NumberFormat(locale).format(numberWithDecimal);
55
- return formatted.replace(/\d/g, "")[0]; // z.B. "," oder "."
53
+ const numberWithDecimal = 1.1;
54
+ const formatted = new Intl.NumberFormat(locale).format(numberWithDecimal);
55
+ return formatted.replace(/\d/g, "")[0]; // z.B. "," oder "."
56
56
  }
57
57
 
58
58
  /**
@@ -64,125 +64,125 @@ function getDecimalSeparator(locale = navigator.language) {
64
64
  * @return {Object} An object containing information about the detected language, preferred language, and available languages.
65
65
  */
66
66
  export function detectUserLanguagePreference() {
67
- const currentLang = document.documentElement.lang;
68
-
69
- let preferredLanguages = [];
70
-
71
- if (typeof navigator.language === "string" && navigator.language.length > 0) {
72
- preferredLanguages = [navigator.language];
73
- }
74
-
75
- if (Array.isArray(navigator.languages) && navigator.languages.length > 0) {
76
- preferredLanguages = navigator.languages;
77
- }
78
-
79
- // add to preferredLanguages all the base languages of the preferred languages
80
- preferredLanguages = preferredLanguages.concat(
81
- preferredLanguages.map((lang) => lang.split("-")[0]),
82
- );
83
-
84
- if (!currentLang && preferredLanguages.length === 0) {
85
- return {
86
- message: "No language information available.",
87
- };
88
- }
89
-
90
- const linkTags = document.querySelectorAll("link[hreflang]");
91
- if (linkTags.length === 0) {
92
- return {
93
- current: currentLang || null,
94
- message: "No <link> tags with hreflang available.",
95
- };
96
- }
97
-
98
- const availableLanguages = [...linkTags].map((link) => {
99
- const fullLang = link.hreflang;
100
- const baseLang = fullLang.split("-")[0];
101
- let label = link.getAttribute("data-monster-label");
102
- if (!label) {
103
- label = link.getAttribute("title");
104
- if (!label) {
105
- label = languages?.[fullLang];
106
- if (!label) {
107
- label = languages?.[baseLang];
108
- }
109
- }
110
- }
111
-
112
- return {
113
- fullLang,
114
- baseLang,
115
- label,
116
- href: link.href,
117
- };
118
- });
119
-
120
- // filter availableLanguages to only include languages that are in the preferredLanguages array
121
- const offerableLanguages = availableLanguages.filter(
122
- (lang) =>
123
- preferredLanguages.includes(lang.fullLang) ||
124
- preferredLanguages.includes(lang.baseLang),
125
- );
126
-
127
- if (offerableLanguages.length === 0) {
128
- return {
129
- current: currentLang || null,
130
- message: "No available languages match the user's preferences.",
131
- available: availableLanguages.map((lang) => ({
132
- ...lang,
133
- weight: 1,
134
- })),
135
- };
136
- }
137
-
138
- // Helper function to determine the "weight" of a language match
139
- function getWeight(langEntry) {
140
- // Full match has priority 3
141
- if (preferredLanguages.includes(langEntry.fullLang)) return 3;
142
- // Base language match has priority 2
143
- if (preferredLanguages.includes(langEntry.baseLang)) return 2;
144
- // No match is priority 1
145
- return 1;
146
- }
147
-
148
- // Sort the available languages by descending weight
149
- offerableLanguages.sort((a, b) => getWeight(b) - getWeight(a));
150
-
151
- // The best match is the first in the sorted list
152
- const bestMatch = offerableLanguages[0];
153
- const bestMatchWeight = getWeight(bestMatch);
154
-
155
- const currentLabel = languages?.[currentLang] || currentLang;
156
-
157
- // If we found a language that matches user preferences (weight > 1)
158
- if (bestMatchWeight > 0) {
159
- return {
160
- current: currentLang || null,
161
- currentLabel: currentLabel,
162
- preferred: {
163
- full: bestMatch.fullLang,
164
- base: bestMatch.baseLang,
165
- label: bestMatch.label,
166
- href: bestMatch.href,
167
- },
168
- available: availableLanguages.map((lang) => ({
169
- ...lang,
170
- weight: getWeight(lang),
171
- })),
172
- offerable: offerableLanguages.map((lang) => ({
173
- ...lang,
174
- weight: getWeight(lang),
175
- })),
176
- };
177
- }
178
-
179
- // If no language matched the user's preferences
180
- return {
181
- current: currentLang || null,
182
- message: "None of the preferred languages are available.",
183
- available: availableLanguages.map((lang) => ({
184
- ...lang,
185
- weight: getWeight(lang),
186
- })),
187
- };
67
+ const currentLang = document.documentElement.lang;
68
+
69
+ let preferredLanguages = [];
70
+
71
+ if (typeof navigator.language === "string" && navigator.language.length > 0) {
72
+ preferredLanguages = [navigator.language];
73
+ }
74
+
75
+ if (Array.isArray(navigator.languages) && navigator.languages.length > 0) {
76
+ preferredLanguages = navigator.languages;
77
+ }
78
+
79
+ // add to preferredLanguages all the base languages of the preferred languages
80
+ preferredLanguages = preferredLanguages.concat(
81
+ preferredLanguages.map((lang) => lang.split("-")[0]),
82
+ );
83
+
84
+ if (!currentLang && preferredLanguages.length === 0) {
85
+ return {
86
+ message: "No language information available.",
87
+ };
88
+ }
89
+
90
+ const linkTags = document.querySelectorAll("link[hreflang]");
91
+ if (linkTags.length === 0) {
92
+ return {
93
+ current: currentLang || null,
94
+ message: "No <link> tags with hreflang available.",
95
+ };
96
+ }
97
+
98
+ const availableLanguages = [...linkTags].map((link) => {
99
+ const fullLang = link.hreflang;
100
+ const baseLang = fullLang.split("-")[0];
101
+ let label = link.getAttribute("data-monster-label");
102
+ if (!label) {
103
+ label = link.getAttribute("title");
104
+ if (!label) {
105
+ label = languages?.[fullLang];
106
+ if (!label) {
107
+ label = languages?.[baseLang];
108
+ }
109
+ }
110
+ }
111
+
112
+ return {
113
+ fullLang,
114
+ baseLang,
115
+ label,
116
+ href: link.href,
117
+ };
118
+ });
119
+
120
+ // filter availableLanguages to only include languages that are in the preferredLanguages array
121
+ const offerableLanguages = availableLanguages.filter(
122
+ (lang) =>
123
+ preferredLanguages.includes(lang.fullLang) ||
124
+ preferredLanguages.includes(lang.baseLang),
125
+ );
126
+
127
+ if (offerableLanguages.length === 0) {
128
+ return {
129
+ current: currentLang || null,
130
+ message: "No available languages match the user's preferences.",
131
+ available: availableLanguages.map((lang) => ({
132
+ ...lang,
133
+ weight: 1,
134
+ })),
135
+ };
136
+ }
137
+
138
+ // Helper function to determine the "weight" of a language match
139
+ function getWeight(langEntry) {
140
+ // Full match has priority 3
141
+ if (preferredLanguages.includes(langEntry.fullLang)) return 3;
142
+ // Base language match has priority 2
143
+ if (preferredLanguages.includes(langEntry.baseLang)) return 2;
144
+ // No match is priority 1
145
+ return 1;
146
+ }
147
+
148
+ // Sort the available languages by descending weight
149
+ offerableLanguages.sort((a, b) => getWeight(b) - getWeight(a));
150
+
151
+ // The best match is the first in the sorted list
152
+ const bestMatch = offerableLanguages[0];
153
+ const bestMatchWeight = getWeight(bestMatch);
154
+
155
+ const currentLabel = languages?.[currentLang] || currentLang;
156
+
157
+ // If we found a language that matches user preferences (weight > 1)
158
+ if (bestMatchWeight > 0) {
159
+ return {
160
+ current: currentLang || null,
161
+ currentLabel: currentLabel,
162
+ preferred: {
163
+ full: bestMatch.fullLang,
164
+ base: bestMatch.baseLang,
165
+ label: bestMatch.label,
166
+ href: bestMatch.href,
167
+ },
168
+ available: availableLanguages.map((lang) => ({
169
+ ...lang,
170
+ weight: getWeight(lang),
171
+ })),
172
+ offerable: offerableLanguages.map((lang) => ({
173
+ ...lang,
174
+ weight: getWeight(lang),
175
+ })),
176
+ };
177
+ }
178
+
179
+ // If no language matched the user's preferences
180
+ return {
181
+ current: currentLang || null,
182
+ message: "None of the preferred languages are available.",
183
+ available: availableLanguages.map((lang) => ({
184
+ ...lang,
185
+ weight: getWeight(lang),
186
+ })),
187
+ };
188
188
  }
@@ -156,7 +156,7 @@ function getMonsterVersion() {
156
156
  }
157
157
 
158
158
  /** don't touch, replaced by make with package.json version */
159
- monsterVersion = new Version("4.38.7");
159
+ monsterVersion = new Version("4.38.8");
160
160
 
161
161
  return monsterVersion;
162
162
  }
@@ -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("4.38.7")
10
+ monsterVersion = new Version("4.38.8")
11
11
 
12
12
  let m = getMonsterVersion();
13
13
 
@@ -9,8 +9,8 @@
9
9
  </head>
10
10
  <body>
11
11
  <div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
12
- <h1 style='margin-bottom: 0.1em;'>Monster 4.38.7</h1>
13
- <div id="lastupdate" style='font-size:0.7em'>last update Mi 13. Aug 11:58:13 CEST 2025</div>
12
+ <h1 style='margin-bottom: 0.1em;'>Monster 4.38.8</h1>
13
+ <div id="lastupdate" style='font-size:0.7em'>last update Mi 27. Aug 12:13:30 CEST 2025</div>
14
14
  </div>
15
15
  <div id="mocha-errors"
16
16
  style="color: red;font-weight: bold;display: flex;align-items: center;justify-content: center;flex-direction: column;margin:20px;"></div>