@mgdis/core-ui-helpers 1.2.1 → 1.2.2
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/CHANGELOG.md +6 -0
- package/package.json +8 -8
- package/rollup.config.mjs +3 -3
- package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +1 -1
- package/src/angular/mg-angular-logger/mg-angular-logger.ts +2 -1
- package/src/locale/index.spec.ts +10 -1
- package/src/locale/locale.spec.ts +43 -18
- package/src/locale/locale.ts +13 -5
- package/src/playwright/utils/utils.spec.ts +16 -9
- package/src/playwright/utils/utils.ts +14 -6
- package/src/stencil/ide/cem.spec.ts +4 -4
- package/src/stencil/ide/cem.ts +9 -9
- package/src/stencil/ide/ide.ts +26 -26
- package/src/storybook/stencil/stencil.spec.ts +30 -6
- package/src/storybook/stencil/stencil.ts +24 -13
- package/src/tests/index.spec.ts +7 -1
- package/src/tests/unit/unit.conf.ts +9 -2
- package/src/tests/unit/unit.spec.ts +103 -61
- package/src/tests/unit/unit.ts +38 -11
- package/src/utils/attributes/id/id.spec.ts +9 -6
- package/src/utils/attributes/id/id.ts +3 -2
- package/src/utils/components/components.spec.ts +6 -6
- package/src/utils/components/components.ts +19 -5
- package/src/utils/elements/elements.ts +4 -2
- package/src/utils/number/number.spec.ts +2 -2
- package/src/utils/object/object.spec.ts +2 -2
- package/src/utils/object/object.ts +2 -1
- package/src/utils/string/string.spec.ts +12 -5
- package/src/utils/string/string.ts +2 -1
- package/src/utils/windows/windows.spec.ts +6 -2
- package/src/vue/mg-model/mg-model.ts +7 -2
- package/vitest.config.mts +1 -1
- package/.turbo/turbo-build.log +0 -49
- package/dist/angular/index.cjs +0 -27
- package/dist/angular/index.cjs.map +0 -1
- package/dist/angular/index.d.ts +0 -21
- package/dist/angular/index.js +0 -25
- package/dist/angular/index.js.map +0 -1
- package/dist/angular/index.umd.js +0 -33
- package/dist/angular/index.umd.js.map +0 -1
- package/dist/locale/index.cjs +0 -188
- package/dist/locale/index.cjs.map +0 -1
- package/dist/locale/index.d.ts +0 -110
- package/dist/locale/index.js +0 -179
- package/dist/locale/index.js.map +0 -1
- package/dist/locale/index.umd.js +0 -194
- package/dist/locale/index.umd.js.map +0 -1
- package/dist/playwright/index.cjs +0 -130
- package/dist/playwright/index.cjs.map +0 -1
- package/dist/playwright/index.d.ts +0 -39
- package/dist/playwright/index.js +0 -126
- package/dist/playwright/index.js.map +0 -1
- package/dist/playwright/index.umd.js +0 -136
- package/dist/playwright/index.umd.js.map +0 -1
- package/dist/stencil/index.cjs +0 -363
- package/dist/stencil/index.cjs.map +0 -1
- package/dist/stencil/index.d.ts +0 -222
- package/dist/stencil/index.js +0 -357
- package/dist/stencil/index.js.map +0 -1
- package/dist/stencil/index.umd.js +0 -369
- package/dist/stencil/index.umd.js.map +0 -1
- package/dist/storybook/index.cjs +0 -2333
- package/dist/storybook/index.cjs.map +0 -1
- package/dist/storybook/index.d.ts +0 -85
- package/dist/storybook/index.js +0 -2327
- package/dist/storybook/index.js.map +0 -1
- package/dist/storybook/index.umd.js +0 -2339
- package/dist/storybook/index.umd.js.map +0 -1
- package/dist/tests/index.cjs +0 -300
- package/dist/tests/index.cjs.map +0 -1
- package/dist/tests/index.d.ts +0 -100
- package/dist/tests/index.js +0 -294
- package/dist/tests/index.js.map +0 -1
- package/dist/tests/index.umd.js +0 -306
- package/dist/tests/index.umd.js.map +0 -1
- package/dist/utils/index.cjs +0 -433
- package/dist/utils/index.cjs.map +0 -1
- package/dist/utils/index.d.ts +0 -265
- package/dist/utils/index.js +0 -409
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/index.umd.js +0 -439
- package/dist/utils/index.umd.js.map +0 -1
- package/dist/vue/index.cjs +0 -1934
- package/dist/vue/index.cjs.map +0 -1
- package/dist/vue/index.d.ts +0 -35
- package/dist/vue/index.js +0 -1932
- package/dist/vue/index.js.map +0 -1
- package/dist/vue/index.umd.js +0 -1940
- package/dist/vue/index.umd.js.map +0 -1
package/dist/locale/index.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Date RegExp, usefull to test if string is a follow the date pattern
|
|
3
|
-
* @example
|
|
4
|
-
* ```ts
|
|
5
|
-
* dateRegExp.test('mystring') // false
|
|
6
|
-
* dateRegExp.test('2020-12-31') // true
|
|
7
|
-
* ```
|
|
8
|
-
*/
|
|
9
|
-
const dateRegExp = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Gets the date pattern based on the specified locale.
|
|
13
|
-
* @param locale - the locale to refer to
|
|
14
|
-
* @returns date pattern
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* localeDatePattern('fr') // 'dd/mm/yyyy'
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
const localeDatePattern = (locale) => {
|
|
21
|
-
const year = { value: '2023', pattern: 'yyyy' };
|
|
22
|
-
const month = { value: '12', pattern: 'mm' };
|
|
23
|
-
const day = { value: '24', pattern: 'dd' };
|
|
24
|
-
return localeDate([year.value, month.value, day.value].join('-'), locale, { timeZone: 'UTC' })
|
|
25
|
-
.replace(year.value, year.pattern)
|
|
26
|
-
.replace(month.value, month.pattern)
|
|
27
|
-
.replace(day.value, day.pattern);
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Get locale and messages
|
|
31
|
-
* We load the defined locale but for now we only support the first subtag for messages
|
|
32
|
-
* @param element - element we need to get the language
|
|
33
|
-
* @param messages - messages to use
|
|
34
|
-
* @param defaultLocale - default messages locale
|
|
35
|
-
* @returns messages object
|
|
36
|
-
*/
|
|
37
|
-
const localeMessages = (element, messages, defaultLocale) => {
|
|
38
|
-
// Get local
|
|
39
|
-
const closestLangAttribute = element.closest('[lang]');
|
|
40
|
-
const closestLang = Intl.NumberFormat.supportedLocalesOf(closestLangAttribute?.lang);
|
|
41
|
-
const locale = closestLang.length > 0 && typeof closestLang[0] === 'string' ? closestLang[0] : navigator.language || defaultLocale;
|
|
42
|
-
// Only keep first subtag
|
|
43
|
-
const localeSubtag = locale.split('-').shift();
|
|
44
|
-
// If messages is empty, return a default object
|
|
45
|
-
if (Object.keys(messages).length === 0) {
|
|
46
|
-
return {
|
|
47
|
-
locale,
|
|
48
|
-
messages: { lang: defaultLocale },
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
// Return
|
|
52
|
-
return {
|
|
53
|
-
locale,
|
|
54
|
-
messages: (messages[localeSubtag] ?? messages[defaultLocale] ?? { lang: defaultLocale }),
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Format number to the locale currency
|
|
59
|
-
* @param number - number to format
|
|
60
|
-
* @param locale - locale to apply
|
|
61
|
-
* @param currency - currency to apply
|
|
62
|
-
* @returns formatted currency
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* localeCurrency(1234567890.12, 'fr', 'EUR') // '1 234 567 890,12\xa0€'
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
const localeCurrency = (number, locale, currency) => new Intl.NumberFormat(locale, { style: 'currency', currency }).format(number);
|
|
69
|
-
/**
|
|
70
|
-
* Format number to locale
|
|
71
|
-
* @param number - number to format
|
|
72
|
-
* @param locale - locale to apply
|
|
73
|
-
* @param decimalLength - decimal length to apply
|
|
74
|
-
* @returns formatted number
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* localeNumber(1234567890.12, 'fr') // 1 234 567 890,12
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
const localeNumber = (number, locale, decimalLength = 0) => new Intl.NumberFormat(locale, { minimumFractionDigits: decimalLength }).format(Number(number));
|
|
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
|
-
const localeByte = (number, locale) => {
|
|
88
|
-
if (typeof number !== 'number' || Number.isNaN(number) || number < 0) {
|
|
89
|
-
throw new Error('localeByte - size must be a positive number.');
|
|
90
|
-
}
|
|
91
|
-
const base = 1024;
|
|
92
|
-
const units = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'];
|
|
93
|
-
// find appropriate unit base on size and base power
|
|
94
|
-
const unitIndex = units.findIndex((_, key) => number < Math.pow(base, key + 1));
|
|
95
|
-
// convert number to string with unit
|
|
96
|
-
return Intl.NumberFormat(locale, {
|
|
97
|
-
minimumFractionDigits: 0,
|
|
98
|
-
maximumFractionDigits: 2,
|
|
99
|
-
unit: units[unitIndex],
|
|
100
|
-
unitDisplay: units[unitIndex] === 'byte' ? 'long' : 'short',
|
|
101
|
-
style: 'unit',
|
|
102
|
-
}).format(number / Math.pow(base, unitIndex));
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* Format number as percentage based on locale
|
|
106
|
-
* @param number - number to format
|
|
107
|
-
* @param locale - locale to apply
|
|
108
|
-
* @param decimalLength - decimal length to apply
|
|
109
|
-
* @returns formatted percentage
|
|
110
|
-
* @example
|
|
111
|
-
* ```ts
|
|
112
|
-
* localePercent(0.42, 'fr', 2) // '42,00 %'
|
|
113
|
-
* localePercent(0.42, 'en', 2) // '42.00%'
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
const localePercent = (number, locale, decimalLength = 0) => {
|
|
117
|
-
return new Intl.NumberFormat(locale, {
|
|
118
|
-
style: 'percent',
|
|
119
|
-
minimumFractionDigits: decimalLength,
|
|
120
|
-
maximumFractionDigits: decimalLength,
|
|
121
|
-
}).format(number);
|
|
122
|
-
};
|
|
123
|
-
/**
|
|
124
|
-
* Format number with standardized unit based on locale using Intl unit formatting
|
|
125
|
-
* @param number - number to format
|
|
126
|
-
* @param locale - locale to apply
|
|
127
|
-
* @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
|
|
128
|
-
* @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
|
|
129
|
-
* @param decimalLength - decimal length to apply
|
|
130
|
-
* @returns formatted number with localized unit
|
|
131
|
-
* @example
|
|
132
|
-
* ```ts
|
|
133
|
-
* localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
|
|
134
|
-
* localeUnit(23, 'fr', 'celsius') // '23 °C'
|
|
135
|
-
* localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
const localeUnit = (number, locale, unit, unitDisplay = 'short', decimalLength = 0) => {
|
|
139
|
-
return new Intl.NumberFormat(locale, {
|
|
140
|
-
style: 'unit',
|
|
141
|
-
unit,
|
|
142
|
-
unitDisplay,
|
|
143
|
-
minimumFractionDigits: decimalLength,
|
|
144
|
-
maximumFractionDigits: decimalLength,
|
|
145
|
-
}).format(number);
|
|
146
|
-
};
|
|
147
|
-
/**
|
|
148
|
-
* Locale date format
|
|
149
|
-
* @param date - date to format
|
|
150
|
-
* @param locale - locale to apply
|
|
151
|
-
* @param config - DateTimeFormatOptions object to apply
|
|
152
|
-
* @returns formatted date
|
|
153
|
-
* @example
|
|
154
|
-
* ```ts
|
|
155
|
-
* localeDate('2022-06-02', 'fr') // '02/06/2022'
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
const localeDate = (date, locale, config) => typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
|
|
159
|
-
/**
|
|
160
|
-
* Get Intl object
|
|
161
|
-
* @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
|
|
162
|
-
* @param defaultLocale - fallback locale to render. `ex: 'en'`.
|
|
163
|
-
* @returns from the element passed in return function you will get the matching messages object
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* import en from './en/messages.json';
|
|
167
|
-
* import fr from './fr/messages.json';
|
|
168
|
-
* import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
|
|
169
|
-
*
|
|
170
|
-
* const defaultLocale = 'en';
|
|
171
|
-
* const messages = { en, fr };
|
|
172
|
-
*
|
|
173
|
-
* export const initLocales = defineLocales(messages, defaultLocale);
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
|
|
177
|
-
|
|
178
|
-
export { defineLocales, localeByte, localeCurrency, localeDate, localeDatePattern, localeNumber, localePercent, localeUnit };
|
|
179
|
-
//# sourceMappingURL=index.js.map
|
package/dist/locale/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/utils/date/date.ts","../../src/locale/locale.ts"],"sourcesContent":[null,null],"names":[],"mappings":"AAAA;;;;;;;AAOG;AACI,MAAM,UAAU,GAAG,+CAA+C;;ACLzE;;;;;;;;AAQG;AACI,MAAM,iBAAiB,GAAG,CAAC,MAAc,KAAI;IAClD,MAAM,IAAI,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE;IAC/C,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;IAC5C,MAAM,GAAG,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;AAC1C,IAAA,OAAO,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE;SAC1F,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO;SAChC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO;SAClC,OAAO,CAAC,G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QAAA,qBAAqB,EAAE,aAAa;AACrC,KAAA,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;AACnB;AAEA;;;;;;;;;;;;;;AAcG;AACI,MAAM,UAAU,GAAG,CACxB,MAAc,EACd,MAAc,EACd,IAAsC,EACtC,WAAA,GAAuD,OAAO,EAC9D,aAAA,GAAwB,CAAC,KACf;AACV,IAAA,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;AACnC,QAAA,KAAK,EAAE,MAAM;QACb,IAAI;QACJ,WAAW;AACX,QAAA,qBAAqB,EAAE,aAAa;AACpC,QAAA,qBAAqB,EAAE,aAAa;AACrC,KAAA,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;AACnB;AAEA;;;;;;;;;;AAUG;AACI,MAAM,UAAU,GAAG,CAAC,IAAwB,EAAE,MAAc,EAAE,MAAmC,KACtG,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC;AAExI;;;;;;;;;;;;;;;;AAgBG;AACI,MAAM,aAAa,GACxB,CAAC,QAAoB,EAAE,aAA0B,KACjD,CAAC,OAAoB,KACnB,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,aAAa;;;;"}
|
package/dist/locale/index.umd.js
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.CoreUiHelpers = global.CoreUiHelpers || {}, global.CoreUiHelpers.locale = {})));
|
|
5
|
-
})(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Date RegExp, usefull to test if string is a follow the date pattern
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* dateRegExp.test('mystring') // false
|
|
12
|
-
* dateRegExp.test('2020-12-31') // true
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
const dateRegExp = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Gets the date pattern based on the specified locale.
|
|
19
|
-
* @param locale - the locale to refer to
|
|
20
|
-
* @returns date pattern
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* localeDatePattern('fr') // 'dd/mm/yyyy'
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
const localeDatePattern = (locale) => {
|
|
27
|
-
const year = { value: '2023', pattern: 'yyyy' };
|
|
28
|
-
const month = { value: '12', pattern: 'mm' };
|
|
29
|
-
const day = { value: '24', pattern: 'dd' };
|
|
30
|
-
return localeDate([year.value, month.value, day.value].join('-'), locale, { timeZone: 'UTC' })
|
|
31
|
-
.replace(year.value, year.pattern)
|
|
32
|
-
.replace(month.value, month.pattern)
|
|
33
|
-
.replace(day.value, day.pattern);
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Get locale and messages
|
|
37
|
-
* We load the defined locale but for now we only support the first subtag for messages
|
|
38
|
-
* @param element - element we need to get the language
|
|
39
|
-
* @param messages - messages to use
|
|
40
|
-
* @param defaultLocale - default messages locale
|
|
41
|
-
* @returns messages object
|
|
42
|
-
*/
|
|
43
|
-
const localeMessages = (element, messages, defaultLocale) => {
|
|
44
|
-
// Get local
|
|
45
|
-
const closestLangAttribute = element.closest('[lang]');
|
|
46
|
-
const closestLang = Intl.NumberFormat.supportedLocalesOf(closestLangAttribute?.lang);
|
|
47
|
-
const locale = closestLang.length > 0 && typeof closestLang[0] === 'string' ? closestLang[0] : navigator.language || defaultLocale;
|
|
48
|
-
// Only keep first subtag
|
|
49
|
-
const localeSubtag = locale.split('-').shift();
|
|
50
|
-
// If messages is empty, return a default object
|
|
51
|
-
if (Object.keys(messages).length === 0) {
|
|
52
|
-
return {
|
|
53
|
-
locale,
|
|
54
|
-
messages: { lang: defaultLocale },
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
// Return
|
|
58
|
-
return {
|
|
59
|
-
locale,
|
|
60
|
-
messages: (messages[localeSubtag] ?? messages[defaultLocale] ?? { lang: defaultLocale }),
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* Format number to the locale currency
|
|
65
|
-
* @param number - number to format
|
|
66
|
-
* @param locale - locale to apply
|
|
67
|
-
* @param currency - currency to apply
|
|
68
|
-
* @returns formatted currency
|
|
69
|
-
* @example
|
|
70
|
-
* ```ts
|
|
71
|
-
* localeCurrency(1234567890.12, 'fr', 'EUR') // '1 234 567 890,12\xa0€'
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
const localeCurrency = (number, locale, currency) => new Intl.NumberFormat(locale, { style: 'currency', currency }).format(number);
|
|
75
|
-
/**
|
|
76
|
-
* Format number to locale
|
|
77
|
-
* @param number - number to format
|
|
78
|
-
* @param locale - locale to apply
|
|
79
|
-
* @param decimalLength - decimal length to apply
|
|
80
|
-
* @returns formatted number
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* localeNumber(1234567890.12, 'fr') // 1 234 567 890,12
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
const localeNumber = (number, locale, decimalLength = 0) => new Intl.NumberFormat(locale, { minimumFractionDigits: decimalLength }).format(Number(number));
|
|
87
|
-
/**
|
|
88
|
-
* Convert bytes number to locale string representation
|
|
89
|
-
* @param number - size in bytes
|
|
90
|
-
* @param locale - locale to apply
|
|
91
|
-
* @returns bytes in locale string format
|
|
92
|
-
*/
|
|
93
|
-
const localeByte = (number, locale) => {
|
|
94
|
-
if (typeof number !== 'number' || Number.isNaN(number) || number < 0) {
|
|
95
|
-
throw new Error('localeByte - size must be a positive number.');
|
|
96
|
-
}
|
|
97
|
-
const base = 1024;
|
|
98
|
-
const units = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'];
|
|
99
|
-
// find appropriate unit base on size and base power
|
|
100
|
-
const unitIndex = units.findIndex((_, key) => number < Math.pow(base, key + 1));
|
|
101
|
-
// convert number to string with unit
|
|
102
|
-
return Intl.NumberFormat(locale, {
|
|
103
|
-
minimumFractionDigits: 0,
|
|
104
|
-
maximumFractionDigits: 2,
|
|
105
|
-
unit: units[unitIndex],
|
|
106
|
-
unitDisplay: units[unitIndex] === 'byte' ? 'long' : 'short',
|
|
107
|
-
style: 'unit',
|
|
108
|
-
}).format(number / Math.pow(base, unitIndex));
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* Format number as percentage based on locale
|
|
112
|
-
* @param number - number to format
|
|
113
|
-
* @param locale - locale to apply
|
|
114
|
-
* @param decimalLength - decimal length to apply
|
|
115
|
-
* @returns formatted percentage
|
|
116
|
-
* @example
|
|
117
|
-
* ```ts
|
|
118
|
-
* localePercent(0.42, 'fr', 2) // '42,00 %'
|
|
119
|
-
* localePercent(0.42, 'en', 2) // '42.00%'
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
const localePercent = (number, locale, decimalLength = 0) => {
|
|
123
|
-
return new Intl.NumberFormat(locale, {
|
|
124
|
-
style: 'percent',
|
|
125
|
-
minimumFractionDigits: decimalLength,
|
|
126
|
-
maximumFractionDigits: decimalLength,
|
|
127
|
-
}).format(number);
|
|
128
|
-
};
|
|
129
|
-
/**
|
|
130
|
-
* Format number with standardized unit based on locale using Intl unit formatting
|
|
131
|
-
* @param number - number to format
|
|
132
|
-
* @param locale - locale to apply
|
|
133
|
-
* @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
|
|
134
|
-
* @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
|
|
135
|
-
* @param decimalLength - decimal length to apply
|
|
136
|
-
* @returns formatted number with localized unit
|
|
137
|
-
* @example
|
|
138
|
-
* ```ts
|
|
139
|
-
* localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
|
|
140
|
-
* localeUnit(23, 'fr', 'celsius') // '23 °C'
|
|
141
|
-
* localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
const localeUnit = (number, locale, unit, unitDisplay = 'short', decimalLength = 0) => {
|
|
145
|
-
return new Intl.NumberFormat(locale, {
|
|
146
|
-
style: 'unit',
|
|
147
|
-
unit,
|
|
148
|
-
unitDisplay,
|
|
149
|
-
minimumFractionDigits: decimalLength,
|
|
150
|
-
maximumFractionDigits: decimalLength,
|
|
151
|
-
}).format(number);
|
|
152
|
-
};
|
|
153
|
-
/**
|
|
154
|
-
* Locale date format
|
|
155
|
-
* @param date - date to format
|
|
156
|
-
* @param locale - locale to apply
|
|
157
|
-
* @param config - DateTimeFormatOptions object to apply
|
|
158
|
-
* @returns formatted date
|
|
159
|
-
* @example
|
|
160
|
-
* ```ts
|
|
161
|
-
* localeDate('2022-06-02', 'fr') // '02/06/2022'
|
|
162
|
-
* ```
|
|
163
|
-
*/
|
|
164
|
-
const localeDate = (date, locale, config) => typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
|
|
165
|
-
/**
|
|
166
|
-
* Get Intl object
|
|
167
|
-
* @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
|
|
168
|
-
* @param defaultLocale - fallback locale to render. `ex: 'en'`.
|
|
169
|
-
* @returns from the element passed in return function you will get the matching messages object
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* import en from './en/messages.json';
|
|
173
|
-
* import fr from './fr/messages.json';
|
|
174
|
-
* import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
|
|
175
|
-
*
|
|
176
|
-
* const defaultLocale = 'en';
|
|
177
|
-
* const messages = { en, fr };
|
|
178
|
-
*
|
|
179
|
-
* export const initLocales = defineLocales(messages, defaultLocale);
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
|
|
183
|
-
|
|
184
|
-
exports.defineLocales = defineLocales;
|
|
185
|
-
exports.localeByte = localeByte;
|
|
186
|
-
exports.localeCurrency = localeCurrency;
|
|
187
|
-
exports.localeDate = localeDate;
|
|
188
|
-
exports.localeDatePattern = localeDatePattern;
|
|
189
|
-
exports.localeNumber = localeNumber;
|
|
190
|
-
exports.localePercent = localePercent;
|
|
191
|
-
exports.localeUnit = localeUnit;
|
|
192
|
-
|
|
193
|
-
}));
|
|
194
|
-
//# sourceMappingURL=index.umd.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../../src/utils/date/date.ts","../../src/locale/locale.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;;;;IAAA;;;;;;;IAOG;IACI,MAAM,UAAU,GAAG,+CAA+C;;ICLzE;;;;;;;;IAQG;AACI,UAAM,iBAAiB,GAAG,CAAC,MAAc,KAAI;QAClD,MAAM,IAAI,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE;QAC/C,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;QAC5C,MAAM,GAAG,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;IAC1C,IAAA,OAAO,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,EAAE,QAAQ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}/__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
|
-
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
|
|
38
|
-
trace: 'on-first-retry',
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Check if a value is of object type.
|
|
44
|
-
* @param object - The value to validate.
|
|
45
|
-
* @returns `true` if the value is a valid object (non-null and not an array), otherwise `false`.
|
|
46
|
-
*/
|
|
47
|
-
const isObject = (object) => typeof object === 'object' && !Array.isArray(object) && object !== null;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Typeguard function to check if all array items are strings.
|
|
51
|
-
* @param items - items to check
|
|
52
|
-
* @returns `true` if all items are strings
|
|
53
|
-
*/
|
|
54
|
-
/**
|
|
55
|
-
* Validate string
|
|
56
|
-
* @param value - value to check
|
|
57
|
-
* @returns `true` if string is valid
|
|
58
|
-
*/
|
|
59
|
-
const isValidString = (value) => typeof value === 'string' && value.trim() !== '';
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Render attributes from props objects.
|
|
63
|
-
*
|
|
64
|
-
* Booleans follow the HTML spec for boolean attributes: `true` emits an empty-value attribute
|
|
65
|
-
* (presence implies `true`), `false` omits the attribute entirely. This matches the behaviour
|
|
66
|
-
* of the `normalizeBooleanAttributes` Stencil helper.
|
|
67
|
-
* @param args - argument to render as string. ex: `{status: 'visible'}`
|
|
68
|
-
* @returns formated inline attributed. ex: 'status="visible"'
|
|
69
|
-
* @example
|
|
70
|
-
* ```ts
|
|
71
|
-
* import { renderAttributes } from '@mgdis/core-ui-helpers/dist/playwright';
|
|
72
|
-
*
|
|
73
|
-
* const attributes = renderAttributes({ status: 'visible', color: 'red', disabled: true, readonly: false });
|
|
74
|
-
* console.log(attributes); // Output: 'status="visible" color="red" disabled=""'
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
const renderAttributes = (args) => (isObject(args) &&
|
|
78
|
-
Object.keys(args)
|
|
79
|
-
.filter(key => ![null, undefined].includes(args[key]) && !['object', 'function'].includes(typeof args[key]) && args[key] !== false)
|
|
80
|
-
.map(key => {
|
|
81
|
-
const attribute = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
82
|
-
if (args[key] === true)
|
|
83
|
-
return `${attribute}=""`;
|
|
84
|
-
const value = typeof args[key] === 'number' || isValidString(args[key]) ? String(args[key]).replace(/"/g, "'") : '';
|
|
85
|
-
return `${attribute}="${value}"`;
|
|
86
|
-
})
|
|
87
|
-
.join(' ')) ||
|
|
88
|
-
'';
|
|
89
|
-
/**
|
|
90
|
-
* Render properties from props objects.
|
|
91
|
-
* Insert return value in <script></script> element
|
|
92
|
-
* @param args - argument to render as script. ex: `{status: 'visible'}`
|
|
93
|
-
* @param selector - querySelector get targetted element and bind properties on it
|
|
94
|
-
* @returns stringified properties script
|
|
95
|
-
* @example
|
|
96
|
-
* ```ts
|
|
97
|
-
* import { renderProperties } from '@mgdis/core-ui-helpers/dist/playwright';
|
|
98
|
-
*
|
|
99
|
-
* const propertiesScript = renderProperties({ status: 'visible', color: 'red' }, '.targetElement');
|
|
100
|
-
* console.log(propertiesScript);
|
|
101
|
-
* // Output: 'document.querySelector(".targetElement").status="visible"; document.querySelector(".targetElement").color="red";'
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
const renderProperties = (args, selector) => {
|
|
105
|
-
// has generated id selector starting with "#" create charcters (ex: '#0j3xzw4w7m') make test crashed sometime with "querySelector"
|
|
106
|
-
// we use "getElementById" document query
|
|
107
|
-
const query = selector.startsWith('#') ? 'getElementById' : 'querySelector';
|
|
108
|
-
if (query === 'getElementById')
|
|
109
|
-
selector = selector.replace('#', '');
|
|
110
|
-
return args !== null && typeof args === 'object' && Object.keys(args).length > 0
|
|
111
|
-
? `
|
|
112
|
-
${Object.keys(args)
|
|
113
|
-
.filter(key => ['object', 'function'].includes(typeof args[key]))
|
|
114
|
-
.map(key => {
|
|
115
|
-
const value = JSON.stringify(args[key], (_key, val) => (typeof val === 'function' ? `<fn>${val}</fn>` : val));
|
|
116
|
-
return `document.${query}('${selector}').${key}=${value}`;
|
|
117
|
-
}) // stringify json AND keep function values
|
|
118
|
-
.join(';') // create string
|
|
119
|
-
.split('"<fn>') // remove fn start decorator
|
|
120
|
-
.join('')
|
|
121
|
-
.split('</fn>"')
|
|
122
|
-
.join('') // remove fn end decorator
|
|
123
|
-
};`
|
|
124
|
-
: '';
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
exports.playwrightBaseConfig = config;
|
|
128
|
-
exports.renderAttributes = renderAttributes;
|
|
129
|
-
exports.renderProperties = renderProperties;
|
|
130
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/playwright/config/index.ts","../../src/utils/object/object.ts","../../src/utils/string/string.ts","../../src/playwright/utils/utils.ts"],"sourcesContent":[null,null,null,null],"names":[],"mappings":";;AAEA,MAAM,MAAM,GAAyB;AACnC,IAAA,SAAS,EAAE,UAAU;AACrB,IAAA,oBAAoB,EAAE,wDAAwD;;IAE9E,OAAO,EAAE,EAAE,GAAG,IAAI;AAClB,IAAA,MAAM,EAAE;AACN;;;AAGG;AACH,QAAA,OAAO,EAAE,IAAI;AACd,KAAA;;AAED,IAAA,aAAa,EAAE,IAAI;;AAEnB,IAAA,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;;AAE5B,IAAA,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;;AAE/B,IAAA,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;;AAEnC,IAAA,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS;;AAEvC,IAAA,QAAQ,EAAE;AACR,QAAA,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC3B,QAAA,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;AACzC,KAAA;;AAED,IAAA,GAAG,EAAE;AACH,QAAA,QAAQ,EAAE,IAAI;;AAEd,QAAA,aAAa,EAAE,CAAC;;;;AAKhB,QAAA,KAAK,EAAE,gBAAgB;AACxB,KAAA;;;ACvCH;;;;AAIG;AACI,MAAM,QAAQ,GAAG,CAAI,MAAe,KA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
|
-
|
|
3
|
-
declare const config: PlaywrightTestConfig;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Render attributes from props objects.
|
|
7
|
-
*
|
|
8
|
-
* Booleans follow the HTML spec for boolean attributes: `true` emits an empty-value attribute
|
|
9
|
-
* (presence implies `true`), `false` omits the attribute entirely. This matches the behaviour
|
|
10
|
-
* of the `normalizeBooleanAttributes` Stencil helper.
|
|
11
|
-
* @param args - argument to render as string. ex: `{status: 'visible'}`
|
|
12
|
-
* @returns formated inline attributed. ex: 'status="visible"'
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* import { renderAttributes } from '@mgdis/core-ui-helpers/dist/playwright';
|
|
16
|
-
*
|
|
17
|
-
* const attributes = renderAttributes({ status: 'visible', color: 'red', disabled: true, readonly: false });
|
|
18
|
-
* console.log(attributes); // Output: 'status="visible" color="red" disabled=""'
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
declare const renderAttributes: (args: Record<string, unknown>) => string;
|
|
22
|
-
/**
|
|
23
|
-
* Render properties from props objects.
|
|
24
|
-
* Insert return value in <script></script> element
|
|
25
|
-
* @param args - argument to render as script. ex: `{status: 'visible'}`
|
|
26
|
-
* @param selector - querySelector get targetted element and bind properties on it
|
|
27
|
-
* @returns stringified properties script
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* import { renderProperties } from '@mgdis/core-ui-helpers/dist/playwright';
|
|
31
|
-
*
|
|
32
|
-
* const propertiesScript = renderProperties({ status: 'visible', color: 'red' }, '.targetElement');
|
|
33
|
-
* console.log(propertiesScript);
|
|
34
|
-
* // Output: 'document.querySelector(".targetElement").status="visible"; document.querySelector(".targetElement").color="red";'
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
declare const renderProperties: (args: Record<string, unknown>, selector: string) => string;
|
|
38
|
-
|
|
39
|
-
export { config as playwrightBaseConfig, renderAttributes, renderProperties };
|