@mgdis/core-ui-helpers 1.2.1 → 1.3.0

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.
Files changed (143) hide show
  1. package/dist/angular/index.cjs +1 -1
  2. package/dist/angular/index.cjs.map +1 -1
  3. package/dist/angular/index.js +1 -1
  4. package/dist/angular/index.js.map +1 -1
  5. package/dist/angular/index.umd.js +1 -1
  6. package/dist/angular/index.umd.js.map +1 -1
  7. package/dist/locale/index.cjs +3 -1
  8. package/dist/locale/index.cjs.map +1 -1
  9. package/dist/locale/index.js +3 -1
  10. package/dist/locale/index.js.map +1 -1
  11. package/dist/locale/index.umd.js +3 -1
  12. package/dist/locale/index.umd.js.map +1 -1
  13. package/dist/playwright/index.cjs +12 -5
  14. package/dist/playwright/index.cjs.map +1 -1
  15. package/dist/playwright/index.js +12 -5
  16. package/dist/playwright/index.js.map +1 -1
  17. package/dist/playwright/index.umd.js +12 -5
  18. package/dist/playwright/index.umd.js.map +1 -1
  19. package/dist/stencil/index.cjs +33 -33
  20. package/dist/stencil/index.cjs.map +1 -1
  21. package/dist/stencil/index.js +33 -33
  22. package/dist/stencil/index.js.map +1 -1
  23. package/dist/stencil/index.umd.js +33 -33
  24. package/dist/stencil/index.umd.js.map +1 -1
  25. package/dist/storybook/index.cjs +9 -7
  26. package/dist/storybook/index.cjs.map +1 -1
  27. package/dist/storybook/index.js +9 -7
  28. package/dist/storybook/index.js.map +1 -1
  29. package/dist/storybook/index.umd.js +9 -7
  30. package/dist/storybook/index.umd.js.map +1 -1
  31. package/dist/tests/index.cjs +24 -9
  32. package/dist/tests/index.cjs.map +1 -1
  33. package/dist/tests/index.d.ts +2 -2
  34. package/dist/tests/index.js +24 -9
  35. package/dist/tests/index.js.map +1 -1
  36. package/dist/tests/index.umd.js +24 -9
  37. package/dist/tests/index.umd.js.map +1 -1
  38. package/dist/utils/index.cjs +14 -6
  39. package/dist/utils/index.cjs.map +1 -1
  40. package/dist/utils/index.js +14 -6
  41. package/dist/utils/index.js.map +1 -1
  42. package/dist/utils/index.umd.js +14 -6
  43. package/dist/utils/index.umd.js.map +1 -1
  44. package/dist/vue/index.cjs +2 -1
  45. package/dist/vue/index.cjs.map +1 -1
  46. package/dist/vue/index.js +2 -1
  47. package/dist/vue/index.js.map +1 -1
  48. package/dist/vue/index.umd.js +2 -1
  49. package/dist/vue/index.umd.js.map +1 -1
  50. package/package.json +11 -8
  51. package/.turbo/turbo-build.log +0 -49
  52. package/CHANGELOG.md +0 -95
  53. package/json-doc.test.json +0 -264
  54. package/rollup.config.mjs +0 -61
  55. package/src/angular/index.spec.ts +0 -8
  56. package/src/angular/index.ts +0 -1
  57. package/src/angular/mg-angular-logger/index.ts +0 -1
  58. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +0 -3
  59. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +0 -40
  60. package/src/angular/mg-angular-logger/mg-angular-logger.ts +0 -23
  61. package/src/locale/index.spec.ts +0 -15
  62. package/src/locale/index.ts +0 -1
  63. package/src/locale/locale.spec.ts +0 -266
  64. package/src/locale/locale.ts +0 -193
  65. package/src/playwright/config/index.ts +0 -43
  66. package/src/playwright/index.spec.ts +0 -16
  67. package/src/playwright/index.ts +0 -4
  68. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +0 -48
  69. package/src/playwright/utils/index.ts +0 -1
  70. package/src/playwright/utils/utils.spec.ts +0 -43
  71. package/src/playwright/utils/utils.ts +0 -69
  72. package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +0 -248
  73. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +0 -414
  74. package/src/stencil/ide/cem.spec.ts +0 -35
  75. package/src/stencil/ide/cem.ts +0 -96
  76. package/src/stencil/ide/ide.spec.ts +0 -29
  77. package/src/stencil/ide/ide.ts +0 -221
  78. package/src/stencil/ide/index.ts +0 -2
  79. package/src/stencil/index.spec.ts +0 -16
  80. package/src/stencil/index.ts +0 -2
  81. package/src/stencil/normalize-boolean-attributes/index.ts +0 -1
  82. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +0 -63
  83. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +0 -40
  84. package/src/storybook/index.spec.ts +0 -19
  85. package/src/storybook/index.ts +0 -1
  86. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +0 -329
  87. package/src/storybook/stencil/index.ts +0 -1
  88. package/src/storybook/stencil/stencil-shim.d.ts +0 -1
  89. package/src/storybook/stencil/stencil.conf.ts +0 -4
  90. package/src/storybook/stencil/stencil.spec.ts +0 -146
  91. package/src/storybook/stencil/stencil.ts +0 -366
  92. package/src/tests/index.spec.ts +0 -14
  93. package/src/tests/index.ts +0 -1
  94. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +0 -52
  95. package/src/tests/unit/index.ts +0 -1
  96. package/src/tests/unit/unit.conf.ts +0 -9
  97. package/src/tests/unit/unit.spec.ts +0 -164
  98. package/src/tests/unit/unit.ts +0 -306
  99. package/src/utils/attributes/class-list/class-list.spec.ts +0 -36
  100. package/src/utils/attributes/class-list/class-list.ts +0 -51
  101. package/src/utils/attributes/class-list/index.ts +0 -1
  102. package/src/utils/attributes/id/id.spec.ts +0 -51
  103. package/src/utils/attributes/id/id.ts +0 -46
  104. package/src/utils/attributes/id/index.ts +0 -1
  105. package/src/utils/attributes/index.ts +0 -3
  106. package/src/utils/attributes/to-kebab-case/index.ts +0 -1
  107. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +0 -29
  108. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +0 -23
  109. package/src/utils/components/__snapshots__/components.spec.ts.snap +0 -313
  110. package/src/utils/components/components.conf.ts +0 -11
  111. package/src/utils/components/components.spec.ts +0 -192
  112. package/src/utils/components/components.ts +0 -138
  113. package/src/utils/components/index.ts +0 -2
  114. package/src/utils/date/date.spec.ts +0 -12
  115. package/src/utils/date/date.ts +0 -20
  116. package/src/utils/date/index.ts +0 -1
  117. package/src/utils/elements/elements.spec.ts +0 -17
  118. package/src/utils/elements/elements.ts +0 -16
  119. package/src/utils/elements/index.ts +0 -1
  120. package/src/utils/index.spec.ts +0 -84
  121. package/src/utils/index.ts +0 -8
  122. package/src/utils/number/index.ts +0 -1
  123. package/src/utils/number/number.spec.ts +0 -14
  124. package/src/utils/number/number.ts +0 -6
  125. package/src/utils/object/index.ts +0 -2
  126. package/src/utils/object/object.conf.ts +0 -4
  127. package/src/utils/object/object.spec.ts +0 -110
  128. package/src/utils/object/object.ts +0 -26
  129. package/src/utils/string/index.ts +0 -1
  130. package/src/utils/string/string.spec.ts +0 -47
  131. package/src/utils/string/string.ts +0 -38
  132. package/src/utils/windows/index.ts +0 -1
  133. package/src/utils/windows/windows.spec.ts +0 -65
  134. package/src/utils/windows/windows.ts +0 -50
  135. package/src/vue/index.spec.ts +0 -8
  136. package/src/vue/index.ts +0 -1
  137. package/src/vue/mg-model/index.ts +0 -1
  138. package/src/vue/mg-model/mg-model.conf.ts +0 -6
  139. package/src/vue/mg-model/mg-model.spec.ts +0 -50
  140. package/src/vue/mg-model/mg-model.ts +0 -50
  141. package/tsconfig.json +0 -17
  142. package/typedoc.json +0 -14
  143. package/vitest.config.mts +0 -13
