@mgdis/core-ui-helpers 1.2.0 → 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.
Files changed (89) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +8 -8
  3. package/rollup.config.mjs +3 -3
  4. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +1 -1
  5. package/src/angular/mg-angular-logger/mg-angular-logger.ts +2 -1
  6. package/src/locale/index.spec.ts +10 -1
  7. package/src/locale/locale.spec.ts +43 -18
  8. package/src/locale/locale.ts +13 -5
  9. package/src/playwright/utils/utils.spec.ts +16 -9
  10. package/src/playwright/utils/utils.ts +14 -6
  11. package/src/stencil/ide/cem.spec.ts +4 -4
  12. package/src/stencil/ide/cem.ts +9 -9
  13. package/src/stencil/ide/ide.ts +26 -26
  14. package/src/storybook/stencil/stencil.spec.ts +30 -6
  15. package/src/storybook/stencil/stencil.ts +24 -13
  16. package/src/tests/index.spec.ts +7 -1
  17. package/src/tests/unit/unit.conf.ts +9 -2
  18. package/src/tests/unit/unit.spec.ts +103 -61
  19. package/src/tests/unit/unit.ts +38 -11
  20. package/src/utils/attributes/id/id.spec.ts +9 -6
  21. package/src/utils/attributes/id/id.ts +3 -2
  22. package/src/utils/components/components.spec.ts +6 -6
  23. package/src/utils/components/components.ts +19 -5
  24. package/src/utils/elements/elements.ts +4 -2
  25. package/src/utils/number/number.spec.ts +2 -2
  26. package/src/utils/object/object.spec.ts +2 -2
  27. package/src/utils/object/object.ts +2 -1
  28. package/src/utils/string/string.spec.ts +12 -5
  29. package/src/utils/string/string.ts +2 -1
  30. package/src/utils/windows/windows.spec.ts +6 -2
  31. package/src/vue/mg-model/mg-model.ts +7 -2
  32. package/vitest.config.mts +1 -1
  33. package/.turbo/turbo-build.log +0 -52
  34. package/dist/angular/index.cjs +0 -27
  35. package/dist/angular/index.cjs.map +0 -1
  36. package/dist/angular/index.d.ts +0 -21
  37. package/dist/angular/index.js +0 -25
  38. package/dist/angular/index.js.map +0 -1
  39. package/dist/angular/index.umd.js +0 -33
  40. package/dist/angular/index.umd.js.map +0 -1
  41. package/dist/locale/index.cjs +0 -188
  42. package/dist/locale/index.cjs.map +0 -1
  43. package/dist/locale/index.d.ts +0 -110
  44. package/dist/locale/index.js +0 -179
  45. package/dist/locale/index.js.map +0 -1
  46. package/dist/locale/index.umd.js +0 -194
  47. package/dist/locale/index.umd.js.map +0 -1
  48. package/dist/playwright/index.cjs +0 -130
  49. package/dist/playwright/index.cjs.map +0 -1
  50. package/dist/playwright/index.d.ts +0 -39
  51. package/dist/playwright/index.js +0 -126
  52. package/dist/playwright/index.js.map +0 -1
  53. package/dist/playwright/index.umd.js +0 -136
  54. package/dist/playwright/index.umd.js.map +0 -1
  55. package/dist/stencil/index.cjs +0 -363
  56. package/dist/stencil/index.cjs.map +0 -1
  57. package/dist/stencil/index.d.ts +0 -222
  58. package/dist/stencil/index.js +0 -357
  59. package/dist/stencil/index.js.map +0 -1
  60. package/dist/stencil/index.umd.js +0 -369
  61. package/dist/stencil/index.umd.js.map +0 -1
  62. package/dist/storybook/index.cjs +0 -2333
  63. package/dist/storybook/index.cjs.map +0 -1
  64. package/dist/storybook/index.d.ts +0 -85
  65. package/dist/storybook/index.js +0 -2327
  66. package/dist/storybook/index.js.map +0 -1
  67. package/dist/storybook/index.umd.js +0 -2339
  68. package/dist/storybook/index.umd.js.map +0 -1
  69. package/dist/tests/index.cjs +0 -300
  70. package/dist/tests/index.cjs.map +0 -1
  71. package/dist/tests/index.d.ts +0 -100
  72. package/dist/tests/index.js +0 -294
  73. package/dist/tests/index.js.map +0 -1
  74. package/dist/tests/index.umd.js +0 -306
  75. package/dist/tests/index.umd.js.map +0 -1
  76. package/dist/utils/index.cjs +0 -433
  77. package/dist/utils/index.cjs.map +0 -1
  78. package/dist/utils/index.d.ts +0 -265
  79. package/dist/utils/index.js +0 -409
  80. package/dist/utils/index.js.map +0 -1
  81. package/dist/utils/index.umd.js +0 -439
  82. package/dist/utils/index.umd.js.map +0 -1
  83. package/dist/vue/index.cjs +0 -1934
  84. package/dist/vue/index.cjs.map +0 -1
  85. package/dist/vue/index.d.ts +0 -35
  86. package/dist/vue/index.js +0 -1932
  87. package/dist/vue/index.js.map +0 -1
  88. package/dist/vue/index.umd.js +0 -1940
  89. package/dist/vue/index.umd.js.map +0 -1
