@sankhyalabs/core 6.1.0-dev.3 → 6.1.0-dev.5

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.
@@ -70,8 +70,8 @@ export class DataUnitInMemoryLoader {
70
70
 
71
71
  if (!this._dataUnit) return;
72
72
 
73
- //Isso força o refresh internamente no datunit
74
- this._dataUnit.loadData();
73
+ //Isso força o refresh internamente no datunit mantendo a seleção anterior
74
+ this._dataUnit.loadDataWithParams({keepSelection: true});
75
75
  }
76
76
 
77
77
  public static getConvertedValue(descriptor: FieldDescriptor, strValue: string, dateFormat?: RECORD_DATE_FORMAT) {
package/src/index.ts CHANGED
@@ -53,6 +53,7 @@ import { DataUnitLoaderUtils, PaginationInfoBuilderParams } from "./dataunit/loa
53
53
  import { ColumnFilterManager } from "./utils/ColumnFilterManager.js";
54
54
  import { DataUnitInMemoryLoaderConfig, RECORD_DATE_FORMAT } from "./dataunit/loader/DataUnitInMemoryLoaderConfig.js";
55
55
  import Base64Utils from "./utils/Base64Utils.js";
56
+ import { LangUtils} from './utils/LangUtils.js';
56
57
 
57
58
  /*Classes públicas no pacote*/
58
59
  export {
@@ -139,5 +140,5 @@ export {
139
140
  DataUnitInMemoryLoaderConfig,
140
141
  RECORD_DATE_FORMAT,
141
142
  Base64Utils,
142
-
143
+ LangUtils,
143
144
  };
@@ -0,0 +1,129 @@
1
+
2
+ /**
3
+ * A classe `LangUtils` fornece métodos utilitários para gerenciar configurações de
4
+ * idioma, incluindo recuperar, definir e aplicar preferências de idioma.
5
+ */
6
+ export class LangUtils {
7
+ /**
8
+ * Classe estática contendo os códigos de idioma predefinidos.
9
+ *
10
+ * @property PT_BR - Português (Brasil)
11
+ * @property EN_US - Inglês (Estados Unidos)
12
+ * @property ES_ES - Espanhol (Espanha)
13
+ */
14
+ public static ELanguages = class {
15
+ static PT_BR = "pt_BR";
16
+ static EN_US = "en_US";
17
+ static ES_ES = "es_ES";
18
+ };
19
+
20
+ /**
21
+ * Converte o idioma fornecido para um dos códigos predefinidos.
22
+ *
23
+ * @param lang - O idioma a ser convertido (ex.: "en", "es", "pt-BR").
24
+ * @returns O código de idioma correspondente (ex.: "en_US", "es_ES").
25
+ *
26
+ * @example
27
+ * LangUtils.convertLanguage("en"); // Retorna "en_US"
28
+ * LangUtils.convertLanguage("es-es"); // Retorna "es_ES"
29
+ */
30
+ private static convertLanguage(lang: string): string {
31
+ switch (lang.toLocaleLowerCase()) {
32
+ case "en":
33
+ case "en_us":
34
+ case "en-us":
35
+ return LangUtils.ELanguages.EN_US;
36
+ case "es":
37
+ case "es_es":
38
+ case "es-es":
39
+ return LangUtils.ELanguages.ES_ES;
40
+ default:
41
+ return LangUtils.ELanguages.PT_BR;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Captura o idioma atualmente definido. Primeiro verifica se há um idioma
47
+ * armazenado nos cookies. Caso contrário, utiliza o idioma definido no elemento
48
+ * `<html>`. Se nenhum valor for encontrado nos cookies ou no elemento `<html>`,
49
+ * é utilizada a configuração do navegador. Caso o navegador também não forneça
50
+ * um idioma, o padrão será `"pt_BR"`.
51
+ *
52
+ * @returns O idioma definido (ex.: "pt_BR", "en_US").
53
+ *
54
+ * @example
55
+ * LangUtils.getLanguage(); // Retorna o idioma atual
56
+ */
57
+ public static getLanguage(): string {
58
+ const cookieLanguage = this.getLanguageFromCookie();
59
+
60
+ if (cookieLanguage) {
61
+ return this.convertLanguage(cookieLanguage);
62
+ }
63
+
64
+ return this.getHtmlLanguage();
65
+ }
66
+
67
+ /**
68
+ * Recupera o valor do atributo `lang` do elemento `<html>`. Se o ambiente
69
+ * estiver em modo de desenvolvimento, o idioma padrão será `"pt_BR"`. Caso
70
+ * contrário, utiliza o idioma do navegador ou `"pt-BR"` como padrão.
71
+ *
72
+ * @returns O idioma definido no elemento `<html>` ou o padrão.
73
+ *
74
+ * @example
75
+ * LangUtils.getHtmlLanguage(); // Retorna "pt_BR" no modo desenvolvimento
76
+ */
77
+ public static getHtmlLanguage(): string {
78
+ if (process.env.NODE_ENV === 'development') {
79
+ return LangUtils.ELanguages.PT_BR;
80
+ }
81
+
82
+ const lang = document.documentElement.lang || navigator.language || "pt-BR";
83
+
84
+ return this.convertLanguage(lang);
85
+ }
86
+
87
+ /**
88
+ * Define o atributo `lang` no elemento `<html>` e atualiza o cookie de idioma.
89
+ *
90
+ * @param language - O idioma a ser definido (ex.: "pt_BR", "en_US").
91
+ *
92
+ * > **Nota:** Definir o idioma no elemento `<html>` é essencial para garantir que o conteúdo
93
+ * da página seja interpretado corretamente por navegadores, leitores de tela e mecanismos de
94
+ * busca, melhorando a acessibilidade e a indexação.
95
+ *
96
+ * @example
97
+ * LangUtils.setHtmlLanguage("en_US"); // Define o idioma como "en_US"
98
+ */
99
+ public static setHtmlLanguage(language: string): void {
100
+ document.documentElement.lang = language;
101
+ document.cookie = `lang=${language}; path=/;`;
102
+ }
103
+
104
+ /**
105
+ * Recupera o valor do cookie de idioma.
106
+ *
107
+ * @returns O valor do cookie de idioma ou `null` se não existir.
108
+ *
109
+ * @example
110
+ * LangUtils.getLanguageFromCookie(); // Retorna "pt_BR" se o cookie existir
111
+ */
112
+ public static getLanguageFromCookie(): string | null {
113
+ const match = document.cookie.match(new RegExp('(^| )lang=([^;]+)'));
114
+ return match ? match[2] : null;
115
+ }
116
+
117
+ /**
118
+ * Define o atributo `lang` no elemento `<html>` com base no cookie de idioma, se existir.
119
+ *
120
+ * @example
121
+ * LangUtils.applyLanguageFromCookie(); // Aplica o idioma do cookie ao elemento `<html>`
122
+ */
123
+ public static applyLanguageFromCookie(): void {
124
+ const langFromCookie = this.getLanguageFromCookie();
125
+ if (langFromCookie) {
126
+ this.setHtmlLanguage(langFromCookie);
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,117 @@
1
+ import { LangUtils } from "../../src/utils/LangUtils";
2
+
3
+ describe("LangUtils", () => {
4
+ const defaultLangNavigator = "en_US";
5
+
6
+ function deleteAllCookies() {
7
+ document.cookie.split(';').forEach(cookie => {
8
+ const eqPos = cookie.indexOf('=');
9
+ const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie;
10
+ document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
11
+ });
12
+ }
13
+
14
+ function mockNavigatorLanguage(language: string) {
15
+ Object.defineProperty(navigator, "language", {
16
+ value: language,
17
+ configurable: true,
18
+ });
19
+ }
20
+
21
+ beforeEach(() => {
22
+ deleteAllCookies();
23
+ document.documentElement.lang = "";
24
+ mockNavigatorLanguage(defaultLangNavigator);
25
+ });
26
+
27
+ describe("convertLanguage", () => {
28
+ it("should convert 'en' to EN_US", () => {
29
+ const result = (LangUtils as any).convertLanguage("en");
30
+ expect(result).toBe(LangUtils.ELanguages.EN_US);
31
+ });
32
+
33
+ it("should convert 'es' to ES_ES", () => {
34
+ const result = (LangUtils as any).convertLanguage("es");
35
+ expect(result).toBe(LangUtils.ELanguages.ES_ES);
36
+ });
37
+
38
+ it("should default to PT_BR for unknown languages", () => {
39
+ const result = (LangUtils as any).convertLanguage("fr");
40
+ expect(result).toBe(LangUtils.ELanguages.PT_BR);
41
+ });
42
+ });
43
+
44
+ describe("getLanguage", () => {
45
+ it("should return language from cookie if available", () => {
46
+ document.cookie = "lang=en_US";
47
+ const result = LangUtils.getLanguage();
48
+ expect(result).toBe(LangUtils.ELanguages.EN_US);
49
+ });
50
+
51
+ it("should return HTML language if no cookie is set", () => {
52
+ document.documentElement.lang = "es";
53
+ const result = LangUtils.getLanguage();
54
+ expect(result).toBe(LangUtils.ELanguages.ES_ES);
55
+ });
56
+
57
+ it("should default to settings navigator if no cookie or HTML language is set", () => {
58
+ const result = LangUtils.getLanguage();
59
+ expect(result).toBe(defaultLangNavigator);
60
+ });
61
+
62
+ it("should default to PT_BR if no cookie or HTML language or config navigator is set", () => {
63
+ mockNavigatorLanguage('');
64
+ const result = LangUtils.getLanguage();
65
+ expect(result).toBe(LangUtils.ELanguages.PT_BR);
66
+ });
67
+ });
68
+
69
+ describe("getHtmlLanguage", () => {
70
+ it("should return PT_BR in development mode", () => {
71
+ process.env.NODE_ENV = "development";
72
+ const result = LangUtils.getHtmlLanguage();
73
+ expect(result).toBe(LangUtils.ELanguages.PT_BR);
74
+ });
75
+
76
+ it("should return converted HTML language in production mode", () => {
77
+ process.env.NODE_ENV = "production";
78
+ document.documentElement.lang = "en";
79
+ const result = LangUtils.getHtmlLanguage();
80
+ expect(result).toBe(LangUtils.ELanguages.EN_US);
81
+ });
82
+ });
83
+
84
+ describe("setHtmlLanguage", () => {
85
+ it("should set the HTML lang attribute and update the cookie", () => {
86
+ LangUtils.setHtmlLanguage(LangUtils.ELanguages.ES_ES);
87
+ expect(document.documentElement.lang).toBe(LangUtils.ELanguages.ES_ES);
88
+ expect(document.cookie).toContain("lang=es_ES");
89
+ });
90
+ });
91
+
92
+ describe("getLanguageFromCookie", () => {
93
+ it("should return the language from the cookie if it exists", () => {
94
+ document.cookie = "lang=en_US";
95
+ const result = LangUtils.getLanguageFromCookie();
96
+ expect(result).toBe("en_US");
97
+ });
98
+
99
+ it("should return null if the cookie does not exist", () => {
100
+ const result = LangUtils.getLanguageFromCookie();
101
+ expect(result).toBeNull();
102
+ });
103
+ });
104
+
105
+ describe("applyLanguageFromCookie", () => {
106
+ it("should set the HTML lang attribute based on the cookie", () => {
107
+ document.cookie = "lang=es_ES";
108
+ LangUtils.applyLanguageFromCookie();
109
+ expect(document.documentElement.lang).toBe(LangUtils.ELanguages.ES_ES);
110
+ });
111
+
112
+ it("should do nothing if the cookie does not exist", () => {
113
+ LangUtils.applyLanguageFromCookie();
114
+ expect(document.documentElement.lang).toBe("");
115
+ });
116
+ });
117
+ });