@karpeleslab/klbfw 0.1.13 → 0.2.1

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.
package/util.js CHANGED
@@ -1,51 +1,88 @@
1
1
  'use strict';
2
+ /**
3
+ * @fileoverview Utility functions for KLB Frontend Framework
4
+ *
5
+ * This module provides utility functions for internationalization
6
+ * and URL prefix handling.
7
+ */
8
+
2
9
  const internalFW = require('./fw-wrapper');
3
10
 
4
- function getI18N(language) {
11
+ /**
12
+ * Fetches internationalization data for the specified language
13
+ * @param {string} language - Language code (e.g., 'en-US')
14
+ * @returns {Promise<Object>} Promise resolving to internationalization data
15
+ */
16
+ const getI18N = (language) => {
17
+ // Use language from parameters or get from framework
5
18
  language = language || internalFW.getLocale();
6
19
 
20
+ // Handle platform-specific i18n implementations
7
21
  if (typeof __platformAsyncI18N !== "undefined") {
8
- // new SSR mode
22
+ // New SSR mode
9
23
  return __platformAsyncI18N(language);
10
24
  }
25
+
11
26
  if (typeof __platformGetI18N !== "undefined") {
12
- // we are in SSR mode
13
- return new Promise(function (resolve, reject) {
14
- resolve(__platformGetI18N(language));
15
- });
27
+ // Legacy SSR mode
28
+ return Promise.resolve(__platformGetI18N(language));
16
29
  }
17
30
 
18
- // use fetch()
19
- // /_special/locale/en-US.json
20
- return new Promise(function (resolve, reject) {
21
- // a simple GET is straightforward
31
+ // Use fetch in browser environment
32
+ return new Promise((resolve, reject) => {
22
33
  fetch("/_special/locale/" + language + ".json")
23
- .then(function (res) {
24
- res.json().then(resolve, reject);
25
- }, reject);
34
+ .then(res => {
35
+ if (!res.ok) {
36
+ reject({
37
+ message: `HTTP Error: ${res.status} ${res.statusText}`,
38
+ status: res.status
39
+ });
40
+ return;
41
+ }
42
+
43
+ res.json()
44
+ .then(resolve)
45
+ .catch(error => {
46
+ reject(error || new Error('Failed to parse JSON response'));
47
+ });
48
+ })
49
+ .catch(error => {
50
+ reject(error || new Error('Failed to fetch locale data'));
51
+ });
26
52
  });
27
- }
53
+ };
28
54
 
29
- function trimPrefix(url) {
55
+ /**
56
+ * Extracts prefixes from a URL path
57
+ * @param {string} url - URL path to process
58
+ * @returns {Array} Array containing [prefixObject, remainingPath]
59
+ */
60
+ const trimPrefix = (url) => {
30
61
  let currentPrefix = '';
31
62
  let currentText = '';
32
63
  const prefix = {};
33
64
 
34
65
  for (let i = 0; i < url.length; i++) {
35
66
  const currentChar = url[i];
67
+
68
+ // Skip consecutive slashes
36
69
  if (currentChar === '/' && !currentText) continue;
37
70
 
38
- if (!currentPrefix && currentText.length > 1) { // We are past the prefix
71
+ // If we have text and not in a prefix, we're done with prefixes
72
+ if (!currentPrefix && currentText.length > 1) {
39
73
  currentText = currentText + url.substr(i);
40
74
  break;
41
75
  }
42
76
 
77
+ // Handle slash after text
43
78
  if (currentChar === '/' && currentText) {
44
79
  if (currentText.length === 1) {
80
+ // This is a prefix indicator (e.g., /l/ for language)
45
81
  currentPrefix = currentText;
46
82
  currentText = '';
47
83
  continue;
48
84
  } else {
85
+ // This is a prefix value (e.g., /l/en-US/)
49
86
  prefix[currentPrefix] = currentText;
50
87
  currentPrefix = '';
51
88
  currentText = '';
@@ -53,12 +90,13 @@ function trimPrefix(url) {
53
90
  }
54
91
  }
55
92
 
56
- currentText += currentChar
93
+ // Add character to current text
94
+ currentText += currentChar;
57
95
  }
58
96
 
59
- return [prefix, '/' + currentText]
60
- }
61
-
97
+ return [prefix, '/' + currentText];
98
+ };
62
99
 
100
+ // Export functions
63
101
  module.exports.getI18N = getI18N;
64
- module.exports.trimPrefix = trimPrefix;
102
+ module.exports.trimPrefix = trimPrefix;