@@ -4,7 +4,8 @@
4
4
  * @param tagNames - allowed tag names list
5
5
  * @returns `true` if element tagName is in the tagNames list
6
6
  */
7
- export const isTagName = (element: Element, tagNames: string[]): boolean => tagNames.includes(element?.tagName.toLowerCase());
7
+ export const isTagName = (element: Element, tagNames: string[]): boolean =>
8
+ tagNames.includes(element?.tagName.toLowerCase());
8
9
 
9
10
  /**
10
11
  * CSS selector to select focusable elements.
@@ -13,4 +14,5 @@ export const isTagName = (element: Element, tagNames: string[]): boolean => tagN
13
14
  * const allFocusableElements: HTMLElement[] = Array.from(this.element.querySelectorAll(focusableElements));
14
15
  * ```
15
16
  */
16
- export const focusableElements = 'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"]), [identifier], mg-button';
17
+ export const focusableElements =
18
+ 'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"]), [identifier], mg-button';
@@ -3,11 +3,11 @@ import { isValidNumber } from './number';
3
3
 
4
4
  describe('number', () => {
5
5
  describe('isValidNumber', () => {
6
- test.each(['', ' ', null, undefined, '1', {}, []])('Should return "false" for invalid values', value => {
6
+ test.each(['', ' ', null, undefined, '1', {}, []])('Should return "false" for invalid values', (value) => {
7
7
  expect(isValidNumber(value)).toEqual(false);
8
8
  });
9
9
 
10
- test.each([1, 1.0])('Should return "true" for valid value', value => {
10
+ test.each([1, 1.0])('Should return "true" for valid value', (value) => {
11
11
  expect(isValidNumber(value)).toEqual(true);
12
12
  });
13
13
  });
@@ -3,12 +3,12 @@ import { getObjectValueFromKey, isObject } from './object';
3
3
 
4
4
  describe('object', () => {
5
5
  describe('isObject', () => {
6
- test.each([{}, new Date()])('Should validate an object', value => {
6
+ test.each([{}, new Date()])('Should validate an object', (value) => {
7
7
  const res = isObject(value);
8
8
  expect(res).toEqual(true);
9
9
  });
10
10
 
11
- test.each([undefined, null, '', 'batman', []])('Should NOT validate %s', value => {
11
+ test.each([undefined, null, '', 'batman', []])('Should NOT validate %s', (value) => {
12
12
  const res = isObject(value);
13
13
  expect(res).toEqual(false);
14
14
  });
@@ -3,7 +3,8 @@
3
3
  * @param object - The value to validate.
4
4
  * @returns `true` if the value is a valid object (non-null and not an array), otherwise `false`.
5
5
  */
6
- export const isObject = <T>(object: unknown): object is T => typeof object === 'object' && !Array.isArray(object) && object !== null;
6
+ export const isObject = <T>(object: unknown): object is T =>
7
+ typeof object === 'object' && !Array.isArray(object) && object !== null;
7
8
 
8
9
  /**
9
10
  * Get object value from key
@@ -12,19 +12,26 @@ describe('string', () => {
12
12
  });
13
13
 
14
14
  describe('isValidString', () => {
15
- test.each(['', ' ', null, undefined, 1, {}, []])('Should return "false" for invalid values', value => {
15
+ test.each(['', ' ', null, undefined, 1, {}, []])('Should return "false" for invalid values', (value) => {
16
16
  expect(isValidString(value)).toEqual(false);
17
17
  });
18
18
 
19
- test.each(['batman', 'batman '])('Should return "true" for valid value', value => {
19
+ test.each(['batman', 'batman '])('Should return "true" for valid value', (value) => {
20
20
  expect(isValidString(value)).toEqual(true);
21
21
  });
22
22
  });
23
23
 
24
24
  describe('toString', () => {
25
- test.each([undefined, null, 1, 'string', '', [undefined], [{ value: null }], {}, ['string'], { hello: 'batman' }])('Should return stringified value, original %s', value => {
26
- expect(toString(value)).toEqual(typeof value === 'object' || (Array.isArray(value) && typeof value !== 'string') ? JSON.stringify(value) : `${value}`);
27
- });
25
+ test.each([undefined, null, 1, 'string', '', [undefined], [{ value: null }], {}, ['string'], { hello: 'batman' }])(
26
+ 'Should return stringified value, original %s',
27
+ (value) => {
28
+ expect(toString(value)).toEqual(
29
+ typeof value === 'object' || (Array.isArray(value) && typeof value !== 'string')
30
+ ? JSON.stringify(value)
31
+ : `${value}`
32
+ );
33
+ }
34
+ );
28
35
  });
29
36
 
30
37
  describe('cleanString', () => {
@@ -3,7 +3,8 @@
3
3
  * @param items - items to check
4
4
  * @returns `true` if all items are strings
5
5
  */
6
- export const allItemsAreString = (items: unknown): items is string[] => Array.isArray(items) && items.every(item => typeof item === 'string');
6
+ export const allItemsAreString = (items: unknown): items is string[] =>
7
+ Array.isArray(items) && items.every((item) => typeof item === 'string');
7
8
 
8
9
  /**
9
10
  * Validate string
@@ -30,7 +30,9 @@ describe('windows', () => {
30
30
 
31
31
  test('Should return the window with a parent', () => {
32
32
  mockWindowFramesLength.mockReturnValue(0);
33
- const spyWindowSelf = vi.spyOn(window, 'self', 'get').mockImplementationOnce(() => ({}) as Window & typeof globalThis);
33
+ const spyWindowSelf = vi
34
+ .spyOn(window, 'self', 'get')
35
+ .mockImplementationOnce(() => ({}) as Window & typeof globalThis);
34
36
  const spyWindowParent = vi.spyOn(window, 'parent', 'get').mockImplementationOnce(() => window);
35
37
 
36
38
  const windows = getWindows(window);
@@ -49,7 +51,9 @@ describe('windows', () => {
49
51
  test('Should throw an error when cannot access top parent window', () => {
50
52
  mockWindowFramesLength.mockReturnValue(0);
51
53
  const spyConsole = vi.spyOn(console, 'error');
52
- const spyWindowSelf = vi.spyOn(window, 'self', 'get').mockImplementationOnce(() => ({}) as Window & typeof globalThis);
54
+ const spyWindowSelf = vi
55
+ .spyOn(window, 'self', 'get')
56
+ .mockImplementationOnce(() => ({}) as Window & typeof globalThis);
53
57
  const spyWindowParent = vi.spyOn(window, 'parent', 'get').mockImplementationOnce(() => {
54
58
  throw new Error('non');
55
59
  });
@@ -32,8 +32,13 @@ export const mgModel = {
32
32
  Vue.directive('mg-model', <Directive<HTMLInputElement, string>>{
33
33
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
34
  bind(el: HTMLInputElement, binding: any, vnode: VNode) {
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- const valueChangeHandler = (event: Event) => set(vnode.context as any, binding.expression, (event as unknown as { target: EventTarget & { value: unknown } }).target.value);
35
+ const valueChangeHandler = (event: Event) =>
36
+ set(
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ vnode.context as any,
39
+ binding.expression,
40
+ (event as unknown as { target: EventTarget & { value: unknown } }).target.value
41
+ );
37
42
  wm.set(el, valueChangeHandler);
38
43
  el.value = binding.value ?? '';
39
44
  el.addEventListener('value-change', valueChangeHandler);
package/vitest.config.mts CHANGED
@@ -9,5 +9,5 @@ export default mergeConfig(
9
9
  exclude: ['src/**/config'],
10
10
  },
11
11
  },
12
- }),
12
+ })
13
13
  );
@@ -1,52 +0,0 @@
1
-
2
- > @mgdis/core-ui-helpers@1.2.0 build /builds/core/core-ui/core-ui/packages/helpers
3
- > rollup -c ./rollup.config.mjs
4
-
5
- 
6
- src/angular/index.ts → dist/angular/index.js, dist/angular/index.cjs, dist/angular/index.umd.js...
7
- created dist/angular/index.js, dist/angular/index.cjs, dist/angular/index.umd.js in 1.7s
8
- 
9
- src/angular/index.ts → dist/angular/index.d.ts...
10
- created dist/angular/index.d.ts in 610ms
11
- 
12
- src/locale/index.ts → dist/locale/index.js, dist/locale/index.cjs, dist/locale/index.umd.js...
13
- created dist/locale/index.js, dist/locale/index.cjs, dist/locale/index.umd.js in 1.3s
14
- 
15
- src/locale/index.ts → dist/locale/index.d.ts...
16
- created dist/locale/index.d.ts in 573ms
17
- 
18
- src/playwright/index.ts → dist/playwright/index.js, dist/playwright/index.cjs, dist/playwright/index.umd.js...
19
- created dist/playwright/index.js, dist/playwright/index.cjs, dist/playwright/index.umd.js in 1.3s
20
- 
21
- src/playwright/index.ts → dist/playwright/index.d.ts...
22
- created dist/playwright/index.d.ts in 570ms
23
- 
24
- src/stencil/index.ts → dist/stencil/index.js, dist/stencil/index.cjs, dist/stencil/index.umd.js...
25
- created dist/stencil/index.js, dist/stencil/index.cjs, dist/stencil/index.umd.js in 1.3s
26
- 
27
- src/stencil/index.ts → dist/stencil/index.d.ts...
28
- created dist/stencil/index.d.ts in 614ms
29
- 
30
- src/storybook/index.ts → dist/storybook/index.js, dist/storybook/index.cjs, dist/storybook/index.umd.js...
31
- created dist/storybook/index.js, dist/storybook/index.cjs, dist/storybook/index.umd.js in 1.3s
32
- 
33
- src/storybook/index.ts → dist/storybook/index.d.ts...
34
- created dist/storybook/index.d.ts in 591ms
35
- 
36
- src/tests/index.ts → dist/tests/index.js, dist/tests/index.cjs, dist/tests/index.umd.js...
37
- created dist/tests/index.js, dist/tests/index.cjs, dist/tests/index.umd.js in 1.1s
38
- 
39
- src/tests/index.ts → dist/tests/index.d.ts...
40
- created dist/tests/index.d.ts in 556ms
41
- 
42
- src/utils/index.ts → dist/utils/index.js, dist/utils/index.cjs, dist/utils/index.umd.js...
43
- created dist/utils/index.js, dist/utils/index.cjs, dist/utils/index.umd.js in 1.1s
44
- 
45
- src/utils/index.ts → dist/utils/index.d.ts...
46
- created dist/utils/index.d.ts in 576ms
47
- 
48
- src/vue/index.ts → dist/vue/index.js, dist/vue/index.cjs, dist/vue/index.umd.js...
49
- created dist/vue/index.js, dist/vue/index.cjs, dist/vue/index.umd.js in 1.3s
50
- 
51
- src/vue/index.ts → dist/vue/index.d.ts...
52
- created dist/vue/index.d.ts in 578ms
@@ -1,27 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Set window logger custom rules for angular and mg-components
5
- * @param config - logger configuration object
6
- * @example
7
- * ```js
8
- * // main.js
9
- * import { defineCustomElements } from '@mgdis/mg-components/loader';
10
- * import { setMgAngularLogger } from '@mgdis/core-ui-helpers/dist/angular';
11
- *
12
- * // need to be set before `defineCustomElements` call
13
- * setMgAngularLogger({level: 'error'});
14
- * defineCustomElements();
15
- * ```
16
- */
17
- const setMgAngularLogger = (config = { level: 'log' }) => {
18
- const error = console.error;
19
- console.error = (...args) => {
20
- // filter error when it come from a mg-components component error
21
- if (config.level === 'error' || !args.some(arg => arg instanceof Error && arg.message.startsWith('<mg-')))
22
- error.call(console, ...args);
23
- };
24
- };
25
-
26
- exports.setMgAngularLogger = setMgAngularLogger;
27
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/angular/mg-angular-logger/mg-angular-logger.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;AAaG;AACI,MAAM,kBAAkB,GAAG,CAAC,MAAA,GAAqB,EAAE,KAAK,EAAE,KAAK,EAAE,KAAU;AAChF,IAAA,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK;AAC3B,IAAA,OAAO,CAAC,KAAK,GAAG,CAAC,GAAG,IAAe,KAAI;;QAErC,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;AACzI,IAAA,CAAC;AACH;;;;"}
@@ -1,21 +0,0 @@
1
- type ConfigType = {
2
- level: 'log' | 'error';
3
- };
4
-
5
- /**
6
- * Set window logger custom rules for angular and mg-components
7
- * @param config - logger configuration object
8
- * @example
9
- * ```js
10
- * // main.js
11
- * import { defineCustomElements } from '@mgdis/mg-components/loader';
12
- * import { setMgAngularLogger } from '@mgdis/core-ui-helpers/dist/angular';
13
- *
14
- * // need to be set before `defineCustomElements` call
15
- * setMgAngularLogger({level: 'error'});
16
- * defineCustomElements();
17
- * ```
18
- */
19
- declare const setMgAngularLogger: (config?: ConfigType) => void;
20
-
21
- export { setMgAngularLogger };
@@ -1,25 +0,0 @@
1
- /**
2
- * Set window logger custom rules for angular and mg-components
3
- * @param config - logger configuration object
4
- * @example
5
- * ```js
6
- * // main.js
7
- * import { defineCustomElements } from '@mgdis/mg-components/loader';
8
- * import { setMgAngularLogger } from '@mgdis/core-ui-helpers/dist/angular';
9
- *
10
- * // need to be set before `defineCustomElements` call
11
- * setMgAngularLogger({level: 'error'});
12
- * defineCustomElements();
13
- * ```
14
- */
15
- const setMgAngularLogger = (config = { level: 'log' }) => {
16
- const error = console.error;
17
- console.error = (...args) => {
18
- // filter error when it come from a mg-components component error
19
- if (config.level === 'error' || !args.some(arg => arg instanceof Error && arg.message.startsWith('<mg-')))
20
- error.call(console, ...args);
21
- };
22
- };
23
-
24
- export { setMgAngularLogger };
25
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../../src/angular/mg-angular-logger/mg-angular-logger.ts"],"sourcesContent":[null],"names":[],"mappings":"AAEA;;;;;;;;;;;;;AAaG;AACI,MAAM,kBAAkB,GAAG,CAAC,MAAA,GAAqB,EAAE,KAAK,EAAE,KAAK,EAAE,KAAU;AAChF,IAAA,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK;AAC3B,IAAA,OAAO,CAAC,KAAK,GAAG,CAAC,GAAG,IAAe,KAAI;;QAErC,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;AACzI,IAAA,CAAC;AACH;;;;"}
@@ -1,33 +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.angular = {})));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- /**
8
- * Set window logger custom rules for angular and mg-components
9
- * @param config - logger configuration object
10
- * @example
11
- * ```js
12
- * // main.js
13
- * import { defineCustomElements } from '@mgdis/mg-components/loader';
14
- * import { setMgAngularLogger } from '@mgdis/core-ui-helpers/dist/angular';
15
- *
16
- * // need to be set before `defineCustomElements` call
17
- * setMgAngularLogger({level: 'error'});
18
- * defineCustomElements();
19
- * ```
20
- */
21
- const setMgAngularLogger = (config = { level: 'log' }) => {
22
- const error = console.error;
23
- console.error = (...args) => {
24
- // filter error when it come from a mg-components component error
25
- if (config.level === 'error' || !args.some(arg => arg instanceof Error && arg.message.startsWith('<mg-')))
26
- error.call(console, ...args);
27
- };
28
- };
29
-
30
- exports.setMgAngularLogger = setMgAngularLogger;
31
-
32
- }));
33
- //# sourceMappingURL=index.umd.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.umd.js","sources":["../../src/angular/mg-angular-logger/mg-angular-logger.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;IAEA;;;;;;;;;;;;;IAaG;AACI,UAAM,kBAAkB,GAAG,CAAC,MAAA,GAAqB,EAAE,KAAK,EAAE,KAAK,EAAE,KAAU;IAChF,IAAA,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK;IAC3B,IAAA,OAAO,CAAC,KAAK,GAAG,CAAC,GAAG,IAAe,KAAI;;YAErC,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;gBAAE,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;IACzI,IAAA,CAAC;IACH;;;;;;;;"}
@@ -1,188 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Date RegExp, usefull to test if string is a follow the date pattern
5
- * @example
6
- * ```ts
7
- * dateRegExp.test('mystring') // false
8
- * dateRegExp.test('2020-12-31') // true
9
- * ```
10
- */
11
- const dateRegExp = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
12
-
13
- /**
14
- * Gets the date pattern based on the specified locale.
15
- * @param locale - the locale to refer to
16
- * @returns date pattern
17
- * @example
18
- * ```ts
19
- * localeDatePattern('fr') // 'dd/mm/yyyy'
20
- * ```
21
- */
22
- const localeDatePattern = (locale) => {
23
- const year = { value: '2023', pattern: 'yyyy' };
24
- const month = { value: '12', pattern: 'mm' };
25
- const day = { value: '24', pattern: 'dd' };
26
- return localeDate([year.value, month.value, day.value].join('-'), locale, { timeZone: 'UTC' })
27
- .replace(year.value, year.pattern)
28
- .replace(month.value, month.pattern)
29
- .replace(day.value, day.pattern);
30
- };
31
- /**
32
- * Get locale and messages
33
- * We load the defined locale but for now we only support the first subtag for messages
34
- * @param element - element we need to get the language
35
- * @param messages - messages to use
36
- * @param defaultLocale - default messages locale
37
- * @returns messages object
38
- */
39
- const localeMessages = (element, messages, defaultLocale) => {
40
- // Get local
41
- const closestLangAttribute = element.closest('[lang]');
42
- const closestLang = Intl.NumberFormat.supportedLocalesOf(closestLangAttribute?.lang);
43
- const locale = closestLang.length > 0 && typeof closestLang[0] === 'string' ? closestLang[0] : navigator.language || defaultLocale;
44
- // Only keep first subtag
45
- const localeSubtag = locale.split('-').shift();
46
- // If messages is empty, return a default object
47
- if (Object.keys(messages).length === 0) {
48
- return {
49
- locale,
50
- messages: { lang: defaultLocale },
51
- };
52
- }
53
- // Return
54
- return {
55
- locale,
56
- messages: (messages[localeSubtag] ?? messages[defaultLocale] ?? { lang: defaultLocale }),
57
- };
58
- };
59
- /**
60
- * Format number to the locale currency
61
- * @param number - number to format
62
- * @param locale - locale to apply
63
- * @param currency - currency to apply
64
- * @returns formatted currency
65
- * @example
66
- * ```ts
67
- * localeCurrency(1234567890.12, 'fr', 'EUR') // '1 234 567 890,12\xa0€'
68
- * ```
69
- */
70
- const localeCurrency = (number, locale, currency) => new Intl.NumberFormat(locale, { style: 'currency', currency }).format(number);
71
- /**
72
- * Format number to locale
73
- * @param number - number to format
74
- * @param locale - locale to apply
75
- * @param decimalLength - decimal length to apply
76
- * @returns formatted number
77
- * @example
78
- * ```ts
79
- * localeNumber(1234567890.12, 'fr') // 1 234 567 890,12
80
- * ```
81
- */
82
- const localeNumber = (number, locale, decimalLength = 0) => new Intl.NumberFormat(locale, { minimumFractionDigits: decimalLength }).format(Number(number));
83
- /**
84
- * Convert bytes number to locale string representation
85
- * @param number - size in bytes
86
- * @param locale - locale to apply
87
- * @returns bytes in locale string format
88
- */
89
- const localeByte = (number, locale) => {
90
- if (typeof number !== 'number' || Number.isNaN(number) || number < 0) {
91
- throw new Error('localeByte - size must be a positive number.');
92
- }
93
- const base = 1024;
94
- const units = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'];
95
- // find appropriate unit base on size and base power
96
- const unitIndex = units.findIndex((_, key) => number < Math.pow(base, key + 1));
97
- // convert number to string with unit
98
- return Intl.NumberFormat(locale, {
99
- minimumFractionDigits: 0,
100
- maximumFractionDigits: 2,
101
- unit: units[unitIndex],
102
- unitDisplay: units[unitIndex] === 'byte' ? 'long' : 'short',
103
- style: 'unit',
104
- }).format(number / Math.pow(base, unitIndex));
105
- };
106
- /**
107
- * Format number as percentage based on locale
108
- * @param number - number to format
109
- * @param locale - locale to apply
110
- * @param decimalLength - decimal length to apply
111
- * @returns formatted percentage
112
- * @example
113
- * ```ts
114
- * localePercent(0.42, 'fr', 2) // '42,00 %'
115
- * localePercent(0.42, 'en', 2) // '42.00%'
116
- * ```
117
- */
118
- const localePercent = (number, locale, decimalLength = 0) => {
119
- return new Intl.NumberFormat(locale, {
120
- style: 'percent',
121
- minimumFractionDigits: decimalLength,
122
- maximumFractionDigits: decimalLength,
123
- }).format(number);
124
- };
125
- /**
126
- * Format number with standardized unit based on locale using Intl unit formatting
127
- * @param number - number to format
128
- * @param locale - locale to apply
129
- * @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
130
- * @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
131
- * @param decimalLength - decimal length to apply
132
- * @returns formatted number with localized unit
133
- * @example
134
- * ```ts
135
- * localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
136
- * localeUnit(23, 'fr', 'celsius') // '23 °C'
137
- * localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
138
- * ```
139
- */
140
- const localeUnit = (number, locale, unit, unitDisplay = 'short', decimalLength = 0) => {
141
- return new Intl.NumberFormat(locale, {
142
- style: 'unit',
143
- unit,
144
- unitDisplay,
145
- minimumFractionDigits: decimalLength,
146
- maximumFractionDigits: decimalLength,
147
- }).format(number);
148
- };
149
- /**
150
- * Locale date format
151
- * @param date - date to format
152
- * @param locale - locale to apply
153
- * @param config - DateTimeFormatOptions object to apply
154
- * @returns formatted date
155
- * @example
156
- * ```ts
157
- * localeDate('2022-06-02', 'fr') // '02/06/2022'
158
- * ```
159
- */
160
- const localeDate = (date, locale, config) => typeof date !== 'string' || date === '' || !dateRegExp.test(date) ? '' : new Intl.DateTimeFormat(locale, config).format(new Date(date));
161
- /**
162
- * Get Intl object
163
- * @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
164
- * @param defaultLocale - fallback locale to render. `ex: 'en'`.
165
- * @returns from the element passed in return function you will get the matching messages object
166
- * @example
167
- * ```ts
168
- * import en from './en/messages.json';
169
- * import fr from './fr/messages.json';
170
- * import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
171
- *
172
- * const defaultLocale = 'en';
173
- * const messages = { en, fr };
174
- *
175
- * export const initLocales = defineLocales(messages, defaultLocale);
176
- * ```
177
- */
178
- const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
179
-
180
- exports.defineLocales = defineLocales;
181
- exports.localeByte = localeByte;
182
- exports.localeCurrency = localeCurrency;
183
- exports.localeDate = localeDate;
184
- exports.localeDatePattern = localeDatePattern;
185
- exports.localeNumber = localeNumber;
186
- exports.localePercent = localePercent;
187
- exports.localeUnit = localeUnit;
188
- //# sourceMappingUR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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;;;;;;;;;;;"}
@@ -1,110 +0,0 @@
1
- /**
2
- * Define json object type
3
- */
4
- type ObjectType = Record<string, unknown>;
5
-
6
- /**
7
- * Gets the date pattern based on the specified locale.
8
- * @param locale - the locale to refer to
9
- * @returns date pattern
10
- * @example
11
- * ```ts
12
- * localeDatePattern('fr') // 'dd/mm/yyyy'
13
- * ```
14
- */
15
- declare const localeDatePattern: (locale: string) => string;
16
- /**
17
- * Format number to the locale currency
18
- * @param number - number to format
19
- * @param locale - locale to apply
20
- * @param currency - currency to apply
21
- * @returns formatted currency
22
- * @example
23
- * ```ts
24
- * localeCurrency(1234567890.12, 'fr', 'EUR') // '1 234 567 890,12\xa0€'
25
- * ```
26
- */
27
- declare const localeCurrency: (number: number, locale: string, currency: string) => string;
28
- /**
29
- * Format number to locale
30
- * @param number - number to format
31
- * @param locale - locale to apply
32
- * @param decimalLength - decimal length to apply
33
- * @returns formatted number
34
- * @example
35
- * ```ts
36
- * localeNumber(1234567890.12, 'fr') // 1 234 567 890,12
37
- * ```
38
- */
39
- declare const localeNumber: (number: number, locale: string, decimalLength?: number) => string;
40
- /**
41
- * Convert bytes number to locale string representation
42
- * @param number - size in bytes
43
- * @param locale - locale to apply
44
- * @returns bytes in locale string format
45
- */
46
- declare const localeByte: (number: number, locale: string) => string;
47
- /**
48
- * Format number as percentage based on locale
49
- * @param number - number to format
50
- * @param locale - locale to apply
51
- * @param decimalLength - decimal length to apply
52
- * @returns formatted percentage
53
- * @example
54
- * ```ts
55
- * localePercent(0.42, 'fr', 2) // '42,00 %'
56
- * localePercent(0.42, 'en', 2) // '42.00%'
57
- * ```
58
- */
59
- declare const localePercent: (number: number, locale: string, decimalLength?: number) => string;
60
- /**
61
- * Format number with standardized unit based on locale using Intl unit formatting
62
- * @param number - number to format
63
- * @param locale - locale to apply
64
- * @param unit - standardized unit (e.g., 'kilometer', 'kilogram', 'celsius')
65
- * @param unitDisplay - how to display the unit ('short', 'long', 'narrow')
66
- * @param decimalLength - decimal length to apply
67
- * @returns formatted number with localized unit
68
- * @example
69
- * ```ts
70
- * localeUnit(1234567890.12, 'fr', 'kilometer') // '1 234 567 890,12 km'
71
- * localeUnit(23, 'fr', 'celsius') // '23 °C'
72
- * localeUnit(10, 'fr', 'kilometer', 0, 'long') // '10 kilomètres'
73
- * ```
74
- */
75
- declare const localeUnit: (number: number, locale: string, unit: Intl.NumberFormatOptions["unit"], unitDisplay?: Intl.NumberFormatOptions["unitDisplay"], decimalLength?: number) => string;
76
- /**
77
- * Locale date format
78
- * @param date - date to format
79
- * @param locale - locale to apply
80
- * @param config - DateTimeFormatOptions object to apply
81
- * @returns formatted date
82
- * @example
83
- * ```ts
84
- * localeDate('2022-06-02', 'fr') // '02/06/2022'
85
- * ```
86
- */
87
- declare const localeDate: (date: string | undefined, locale: string, config?: Intl.DateTimeFormatOptions) => string;
88
- /**
89
- * Get Intl object
90
- * @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
91
- * @param defaultLocale - fallback locale to render. `ex: 'en'`.
92
- * @returns from the element passed in return function you will get the matching messages object
93
- * @example
94
- * ```ts
95
- * import en from './en/messages.json';
96
- * import fr from './fr/messages.json';
97
- * import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
98
- *
99
- * const defaultLocale = 'en';
100
- * const messages = { en, fr };
101
- *
102
- * export const initLocales = defineLocales(messages, defaultLocale);
103
- * ```
104
- */
105
- declare const defineLocales: (messages: ObjectType, defaultLocale: "fr" | "en") => (element: HTMLElement) => {
106
- locale: string;
107
- messages: ObjectType;
108
- };
109
-
110
- export { defineLocales, localeByte, localeCurrency, localeDate, localeDatePattern, localeNumber, localePercent, localeUnit };