@@ -1,266 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { localeCurrency, localeNumber, localeDate, localePercent, localeUnit, defineLocales, localeDatePattern, localeByte } from './locale';
3
-
4
- const messagesFr = { lang: 'fr' };
5
- const messagesEn = { lang: 'en' };
6
- const messages = { en: messagesEn, fr: messagesFr };
7
-
8
- describe('locale', () => {
9
- describe.each(['en', 'fr'])('locale: %s', locale => {
10
- describe('localeCurrency', () => {
11
- test('Should Format number to the locale currency', () => {
12
- const formatedCurrency = localeCurrency(1234567890.12, locale, 'EUR');
13
- expect(formatedCurrency).toEqual(locale === 'en' ? '€1,234,567,890.12' : '1 234 567 890,12\xa0€');
14
- });
15
- });
16
-
17
- describe('localeNumber', () => {
18
- test('Should Format number to the locale', () => {
19
- const formatedCurrency = localeNumber(1234567890.12, locale);
20
- expect(formatedCurrency).toEqual(locale === 'en' ? '1,234,567,890.12' : '1 234 567 890,12');
21
- });
22
- test('Should Format number to the locale and manage 00 in decimal returned value', () => {
23
- const formatedCurrency = localeNumber(1234567890, locale, 2);
24
- expect(formatedCurrency).toEqual(locale === 'en' ? '1,234,567,890.00' : '1 234 567 890,00');
25
- });
26
- test.each([0, 5])('Should Format number to the locale and format the decimal length, case length %s', length => {
27
- const formatedCurrency = localeNumber(1234567890, locale, length);
28
- const decimal = length > 0 ? '00000' : null;
29
- const localePoint = locale === 'en' ? '.' : ',';
30
- const integer = locale === 'en' ? '1,234,567,890' : '1 234 567 890';
31
- expect(formatedCurrency).toEqual(integer + (decimal ? localePoint + decimal : ''));
32
- });
33
- });
34
-
35
- describe('localeDatePattern', () => {
36
- test.each([
37
- { locale: 'fr', expected: 'dd/mm/yyyy' },
38
- { locale: 'en', expected: 'mm/dd/yyyy' },
39
- ])('Should get date pattern', ({ locale, expected }) => {
40
- const pattern = localeDatePattern(locale);
41
- expect(pattern).toEqual(expected);
42
- });
43
- });
44
-
45
- describe('localeDate', () => {
46
- test.each([undefined, '', 'blu'])('Should return empty string: %s', date => {
47
- const formatedDate = localeDate(date, locale);
48
- expect(formatedDate).toEqual('');
49
- });
50
-
51
- test('Should return formated date', () => {
52
- const formatedDate = localeDate('2022-06-02', locale);
53
- expect(formatedDate).toEqual(locale === 'en' ? '6/2/2022' : '02/06/2022');
54
- });
55
-
56
- test('Should return formated date with given timeZone config', () => {
57
- const formatedDate = localeDate('2022-06-02', locale, { timeZone: 'America/New_York' });
58
- expect(formatedDate).toEqual(locale === 'en' ? '6/1/2022' : '01/06/2022');
59
- });
60
- });
61
-
62
- describe('defineLocales', () => {
63
- test('Should return navigator locale', () => {
64
- const locales = defineLocales(messages, locale)(document.createElement('div'));
65
- expect(locales.locale).toEqual('en-US');
66
- expect(locales.messages).toMatchObject(messagesEn);
67
- });
68
-
69
- test('Should return default locale', () => {
70
- const navigatorBackup = window.navigator;
71
- globalThis.window.navigator = {} as Navigator;
72
- const locales = defineLocales(messages, locale)(document.createElement('div'));
73
- expect(locales.locale).toEqual(locale);
74
- expect(locales.messages).toMatchObject({ lang: locale });
75
-
76
- // reset default navigator default value
77
- globalThis.window.navigator = navigatorBackup;
78
- });
79
-
80
- test('Should return matching locale', () => {
81
- const div = document.createElement('div');
82
- div.lang = 'fr-FR';
83
- const locales = defineLocales(messages, locale)(div);
84
- expect(locales.locale).toEqual(div.lang);
85
- expect(locales.messages).toMatchObject(messagesFr);
86
- });
87
-
88
- test('Should return default locale messages when requested does not exist', () => {
89
- const div = document.createElement('div');
90
- div.lang = 'ca';
91
- const locales = defineLocales(messages, locale)(div);
92
- expect(locales.locale).toEqual(div.lang);
93
- expect(locales.messages).toMatchObject({ lang: locale });
94
- });
95
-
96
- test('Should handle empty messages object', () => {
97
- const div = document.createElement('div');
98
- div.lang = 'fr';
99
- const locales = defineLocales({}, 'en')(div);
100
- expect(locales.locale).toEqual('fr');
101
- expect(locales.messages).toMatchObject({ lang: 'en' });
102
- });
103
-
104
- test('Should return default object when messages and default locale are not found', () => {
105
- const div = document.createElement('div');
106
- div.lang = 'fr';
107
- const locales = defineLocales({ it: { lang: 'it' } }, 'es')(div);
108
- expect(locales.locale).toEqual('fr');
109
- expect(locales.messages).toMatchObject({ lang: 'es' });
110
- });
111
- });
112
-
113
- describe('localePercent', () => {
114
- test.each([
115
- [0.42, 'en', 2, '42.00%'],
116
- [0.42, 'fr', 2, '42,00 %'],
117
- [1.23, 'en', 1, '123.0%'],
118
- [1.23, 'fr', 1, '123,0 %'],
119
- [0, 'fr', 0, '0 %'],
120
- [0, 'en', 0, '0%'],
121
- [1, 'fr', 0, '100 %'],
122
- [1, 'en', 0, '100%'],
123
- [-0.5, 'fr', 2, '-50,00 %'],
124
- [-0.5, 'en', 2, '-50.00%'],
125
- [Number.POSITIVE_INFINITY, 'fr', 0, '∞ %'],
126
- [Number.POSITIVE_INFINITY, 'en', 0, '∞%'],
127
- [Number.NEGATIVE_INFINITY, 'fr', 0, '-∞ %'],
128
- [Number.NEGATIVE_INFINITY, 'en', 0, '-∞%'],
129
- [NaN, 'fr', 0, 'NaN %'],
130
- [NaN, 'en', 0, 'NaN%'],
131
- ])('Should format %s value correctly for locale %s with %s decimals', (value, locale, decimals, expected) => {
132
- const result = localePercent(value, locale, decimals);
133
- // Normalize spaces before comparison
134
- expect(result.replace(/\s/g, ' ')).toBe(expected.replace(/\s/g, ' '));
135
- });
136
-
137
- test('Should handle undefined decimal length', () => {
138
- expect(localePercent(0.42, 'fr').replace(/\s/g, ' ')).toBe('42 %'.replace(/\s/g, ' '));
139
- expect(localePercent(0.42, 'en').replace(/\s/g, ' ')).toBe('42%'.replace(/\s/g, ' '));
140
- });
141
- });
142
-
143
- describe('localeUnit', () => {
144
- test.each([
145
- [1234567890.12, 'fr', 'kilometer', 'short', 2, '1 234 567 890,12 km'],
146
- [23, 'fr', 'celsius', 'short', 0, '23 °C'],
147
- [10, 'fr', 'kilometer', 'long', 0, '10 kilomètres'],
148
- [1234567890.12, 'en', 'kilometer', 'short', 2, '1,234,567,890.12 km'],
149
- [23, 'en', 'celsius', 'short', 0, '23°C'],
150
- [10, 'en', 'kilometer', 'long', 0, '10 kilometers'],
151
- ])('Should format %s value correctly for locale %s with unit %s', (value, locale, unit, unitDisplay, decimals, expected) => {
152
- const result = localeUnit(value, locale, unit, unitDisplay as Intl.NumberFormatOptions['unitDisplay'], decimals);
153
- // Normalize spaces before comparison
154
- expect(result.replace(/\s/g, ' ')).toBe(expected.replace(/\s/g, ' '));
155
- });
156
-
157
- test('Should respect decimal length', () => {
158
- expect(localeUnit(1234.567, 'en', 'kilometer', 'short', 2).replace(/\s/g, ' ')).toBe('1,234.57 km'.replace(/\s/g, ' '));
159
- expect(localeUnit(1234.567, 'fr', 'kilometer', 'short', 2).replace(/\s/g, ' ')).toBe('1 234,57 km'.replace(/\s/g, ' '));
160
- });
161
-
162
- test('Should use default unitDisplay and decimalLength when not provided', () => {
163
- expect(localeUnit(1234, 'en', 'kilometer').replace(/\s/g, ' ')).toBe('1,234 km');
164
- expect(localeUnit(1234, 'fr', 'kilometer').replace(/\s/g, ' ')).toBe('1 234 km');
165
- });
166
-
167
- test('Should use default unitDisplay when only decimalLength is provided', () => {
168
- expect(localeUnit(1234.567, 'en', 'kilometer', undefined, 2).replace(/\s/g, ' ')).toBe('1,234.57 km');
169
- expect(localeUnit(1234.567, 'fr', 'kilometer', undefined, 2).replace(/\s/g, ' ')).toBe('1 234,57 km');
170
- });
171
- });
172
-
173
- describe('localeByte', () => {
174
- test.each([
175
- {
176
- octets: 0,
177
- expected: {
178
- fr: '0\u00A0octet',
179
- en: '0 bytes',
180
- },
181
- },
182
- {
183
- octets: 1,
184
- expected: {
185
- fr: '1\u00A0octet',
186
- en: '1 byte',
187
- },
188
- },
189
- {
190
- octets: 500,
191
- expected: {
192
- fr: '500\u00A0octets',
193
- en: '500 bytes',
194
- },
195
- },
196
- {
197
- octets: 1024,
198
- expected: {
199
- fr: '1\u202fko',
200
- en: '1 kB',
201
- },
202
- },
203
- {
204
- octets: 1536,
205
- expected: {
206
- fr: '1,5\u202fko',
207
- en: '1.5 kB',
208
- },
209
- },
210
- {
211
- octets: 1048576,
212
- expected: {
213
- fr: '1\u202fMo',
214
- en: '1 MB',
215
- },
216
- },
217
- {
218
- octets: 1572864,
219
- expected: {
220
- fr: '1,5\u202fMo',
221
- en: '1.5 MB',
222
- },
223
- },
224
- {
225
- octets: 1073741824,
226
- expected: {
227
- fr: '1\u202fGo',
228
- en: '1 GB',
229
- },
230
- },
231
- {
232
- octets: 1610612736,
233
- expected: {
234
- fr: '1,5\u202fGo',
235
- en: '1.5 GB',
236
- },
237
- },
238
- {
239
- octets: 1099511627776,
240
- expected: {
241
- fr: '1\u202fTo',
242
- en: '1 TB',
243
- },
244
- },
245
- {
246
- octets: 1649267441664,
247
- expected: {
248
- fr: '1,5\u202fTo',
249
- en: '1.5 TB',
250
- },
251
- },
252
- ])('Should format $octets octets to string', ({ octets, expected }) => {
253
- const res = localeByte(octets, locale);
254
- expect(res).toEqual(expected[locale as 'en' | 'fr']);
255
- });
256
-
257
- test('Should throw error with negative octets', () => {
258
- expect(() => localeByte(-10, locale)).toThrowError('localeByte - size must be a positive number.');
259
- });
260
-
261
- test.each([undefined, null, NaN, []])('Should throw error with %s octets', value => {
262
- expect(() => localeByte(value as number, locale)).toThrowError('localeByte - size must be a positive number.');
263
- });
264
- });
265
- });
266
- });
@@ -1,193 +0,0 @@
1
- import { dateRegExp } from '../utils/date';
2
- import type { ObjectType } from '../utils/object';
3
-
4
- /**
5
- * Gets the date pattern based on the specified locale.
6
- * @param locale - the locale to refer to
7
- * @returns date pattern
8
- * @example
9
- * ```ts
10
- * localeDatePattern('fr') // 'dd/mm/yyyy'
11
- * ```
12
- */
13
- export const localeDatePattern = (locale: string) => {
14
- const year = { value: '2023', pattern: 'yyyy' };
15
- const month = { value: '12', pattern: 'mm' };
16
- const day = { value: '24', pattern: 'dd' };
17
- return localeDate([year.value, month.value, day.value].join('-'), locale, { timeZone: 'UTC' })
18
- .replace(year.value, year.pattern)
19
- .replace(month.value, month.pattern)
20
- .replace(day.value, day.pattern);
21
- };
22
-
23
- /**
24
- * Get locale and messages
25
- * We load the defined locale but for now we only support the first subtag for messages
26
- * @param element - element we need to get the language
27
- * @param messages - messages to use
28
- * @param defaultLocale - default messages locale
29
- * @returns messages object
30
- */
31
- const localeMessages = (element: HTMLElement, messages: ObjectType, defaultLocale: string): { locale: string; messages: ObjectType } => {
32
- // Get local
33
- const closestLangAttribute: HTMLElement | null = element.closest('[lang]');
34
- const closestLang: string[] = Intl.NumberFormat.supportedLocalesOf(closestLangAttribute?.lang as string);
35
- const locale = closestLang.length > 0 && typeof closestLang[0] === 'string' ? closestLang[0] : navigator.language || defaultLocale;
36
- // Only keep first subtag
37
- const localeSubtag = locale.split('-').shift() as string;
38
-
39
- // If messages is empty, return a default object
40
- if (Object.keys(messages).length === 0) {
41
- return {
42
- locale,
43
- messages: { lang: defaultLocale },
44
- };
45
- }
46
-
47
- // Return
48
- return {
49
- locale,
50
- messages: (messages[localeSubtag] ?? messages[defaultLocale] ?? { lang: defaultLocale }) as ObjectType,
51
- };
52
- };
53
-
54
- /**
55
- * Format number to the locale currency
56
- * @param number - number to format
57
- * @param locale - locale to apply
58
- * @param currency - currency to apply
59
- * @returns formatted currency
60
- * @example
61
- * ```ts
62
- * localeCurrency(1234567890.12, 'fr', 'EUR') // '1 234 567 890,12\xa0€'
63
- * ```
64
- */
65
- export const localeCurrency = (number: number, locale: string, currency: string): string => new Intl.NumberFormat(locale, { style: 'currency', currency }).format(number);
66
-
67
- /**
68
- * Format number to locale
69
- * @param number - number to format
70
- * @param locale - locale to apply
71
- * @param decimalLength - decimal length to apply
72
- * @returns formatted number
73
- * @example
74
- * ```ts
75
- * localeNumber(1234567890.12, 'fr') // 1 234 567 890,12
76
- * ```
77
- */
78
- export const localeNumber = (number: number, locale: string, decimalLength: number = 0): string =>
79
- new Intl.NumberFormat(locale, { minimumFractionDigits: decimalLength }).format(Number(number));
80
-
81
- /**
82
- * Convert bytes number to locale string representation
83
- * @param number - size in bytes
84
- * @param locale - locale to apply
85
- * @returns bytes in locale string format
86
- */
87
- export const localeByte = (number: number, locale: string): string => {
88
- if (typeof number !== 'number' || Number.isNaN(number) || number < 0) {
89
- throw new Error('localeByte - size must be a positive number.');
90
- }
91
-
92
- const base = 1024;
93
- const units: string[] = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'];
94
-
95
- // find appropriate unit base on size and base power
96
- const unitIndex = units.findIndex((_, key) => number < Math.pow(base, key + 1));
97
-
98
- // convert number to string with unit
99
- return Intl.NumberFormat(locale, {
100
- minimumFractionDigits: 0,
101
- maximumFractionDigits: 2,
102
- unit: units[unitIndex],
103
- unitDisplay: units[unitIndex] === 'byte' ? 'long' : 'short',
104
- style: 'unit',
105
- }).format(number / Math.pow(base, unitIndex));
106
- };
107
-
108
- /**
109
- * Format number as percentage based on locale
110
- * @param number - number to format
111
- * @param locale - locale to apply
112
- * @param decimalLength - decimal length to apply
113
- * @returns formatted percentage
114
- * @example
115
- * ```ts
116
- * localePercent(0.42, 'fr', 2) // '42,00 %'
117
- * localePercent(0.42, 'en', 2) // '42.00%'
118
- * ```
119
- */
120
- export const localePercent = (number: number, locale: string, decimalLength: number = 0): string => {
121
- return new Intl.NumberFormat(locale, {
122
- style: 'percent',
123
- minimumFractionDigits: decimalLength,
124
- maximumFractionDigits: decimalLength,
125
- }).format(number);
126
- };
127
-
128
- /**
129
- * Format number with standardized unit based on locale using Intl unit formatting
130
- * @param number - number to format
131
- * @param locale - locale to apply
132
- * @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
133
- * @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
134
- * @param decimalLength - decimal length to apply
135
- * @returns formatted number with localized unit
136
- * @example
137
- * ```ts
138
- * localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
139
- * localeUnit(23, 'fr', 'celsius') // '23 °C'
140
- * localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
141
- * ```
142
- */
143
- export const localeUnit = (
144
- number: number,
145
- locale: string,
146
- unit: Intl.NumberFormatOptions['unit'],
147
- unitDisplay: Intl.NumberFormatOptions['unitDisplay'] = 'short',
148
- decimalLength: number = 0,
149
- ): string => {
150
- return new Intl.NumberFormat(locale, {
151
- style: 'unit',
152
- unit,
153
- unitDisplay,
154
- minimumFractionDigits: decimalLength,
155
- maximumFractionDigits: decimalLength,
156
- }).format(number);
157
- };
158
-
159
- /**
160
- * Locale date format
161
- * @param date - date to format
162
- * @param locale - locale to apply
163
- * @param config - DateTimeFormatOptions object to apply
164
- * @returns formatted date
165
- * @example
166
- * ```ts
167
- * localeDate('2022-06-02', 'fr') // '02/06/2022'
168
- * ```
169
- */
170
- export const localeDate = (date: string | undefined, locale: string, config?: Intl.DateTimeFormatOptions): string =>
171
- typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
172
-
173
- /**
174
- * Get Intl object
175
- * @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
176
- * @param defaultLocale - fallback locale to render. `ex: 'en'`.
177
- * @returns from the element passed in return function you will get the matching messages object
178
- * @example
179
- * ```ts
180
- * import en from './en/messages.json';
181
- * import fr from './fr/messages.json';
182
- * import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
183
- *
184
- * const defaultLocale = 'en';
185
- * const messages = { en, fr };
186
- *
187
- * export const initLocales = defineLocales(messages, defaultLocale);
188
- * ```
189
- */
190
- export const defineLocales =
191
- (messages: ObjectType, defaultLocale: 'fr' | 'en') =>
192
- (element: HTMLElement): { locale: string; messages: ObjectType } =>
193
- localeMessages(element, messages, defaultLocale);
@@ -1,43 +0,0 @@
1
- import { type PlaywrightTestConfig } from '@playwright/test';
2
-
3
- const config: PlaywrightTestConfig = {
4
- testMatch: '*.e2e.ts',
5
- snapshotPathTemplate: '{testFileDir}/__screenshots__/{arg}-{projectName}{ext}',
6
- /* Maximum time one test can run for. */
7
- timeout: 30 * 1000,
8
- expect: {
9
- /**
10
- * Maximum time expect() should wait for the condition to be met.
11
- * For example in `await expect(locator).toHaveText();`
12
- */
13
- timeout: 5000,
14
- },
15
- /* Run tests in files in parallel */
16
- fullyParallel: true,
17
- /* Fail the build on CI if you accidentally left test.only in the source code. */
18
- forbidOnly: !!process.env.CI,
19
- /* Retry on CI only */
20
- retries: process.env.CI ? 2 : 0,
21
- /* Stop on CI on first failure */
22
- maxFailures: process.env.CI ? 1 : 0,
23
- /* Opt out of parallel tests on CI. */
24
- workers: process.env.CI ? 1 : undefined,
25
- /* Reporter to use. See https://playwright.dev/docs/test-reporters */
26
- reporter: [
27
- ['html', { open: 'never' }],
28
- ['junit', { outputFile: 'results.xml' }],
29
- ],
30
- /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
31
- use: {
32
- headless: true,
33
- /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
34
- actionTimeout: 0,
35
- /* Base URL to use in actions like `await page.goto('/')`. */
36
- // baseURL: 'http://localhost:3000',
37
-
38
- /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
39
- trace: 'on-first-retry',
40
- },
41
- };
42
-
43
- export default config;
@@ -1,16 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { playwrightBaseConfig, renderAttributes, renderProperties } from '.';
3
-
4
- describe('playwright', () => {
5
- describe('config', () => {
6
- test('import', () => {
7
- expect(playwrightBaseConfig).toBeDefined();
8
- });
9
- });
10
- describe('utils', () => {
11
- test('import', () => {
12
- expect(renderAttributes).toBeDefined();
13
- expect(renderProperties).toBeDefined();
14
- });
15
- });
16
- });
@@ -1,4 +0,0 @@
1
- import playwrightBaseConfig from './config';
2
- import { renderAttributes, renderProperties } from './utils';
3
-
4
- export { playwrightBaseConfig, renderAttributes, renderProperties };
@@ -1,48 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`utils > renderAttributes > Should render attributes 1`] = `""`;
4
-
5
- exports[`utils > renderAttributes > Should render attributes 2`] = `""`;
6
-
7
- exports[`utils > renderAttributes > Should render attributes 3`] = `""`;
8
-
9
- exports[`utils > renderAttributes > Should render attributes 4`] = `"name="batman""`;
10
-
11
- exports[`utils > renderAttributes > Should render attributes 5`] = `"name="<div class='blu'>blu</div>""`;
12
-
13
- exports[`utils > renderAttributes > Should render attributes 6`] = `"name="batman" user="bruce" active="" id="1""`;
14
-
15
- exports[`utils > renderAttributes > Should render attributes 7`] = `"empty-string="""`;
16
-
17
- exports[`utils > renderAttributes > Should render attributes 8`] = `"name="0""`;
18
-
19
- exports[`utils > renderAttributes > Should render attributes 9`] = `""`;
20
-
21
- exports[`utils > renderAttributes > Should render attributes 10`] = `"name="""`;
22
-
23
- exports[`utils > renderAttributes > Should render attributes 11`] = `""`;
24
-
25
- exports[`utils > renderAttributes > Should render attributes 12`] = `""`;
26
-
27
- exports[`utils > renderAttributes > Should render attributes 13`] = `""`;
28
-
29
- exports[`utils > renderProperties > Should not render attributes as properties 1`] = `""`;
30
-
31
- exports[`utils > renderProperties > Should not render attributes as properties 2`] = `""`;
32
-
33
- exports[`utils > renderProperties > Should not render attributes as properties 3`] = `""`;
34
-
35
- exports[`utils > renderProperties > Should not render attributes as properties 4`] = `
36
- "
37
- ;"
38
- `;
39
-
40
- exports[`utils > renderProperties > Should render properties, case id selector 1`] = `
41
- "
42
- document.querySelector('div').object={};document.querySelector('div').array=[];"
43
- `;
44
-
45
- exports[`utils > renderProperties > Should render properties, case id selector 2`] = `
46
- "
47
- document.querySelector('div').object={};document.querySelector('div').array=[];document.querySelector('div').fn=() => {};"
48
- `;
@@ -1 +0,0 @@
1
- export * from './utils';
@@ -1,43 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { renderAttributes, renderProperties } from './utils';
3
-
4
- describe('utils', () => {
5
- describe('renderAttributes', () => {
6
- test.each([
7
- undefined,
8
- null,
9
- {},
10
- { name: 'batman' },
11
- { name: '<div class="blu">blu</div>' },
12
- { name: 'batman', user: 'bruce', active: true, id: 1, object: {}, array: [], function: () => {} },
13
- { emptyString: '' },
14
- { name: 0 },
15
- { name: false },
16
- { name: ' ' },
17
- { name: null },
18
- { name: undefined },
19
- ' ',
20
- ])('Should render attributes', args => {
21
- const res = renderAttributes(args as Record<string, unknown>);
22
- expect(res).toMatchSnapshot();
23
- });
24
- });
25
-
26
- describe('renderProperties', () => {
27
- test('Should render properties, case id selector', () => {
28
- const res = renderProperties({ object: {} }, '#id');
29
- expect(res).toEqual("\n document.getElementById('id').object={};");
30
- });
31
- test.each([undefined, null, {}, { name: 'batman' }])('Should not render attributes as properties', args => {
32
- const res = renderProperties(args as Record<string, unknown>, 'div');
33
- expect(res).toMatchSnapshot();
34
- });
35
- test.each([undefined, () => {}].map(fn => ({ name: 'batman', user: 'bruce', active: true, id: 1, object: {}, array: [], fn })))(
36
- 'Should render properties, case id selector',
37
- args => {
38
- const res = renderProperties(args, 'div');
39
- expect(res).toMatchSnapshot();
40
- },
41
- );
42
- });
43
- });