@mgdis/core-ui-helpers 1.0.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 (146) hide show
  1. package/.turbo/turbo-build.log +52 -0
  2. package/CHANGELOG.md +12 -0
  3. package/LICENSE +29 -0
  4. package/README.md +95 -0
  5. package/dist/angular/index.cjs +27 -0
  6. package/dist/angular/index.cjs.map +1 -0
  7. package/dist/angular/index.d.ts +21 -0
  8. package/dist/angular/index.js +25 -0
  9. package/dist/angular/index.js.map +1 -0
  10. package/dist/angular/index.umd.js +33 -0
  11. package/dist/angular/index.umd.js.map +1 -0
  12. package/dist/locale/index.cjs +164 -0
  13. package/dist/locale/index.cjs.map +1 -0
  14. package/dist/locale/index.d.ts +103 -0
  15. package/dist/locale/index.js +156 -0
  16. package/dist/locale/index.js.map +1 -0
  17. package/dist/locale/index.umd.js +170 -0
  18. package/dist/locale/index.umd.js.map +1 -0
  19. package/dist/playwright/index.cjs +124 -0
  20. package/dist/playwright/index.cjs.map +1 -0
  21. package/dist/playwright/index.d.ts +35 -0
  22. package/dist/playwright/index.js +120 -0
  23. package/dist/playwright/index.js.map +1 -0
  24. package/dist/playwright/index.umd.js +130 -0
  25. package/dist/playwright/index.umd.js.map +1 -0
  26. package/dist/stencil/index.cjs +281 -0
  27. package/dist/stencil/index.cjs.map +1 -0
  28. package/dist/stencil/index.d.ts +111 -0
  29. package/dist/stencil/index.js +277 -0
  30. package/dist/stencil/index.js.map +1 -0
  31. package/dist/stencil/index.umd.js +287 -0
  32. package/dist/stencil/index.umd.js.map +1 -0
  33. package/dist/storybook/index.cjs +2109 -0
  34. package/dist/storybook/index.cjs.map +1 -0
  35. package/dist/storybook/index.d.ts +85 -0
  36. package/dist/storybook/index.js +2103 -0
  37. package/dist/storybook/index.js.map +1 -0
  38. package/dist/storybook/index.umd.js +2115 -0
  39. package/dist/storybook/index.umd.js.map +1 -0
  40. package/dist/tests/index.cjs +166 -0
  41. package/dist/tests/index.cjs.map +1 -0
  42. package/dist/tests/index.d.ts +91 -0
  43. package/dist/tests/index.js +161 -0
  44. package/dist/tests/index.js.map +1 -0
  45. package/dist/tests/index.umd.js +172 -0
  46. package/dist/tests/index.umd.js.map +1 -0
  47. package/dist/utils/index.cjs +433 -0
  48. package/dist/utils/index.cjs.map +1 -0
  49. package/dist/utils/index.d.ts +265 -0
  50. package/dist/utils/index.js +409 -0
  51. package/dist/utils/index.js.map +1 -0
  52. package/dist/utils/index.umd.js +439 -0
  53. package/dist/utils/index.umd.js.map +1 -0
  54. package/dist/vue/index.cjs +1934 -0
  55. package/dist/vue/index.cjs.map +1 -0
  56. package/dist/vue/index.d.ts +35 -0
  57. package/dist/vue/index.js +1932 -0
  58. package/dist/vue/index.js.map +1 -0
  59. package/dist/vue/index.umd.js +1940 -0
  60. package/dist/vue/index.umd.js.map +1 -0
  61. package/json-doc.test.json +253 -0
  62. package/package.json +80 -0
  63. package/rollup.config.mjs +61 -0
  64. package/src/angular/index.spec.ts +8 -0
  65. package/src/angular/index.ts +1 -0
  66. package/src/angular/mg-angular-logger/index.ts +1 -0
  67. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +3 -0
  68. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +40 -0
  69. package/src/angular/mg-angular-logger/mg-angular-logger.ts +23 -0
  70. package/src/locale/index.spec.ts +14 -0
  71. package/src/locale/index.ts +1 -0
  72. package/src/locale/locale.spec.ts +173 -0
  73. package/src/locale/locale.ts +166 -0
  74. package/src/playwright/config/index.ts +43 -0
  75. package/src/playwright/index.spec.ts +16 -0
  76. package/src/playwright/index.ts +4 -0
  77. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +48 -0
  78. package/src/playwright/utils/index.ts +1 -0
  79. package/src/playwright/utils/utils.spec.ts +43 -0
  80. package/src/playwright/utils/utils.ts +64 -0
  81. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +527 -0
  82. package/src/stencil/ide/ide.spec.ts +25 -0
  83. package/src/stencil/ide/ide.ts +214 -0
  84. package/src/stencil/ide/index.ts +1 -0
  85. package/src/stencil/index.spec.ts +12 -0
  86. package/src/stencil/index.ts +1 -0
  87. package/src/storybook/index.spec.ts +15 -0
  88. package/src/storybook/index.ts +1 -0
  89. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +329 -0
  90. package/src/storybook/stencil/index.ts +1 -0
  91. package/src/storybook/stencil/stencil-shim.d.ts +1 -0
  92. package/src/storybook/stencil/stencil.conf.ts +4 -0
  93. package/src/storybook/stencil/stencil.spec.ts +142 -0
  94. package/src/storybook/stencil/stencil.ts +366 -0
  95. package/src/tests/index.spec.ts +13 -0
  96. package/src/tests/index.ts +1 -0
  97. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +52 -0
  98. package/src/tests/unit/index.ts +1 -0
  99. package/src/tests/unit/unit.conf.ts +9 -0
  100. package/src/tests/unit/unit.spec.ts +44 -0
  101. package/src/tests/unit/unit.ts +171 -0
  102. package/src/utils/attributes/class-list/class-list.spec.ts +36 -0
  103. package/src/utils/attributes/class-list/class-list.ts +51 -0
  104. package/src/utils/attributes/class-list/index.ts +1 -0
  105. package/src/utils/attributes/id/id.spec.ts +51 -0
  106. package/src/utils/attributes/id/id.ts +46 -0
  107. package/src/utils/attributes/id/index.ts +1 -0
  108. package/src/utils/attributes/index.ts +3 -0
  109. package/src/utils/attributes/to-kebab-case/index.ts +1 -0
  110. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +29 -0
  111. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +23 -0
  112. package/src/utils/components/__snapshots__/components.spec.ts.snap +313 -0
  113. package/src/utils/components/components.conf.ts +11 -0
  114. package/src/utils/components/components.spec.ts +192 -0
  115. package/src/utils/components/components.ts +138 -0
  116. package/src/utils/components/index.ts +2 -0
  117. package/src/utils/date/date.spec.ts +12 -0
  118. package/src/utils/date/date.ts +20 -0
  119. package/src/utils/date/index.ts +1 -0
  120. package/src/utils/elements/elements.spec.ts +17 -0
  121. package/src/utils/elements/elements.ts +16 -0
  122. package/src/utils/elements/index.ts +1 -0
  123. package/src/utils/index.spec.ts +84 -0
  124. package/src/utils/index.ts +8 -0
  125. package/src/utils/number/index.ts +1 -0
  126. package/src/utils/number/number.spec.ts +14 -0
  127. package/src/utils/number/number.ts +6 -0
  128. package/src/utils/object/index.ts +2 -0
  129. package/src/utils/object/object.conf.ts +4 -0
  130. package/src/utils/object/object.spec.ts +98 -0
  131. package/src/utils/object/object.ts +26 -0
  132. package/src/utils/string/index.ts +1 -0
  133. package/src/utils/string/string.spec.ts +47 -0
  134. package/src/utils/string/string.ts +38 -0
  135. package/src/utils/windows/index.ts +1 -0
  136. package/src/utils/windows/windows.spec.ts +65 -0
  137. package/src/utils/windows/windows.ts +50 -0
  138. package/src/vue/index.spec.ts +8 -0
  139. package/src/vue/index.ts +1 -0
  140. package/src/vue/mg-model/index.ts +1 -0
  141. package/src/vue/mg-model/mg-model.conf.ts +6 -0
  142. package/src/vue/mg-model/mg-model.spec.ts +50 -0
  143. package/src/vue/mg-model/mg-model.ts +50 -0
  144. package/tsconfig.json +17 -0
  145. package/typedoc.json +14 -0
  146. package/vitest.config.mts +13 -0
@@ -0,0 +1,156 @@
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
+ * Format number as percentage based on locale
83
+ * @param number - number to format
84
+ * @param locale - locale to apply
85
+ * @param decimalLength - decimal length to apply
86
+ * @returns formatted percentage
87
+ * @example
88
+ * ```ts
89
+ * localePercent(0.42, 'fr', 2) // '42,00 %'
90
+ * localePercent(0.42, 'en', 2) // '42.00%'
91
+ * ```
92
+ */
93
+ const localePercent = (number, locale, decimalLength = 0) => {
94
+ return new Intl.NumberFormat(locale, {
95
+ style: 'percent',
96
+ minimumFractionDigits: decimalLength,
97
+ maximumFractionDigits: decimalLength,
98
+ }).format(number);
99
+ };
100
+ /**
101
+ * Format number with standardized unit based on locale using Intl unit formatting
102
+ * @param number - number to format
103
+ * @param locale - locale to apply
104
+ * @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
105
+ * @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
106
+ * @param decimalLength - decimal length to apply
107
+ * @returns formatted number with localized unit
108
+ * @example
109
+ * ```ts
110
+ * localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
111
+ * localeUnit(23, 'fr', 'celsius') // '23 °C'
112
+ * localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
113
+ * ```
114
+ */
115
+ const localeUnit = (number, locale, unit, unitDisplay = 'short', decimalLength = 0) => {
116
+ return new Intl.NumberFormat(locale, {
117
+ style: 'unit',
118
+ unit,
119
+ unitDisplay,
120
+ minimumFractionDigits: decimalLength,
121
+ maximumFractionDigits: decimalLength,
122
+ }).format(number);
123
+ };
124
+ /**
125
+ * Locale date format
126
+ * @param date - date to format
127
+ * @param locale - locale to apply
128
+ * @param config - DateTimeFormatOptions object to apply
129
+ * @returns formatted date
130
+ * @example
131
+ * ```ts
132
+ * localeDate('2022-06-02', 'fr') // '02/06/2022'
133
+ * ```
134
+ */
135
+ const localeDate = (date, locale, config) => typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
136
+ /**
137
+ * Get Intl object
138
+ * @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
139
+ * @param defaultLocale - fallback locale to render. `ex: 'en'`.
140
+ * @returns from the element passed in return function you will get the matching messages object
141
+ * @example
142
+ * ```ts
143
+ * import en from './en/messages.json';
144
+ * import fr from './fr/messages.json';
145
+ * import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
146
+ *
147
+ * const defaultLocale = 'en';
148
+ * const messages = { en, fr };
149
+ *
150
+ * export const initLocales = defineLocales(messages, defaultLocale);
151
+ * ```
152
+ */
153
+ const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
154
+
155
+ export { defineLocales, localeCurrency, localeDate, localeDatePattern, localeNumber, localePercent, localeUnit };
156
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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;AACU,MAAA,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,C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OAAoB,KACnB,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,aAAa;;;;"}
@@ -0,0 +1,170 @@
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
+ * Format number as percentage based on locale
89
+ * @param number - number to format
90
+ * @param locale - locale to apply
91
+ * @param decimalLength - decimal length to apply
92
+ * @returns formatted percentage
93
+ * @example
94
+ * ```ts
95
+ * localePercent(0.42, 'fr', 2) // '42,00 %'
96
+ * localePercent(0.42, 'en', 2) // '42.00%'
97
+ * ```
98
+ */
99
+ const localePercent = (number, locale, decimalLength = 0) => {
100
+ return new Intl.NumberFormat(locale, {
101
+ style: 'percent',
102
+ minimumFractionDigits: decimalLength,
103
+ maximumFractionDigits: decimalLength,
104
+ }).format(number);
105
+ };
106
+ /**
107
+ * Format number with standardized unit based on locale using Intl unit formatting
108
+ * @param number - number to format
109
+ * @param locale - locale to apply
110
+ * @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
111
+ * @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
112
+ * @param decimalLength - decimal length to apply
113
+ * @returns formatted number with localized unit
114
+ * @example
115
+ * ```ts
116
+ * localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
117
+ * localeUnit(23, 'fr', 'celsius') // '23 °C'
118
+ * localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
119
+ * ```
120
+ */
121
+ const localeUnit = (number, locale, unit, unitDisplay = 'short', decimalLength = 0) => {
122
+ return new Intl.NumberFormat(locale, {
123
+ style: 'unit',
124
+ unit,
125
+ unitDisplay,
126
+ minimumFractionDigits: decimalLength,
127
+ maximumFractionDigits: decimalLength,
128
+ }).format(number);
129
+ };
130
+ /**
131
+ * Locale date format
132
+ * @param date - date to format
133
+ * @param locale - locale to apply
134
+ * @param config - DateTimeFormatOptions object to apply
135
+ * @returns formatted date
136
+ * @example
137
+ * ```ts
138
+ * localeDate('2022-06-02', 'fr') // '02/06/2022'
139
+ * ```
140
+ */
141
+ const localeDate = (date, locale, config) => typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
142
+ /**
143
+ * Get Intl object
144
+ * @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
145
+ * @param defaultLocale - fallback locale to render. `ex: 'en'`.
146
+ * @returns from the element passed in return function you will get the matching messages object
147
+ * @example
148
+ * ```ts
149
+ * import en from './en/messages.json';
150
+ * import fr from './fr/messages.json';
151
+ * import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
152
+ *
153
+ * const defaultLocale = 'en';
154
+ * const messages = { en, fr };
155
+ *
156
+ * export const initLocales = defineLocales(messages, defaultLocale);
157
+ * ```
158
+ */
159
+ const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
160
+
161
+ exports.defineLocales = defineLocales;
162
+ exports.localeCurrency = localeCurrency;
163
+ exports.localeDate = localeDate;
164
+ exports.localeDatePattern = localeDatePattern;
165
+ exports.localeNumber = localeNumber;
166
+ exports.localePercent = localePercent;
167
+ exports.localeUnit = localeUnit;
168
+
169
+ }));
170
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
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;AACU,UAAA,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,EAAE,KAAK,EAAE;aAC1F,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO;aAChC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO;aAClC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC;IACpC;IAEA;;;;;;;IAOG;IACH,MAAM,cAAc,GAAG,CAAC,OAAoB,EAAE,QAAoB,EAAE,aAAqB,KAA8C;;QAErI,MAAM,oBAAoB,GAAuB,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;IAC1E,IAAA,MAAM,WAAW,GAAa,IAAI,CAAC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,QAAA,KAAK,EAAE,SAAS;IAChB,QAAA,qBAAqB,EAAE,aAAa;IACpC,QAAA,qBAAqB,EAAE,aAAa;IACrC,KAAA,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IACnB;IAEA;;;;;;;;;;;;;;IAcG;AACU,UAAA,UAAU,GAAG,CACxB,MAAc,EACd,MAAc,EACd,IAAsC,EACtC,WAAuD,GAAA,OAAO,EAC9D,aAAwB,GAAA,CAAC,KACf;IACV,IAAA,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;IACnC,QAAA,KAAK,EAAE,MAAM;YACb,IAAI;YACJ,WAAW;IACX,QAAA,qBAAqB,EAAE,aAAa;IACpC,QAAA,qBAAqB,EAAE,aAAa;IACrC,KAAA,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;IACnB;IAEA;;;;;;;;;;IAUG;AACU,UAAA,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;IAExI;;;;;;;;;;;;;;;;IAgBG;AACU,UAAA,aAAa,GACxB,CAAC,QAAoB,EAAE,aAA0B,KACjD,CAAC,OAAoB,KACnB,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,aAAa;;;;;;;;;;;;;;"}
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ const config = {
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
+ /* 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
+ * @param args - argument to render as string. ex: `{status: 'visible'}`
64
+ * @returns formated inline attributed. ex: 'status="visible"'
65
+ * @example
66
+ * ```ts
67
+ * import { renderAttributes } from '@mgdis/core-ui-helpers/dist/playwright';
68
+ *
69
+ * const attributes = renderAttributes({ status: 'visible', color: 'red' });
70
+ * console.log(attributes); // Output: 'status="visible" color="red"'
71
+ * ```
72
+ */
73
+ const renderAttributes = (args) => (isObject(args) &&
74
+ Object.keys(args)
75
+ .filter(key => ![null, undefined, false].includes(args[key]) && !['object', 'function'].includes(typeof args[key]))
76
+ .map(key => {
77
+ const attribute = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
78
+ const value = ['number', 'boolean'].includes(typeof args[key]) || isValidString(args[key]) ? String(args[key]).replace(/"/g, "'") : '';
79
+ return `${attribute}="${value}"`;
80
+ })
81
+ .join(' ')) ||
82
+ '';
83
+ /**
84
+ * Render properties from props objects.
85
+ * Insert return value in <script></script> element
86
+ * @param args - argument to render as script. ex: `{status: 'visible'}`
87
+ * @param selector - querySelector get targetted element and bind properties on it
88
+ * @returns stringified properties script
89
+ * @example
90
+ * ```ts
91
+ * import { renderProperties } from '@mgdis/core-ui-helpers/dist/playwright';
92
+ *
93
+ * const propertiesScript = renderProperties({ status: 'visible', color: 'red' }, '.targetElement');
94
+ * console.log(propertiesScript);
95
+ * // Output: 'document.querySelector(".targetElement").status="visible"; document.querySelector(".targetElement").color="red";'
96
+ * ```
97
+ */
98
+ const renderProperties = (args, selector) => {
99
+ // has generated id selector starting with "#" create charcters (ex: '#0j3xzw4w7m') make test crashed sometime with "querySelector"
100
+ // we use "getElementById" document query
101
+ const query = selector.startsWith('#') ? 'getElementById' : 'querySelector';
102
+ if (query === 'getElementById')
103
+ selector = selector.replace('#', '');
104
+ return args !== null && typeof args === 'object' && Object.keys(args).length > 0
105
+ ? `
106
+ ${Object.keys(args)
107
+ .filter(key => ['object', 'function'].includes(typeof args[key]))
108
+ .map(key => {
109
+ const value = JSON.stringify(args[key], (_key, val) => (typeof val === 'function' ? `<fn>${val}</fn>` : val));
110
+ return `document.${query}('${selector}').${key}=${value}`;
111
+ }) // stringify json AND keep function values
112
+ .join(';') // create string
113
+ .split('"<fn>') // remove fn start decorator
114
+ .join('')
115
+ .split('</fn>"')
116
+ .join('') // remove fn end decorator
117
+ }`
118
+ : '';
119
+ };
120
+
121
+ exports.playwrightBaseConfig = config;
122
+ exports.renderAttributes = renderAttributes;
123
+ exports.renderProperties = renderProperties;
124
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
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,KAAkB,OAAO,M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C,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,gBAAgB,GAAG,eAAe;IAC/G,IAAI,KAAK,KAAK,gBAAgB;QAAE,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;AAEpE,IAAA,OAAO,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG;AAC7E,UAAE;AAEF,EAAA,EAAA,MAAM,CAAC,IAAI,CAAC,IAAI;AACb,aAAA,MAAM,CAAC,GAAG,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,QAAQ,CAAC,OAAQ,IAA8B,CAAC,GAAG,CAAC,CAAC;aAC1F,GAAG,CAAC,GAAG,IAAG;AACT,YAAA,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAE,IAA8B,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,MAAM,OAAO,GAAG,KAAK,UAAU,GAAG,CAAA,IAAA,EAAO,GAAG,CAAA,KAAA,CAAO,GAAG,GAAG,CAAC,CAAC;YACxI,OAAO,CAAA,SAAA,EAAY,KAAK,CAAK,EAAA,EAAA,QAAQ,MAAM,GAAG,CAAA,CAAA,EAAI,KAAK,CAAA,CAAE;SAC1D,CAAC;AACD,aAAA,IAAI,CAAC,GAAG,CAAC;AACT,aAAA,KAAK,CAAC,OAAO,CAAC;aACd,IAAI,CAAC,EAAE;aACP,KAAK,CAAC,QAAQ;AACd,aAAA,IAAI,CAAC,EAAE,CAAC;SACX;UACE,EAAE;AACR;;;;;;"}
@@ -0,0 +1,35 @@
1
+ import { PlaywrightTestConfig } from '@playwright/test';
2
+
3
+ declare const config: PlaywrightTestConfig;
4
+
5
+ /**
6
+ * Render attributes from props objects
7
+ * @param args - argument to render as string. ex: `{status: 'visible'}`
8
+ * @returns formated inline attributed. ex: 'status="visible"'
9
+ * @example
10
+ * ```ts
11
+ * import { renderAttributes } from '@mgdis/core-ui-helpers/dist/playwright';
12
+ *
13
+ * const attributes = renderAttributes({ status: 'visible', color: 'red' });
14
+ * console.log(attributes); // Output: 'status="visible" color="red"'
15
+ * ```
16
+ */
17
+ declare const renderAttributes: (args: Record<string, unknown>) => string;
18
+ /**
19
+ * Render properties from props objects.
20
+ * Insert return value in <script></script> element
21
+ * @param args - argument to render as script. ex: `{status: 'visible'}`
22
+ * @param selector - querySelector get targetted element and bind properties on it
23
+ * @returns stringified properties script
24
+ * @example
25
+ * ```ts
26
+ * import { renderProperties } from '@mgdis/core-ui-helpers/dist/playwright';
27
+ *
28
+ * const propertiesScript = renderProperties({ status: 'visible', color: 'red' }, '.targetElement');
29
+ * console.log(propertiesScript);
30
+ * // Output: 'document.querySelector(".targetElement").status="visible"; document.querySelector(".targetElement").color="red";'
31
+ * ```
32
+ */
33
+ declare const renderProperties: (args: Record<string, unknown>, selector: string) => string;
34
+
35
+ export { config as playwrightBaseConfig, renderAttributes, renderProperties };
@@ -0,0 +1,120 @@
1
+ const config = {
2
+ testMatch: '*.e2e.ts',
3
+ snapshotPathTemplate: '{testFileDir}/__screenshots__/{arg}-{projectName}{ext}',
4
+ /* Maximum time one test can run for. */
5
+ timeout: 30 * 1000,
6
+ expect: {
7
+ /**
8
+ * Maximum time expect() should wait for the condition to be met.
9
+ * For example in `await expect(locator).toHaveText();`
10
+ */
11
+ timeout: 5000,
12
+ },
13
+ /* Run tests in files in parallel */
14
+ fullyParallel: true,
15
+ /* Fail the build on CI if you accidentally left test.only in the source code. */
16
+ forbidOnly: !!process.env.CI,
17
+ /* Retry on CI only */
18
+ retries: process.env.CI ? 2 : 0,
19
+ /* Stop on CI on first failure */
20
+ maxFailures: process.env.CI ? 1 : 0,
21
+ /* Opt out of parallel tests on CI. */
22
+ workers: process.env.CI ? 1 : undefined,
23
+ /* Reporter to use. See https://playwright.dev/docs/test-reporters */
24
+ reporter: [
25
+ ['html', { open: 'never' }],
26
+ ['junit', { outputFile: 'results.xml' }],
27
+ ],
28
+ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
29
+ use: {
30
+ headless: true,
31
+ /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
32
+ actionTimeout: 0,
33
+ /* Base URL to use in actions like `await page.goto('/')`. */
34
+ // baseURL: 'http://localhost:3000',
35
+ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
36
+ trace: 'on-first-retry',
37
+ },
38
+ };
39
+
40
+ /**
41
+ * Check if a value is of object type.
42
+ * @param object - The value to validate.
43
+ * @returns `true` if the value is a valid object (non-null and not an array), otherwise `false`.
44
+ */
45
+ const isObject = (object) => typeof object === 'object' && !Array.isArray(object) && object !== null;
46
+
47
+ /**
48
+ * Typeguard function to check if all array items are strings.
49
+ * @param items - items to check
50
+ * @returns `true` if all items are strings
51
+ */
52
+ /**
53
+ * Validate string
54
+ * @param value - value to check
55
+ * @returns `true` if string is valid
56
+ */
57
+ const isValidString = (value) => typeof value === 'string' && value.trim() !== '';
58
+
59
+ /**
60
+ * Render attributes from props objects
61
+ * @param args - argument to render as string. ex: `{status: 'visible'}`
62
+ * @returns formated inline attributed. ex: 'status="visible"'
63
+ * @example
64
+ * ```ts
65
+ * import { renderAttributes } from '@mgdis/core-ui-helpers/dist/playwright';
66
+ *
67
+ * const attributes = renderAttributes({ status: 'visible', color: 'red' });
68
+ * console.log(attributes); // Output: 'status="visible" color="red"'
69
+ * ```
70
+ */
71
+ const renderAttributes = (args) => (isObject(args) &&
72
+ Object.keys(args)
73
+ .filter(key => ![null, undefined, false].includes(args[key]) && !['object', 'function'].includes(typeof args[key]))
74
+ .map(key => {
75
+ const attribute = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
76
+ const value = ['number', 'boolean'].includes(typeof args[key]) || isValidString(args[key]) ? String(args[key]).replace(/"/g, "'") : '';
77
+ return `${attribute}="${value}"`;
78
+ })
79
+ .join(' ')) ||
80
+ '';
81
+ /**
82
+ * Render properties from props objects.
83
+ * Insert return value in <script></script> element
84
+ * @param args - argument to render as script. ex: `{status: 'visible'}`
85
+ * @param selector - querySelector get targetted element and bind properties on it
86
+ * @returns stringified properties script
87
+ * @example
88
+ * ```ts
89
+ * import { renderProperties } from '@mgdis/core-ui-helpers/dist/playwright';
90
+ *
91
+ * const propertiesScript = renderProperties({ status: 'visible', color: 'red' }, '.targetElement');
92
+ * console.log(propertiesScript);
93
+ * // Output: 'document.querySelector(".targetElement").status="visible"; document.querySelector(".targetElement").color="red";'
94
+ * ```
95
+ */
96
+ const renderProperties = (args, selector) => {
97
+ // has generated id selector starting with "#" create charcters (ex: '#0j3xzw4w7m') make test crashed sometime with "querySelector"
98
+ // we use "getElementById" document query
99
+ const query = selector.startsWith('#') ? 'getElementById' : 'querySelector';
100
+ if (query === 'getElementById')
101
+ selector = selector.replace('#', '');
102
+ return args !== null && typeof args === 'object' && Object.keys(args).length > 0
103
+ ? `
104
+ ${Object.keys(args)
105
+ .filter(key => ['object', 'function'].includes(typeof args[key]))
106
+ .map(key => {
107
+ const value = JSON.stringify(args[key], (_key, val) => (typeof val === 'function' ? `<fn>${val}</fn>` : val));
108
+ return `document.${query}('${selector}').${key}=${value}`;
109
+ }) // stringify json AND keep function values
110
+ .join(';') // create string
111
+ .split('"<fn>') // remove fn start decorator
112
+ .join('')
113
+ .split('</fn>"')
114
+ .join('') // remove fn end decorator
115
+ }`
116
+ : '';
117
+ };
118
+
119
+ export { config as playwrightBaseConfig, renderAttributes, renderProperties };
120
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","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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