@mgdis/core-ui-helpers 1.2.2 → 1.3.1
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/dist/angular/index.cjs +27 -0
- package/dist/angular/index.cjs.map +1 -0
- package/dist/angular/index.d.ts +21 -0
- package/dist/angular/index.js +25 -0
- package/dist/angular/index.js.map +1 -0
- package/dist/angular/index.umd.js +33 -0
- package/dist/angular/index.umd.js.map +1 -0
- package/dist/locale/index.cjs +190 -0
- package/dist/locale/index.cjs.map +1 -0
- package/dist/locale/index.d.ts +110 -0
- package/dist/locale/index.js +181 -0
- package/dist/locale/index.js.map +1 -0
- package/dist/locale/index.umd.js +196 -0
- package/dist/locale/index.umd.js.map +1 -0
- package/dist/playwright/index.cjs +137 -0
- package/dist/playwright/index.cjs.map +1 -0
- package/dist/playwright/index.d.ts +39 -0
- package/dist/playwright/index.js +133 -0
- package/dist/playwright/index.js.map +1 -0
- package/dist/playwright/index.umd.js +143 -0
- package/dist/playwright/index.umd.js.map +1 -0
- package/dist/stencil/index.cjs +363 -0
- package/dist/stencil/index.cjs.map +1 -0
- package/dist/stencil/index.d.ts +222 -0
- package/dist/stencil/index.js +357 -0
- package/dist/stencil/index.js.map +1 -0
- package/dist/stencil/index.umd.js +369 -0
- package/dist/stencil/index.umd.js.map +1 -0
- package/dist/storybook/index.cjs +2335 -0
- package/dist/storybook/index.cjs.map +1 -0
- package/dist/storybook/index.d.ts +85 -0
- package/dist/storybook/index.js +2329 -0
- package/dist/storybook/index.js.map +1 -0
- package/dist/storybook/index.umd.js +2341 -0
- package/dist/storybook/index.umd.js.map +1 -0
- package/dist/tests/index.cjs +315 -0
- package/dist/tests/index.cjs.map +1 -0
- package/dist/tests/index.d.ts +100 -0
- package/dist/tests/index.js +309 -0
- package/dist/tests/index.js.map +1 -0
- package/dist/tests/index.umd.js +321 -0
- package/dist/tests/index.umd.js.map +1 -0
- package/dist/utils/index.cjs +441 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.ts +265 -0
- package/dist/utils/index.js +417 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.umd.js +447 -0
- package/dist/utils/index.umd.js.map +1 -0
- package/dist/vue/index.cjs +1935 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.ts +35 -0
- package/dist/vue/index.js +1933 -0
- package/dist/vue/index.js.map +1 -0
- package/dist/vue/index.umd.js +1941 -0
- package/dist/vue/index.umd.js.map +1 -0
- package/package.json +14 -11
- package/CHANGELOG.md +0 -101
- package/json-doc.test.json +0 -264
- package/rollup.config.mjs +0 -61
- package/src/angular/index.spec.ts +0 -8
- package/src/angular/index.ts +0 -1
- package/src/angular/mg-angular-logger/index.ts +0 -1
- package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +0 -3
- package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +0 -40
- package/src/angular/mg-angular-logger/mg-angular-logger.ts +0 -24
- package/src/locale/index.spec.ts +0 -24
- package/src/locale/index.ts +0 -1
- package/src/locale/locale.spec.ts +0 -291
- package/src/locale/locale.ts +0 -201
- package/src/playwright/config/index.ts +0 -43
- package/src/playwright/index.spec.ts +0 -16
- package/src/playwright/index.ts +0 -4
- package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +0 -48
- package/src/playwright/utils/index.ts +0 -1
- package/src/playwright/utils/utils.spec.ts +0 -50
- package/src/playwright/utils/utils.ts +0 -77
- package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +0 -248
- package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +0 -414
- package/src/stencil/ide/cem.spec.ts +0 -35
- package/src/stencil/ide/cem.ts +0 -96
- package/src/stencil/ide/ide.spec.ts +0 -29
- package/src/stencil/ide/ide.ts +0 -221
- package/src/stencil/ide/index.ts +0 -2
- package/src/stencil/index.spec.ts +0 -16
- package/src/stencil/index.ts +0 -2
- package/src/stencil/normalize-boolean-attributes/index.ts +0 -1
- package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +0 -63
- package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +0 -40
- package/src/storybook/index.spec.ts +0 -19
- package/src/storybook/index.ts +0 -1
- package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +0 -329
- package/src/storybook/stencil/index.ts +0 -1
- package/src/storybook/stencil/stencil-shim.d.ts +0 -1
- package/src/storybook/stencil/stencil.conf.ts +0 -4
- package/src/storybook/stencil/stencil.spec.ts +0 -170
- package/src/storybook/stencil/stencil.ts +0 -377
- package/src/tests/index.spec.ts +0 -20
- package/src/tests/index.ts +0 -1
- package/src/tests/unit/__snapshots__/unit.spec.ts.snap +0 -52
- package/src/tests/unit/index.ts +0 -1
- package/src/tests/unit/unit.conf.ts +0 -16
- package/src/tests/unit/unit.spec.ts +0 -206
- package/src/tests/unit/unit.ts +0 -333
- package/src/utils/attributes/class-list/class-list.spec.ts +0 -36
- package/src/utils/attributes/class-list/class-list.ts +0 -51
- package/src/utils/attributes/class-list/index.ts +0 -1
- package/src/utils/attributes/id/id.spec.ts +0 -54
- package/src/utils/attributes/id/id.ts +0 -47
- package/src/utils/attributes/id/index.ts +0 -1
- package/src/utils/attributes/index.ts +0 -3
- package/src/utils/attributes/to-kebab-case/index.ts +0 -1
- package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +0 -29
- package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +0 -23
- package/src/utils/components/__snapshots__/components.spec.ts.snap +0 -313
- package/src/utils/components/components.conf.ts +0 -11
- package/src/utils/components/components.spec.ts +0 -192
- package/src/utils/components/components.ts +0 -152
- package/src/utils/components/index.ts +0 -2
- package/src/utils/date/date.spec.ts +0 -12
- package/src/utils/date/date.ts +0 -20
- package/src/utils/date/index.ts +0 -1
- package/src/utils/elements/elements.spec.ts +0 -17
- package/src/utils/elements/elements.ts +0 -18
- package/src/utils/elements/index.ts +0 -1
- package/src/utils/index.spec.ts +0 -84
- package/src/utils/index.ts +0 -8
- package/src/utils/number/index.ts +0 -1
- package/src/utils/number/number.spec.ts +0 -14
- package/src/utils/number/number.ts +0 -6
- package/src/utils/object/index.ts +0 -2
- package/src/utils/object/object.conf.ts +0 -4
- package/src/utils/object/object.spec.ts +0 -110
- package/src/utils/object/object.ts +0 -27
- package/src/utils/string/index.ts +0 -1
- package/src/utils/string/string.spec.ts +0 -54
- package/src/utils/string/string.ts +0 -39
- package/src/utils/windows/index.ts +0 -1
- package/src/utils/windows/windows.spec.ts +0 -69
- package/src/utils/windows/windows.ts +0 -50
- package/src/vue/index.spec.ts +0 -8
- package/src/vue/index.ts +0 -1
- package/src/vue/mg-model/index.ts +0 -1
- package/src/vue/mg-model/mg-model.conf.ts +0 -6
- package/src/vue/mg-model/mg-model.spec.ts +0 -50
- package/src/vue/mg-model/mg-model.ts +0 -55
- package/tsconfig.json +0 -17
- package/typedoc.json +0 -14
- package/vitest.config.mts +0 -13
|
@@ -0,0 +1,27 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;;AAErC,QAAA,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YACzG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;AAChC,IAAA,CAAC;AACH;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 };
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;;AAErC,QAAA,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YACzG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;AAChC,IAAA,CAAC;AACH;;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;;IAErC,QAAA,IAAI,MAAM,CAAC,KAAK,KAAK,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;gBACzG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;IAChC,IAAA,CAAC;IACH;;;;"}
|
|
@@ -0,0 +1,190 @@
|
|
|
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)
|
|
161
|
+
? ''
|
|
162
|
+
: new Intl.DateTimeFormat(locale, config).format(new Date(date));
|
|
163
|
+
/**
|
|
164
|
+
* Get Intl object
|
|
165
|
+
* @param messages - locales to render in object format. `ex: { en: { porp: "test" }, fr: { porp: "test" }}`.
|
|
166
|
+
* @param defaultLocale - fallback locale to render. `ex: 'en'`.
|
|
167
|
+
* @returns from the element passed in return function you will get the matching messages object
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* import en from './en/messages.json';
|
|
171
|
+
* import fr from './fr/messages.json';
|
|
172
|
+
* import { defineLocales } from '@mgdis/core-ui-helpers/dist/utils';
|
|
173
|
+
*
|
|
174
|
+
* const defaultLocale = 'en';
|
|
175
|
+
* const messages = { en, fr };
|
|
176
|
+
*
|
|
177
|
+
* export const initLocales = defineLocales(messages, defaultLocale);
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
const defineLocales = (messages, defaultLocale) => (element) => localeMessages(element, messages, defaultLocale);
|
|
181
|
+
|
|
182
|
+
exports.defineLocales = defineLocales;
|
|
183
|
+
exports.localeByte = localeByte;
|
|
184
|
+
exports.localeCurrency = localeCurrency;
|
|
185
|
+
exports.localeDate = localeDate;
|
|
186
|
+
exports.localeDatePattern = localeDatePattern;
|
|
187
|
+
exports.localeNumber = localeNumber;
|
|
188
|
+
exports.localePercent = localePercent;
|
|
189
|
+
exports.localeUnit = localeUnit;
|
|
190
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["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M,UAAU,GAAG,CAAC,IAAwB,EAAE,MAAc,EAAE,MAAmC,KACtG,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI;AAC9D,MAAE;AACF,MAAE,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC;AAEnE;;;;;;;;;;;;;;;;AAgBG;AACI,MAAM,aAAa,GACxB,CAAC,QAAoB,EAAE,aAA0B,KACjD,CAAC,OAAoB,KACnB,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,aAAa;;;;;;;;;"}
|
|
@@ -0,0 +1,110 @@
|
|
|
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 };
|
|
@@ -0,0 +1,181 @@
|
|
|
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)
|
|
159
|
+
? ''
|
|
160
|
+
: 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
|
+
export { defineLocales, localeByte, localeCurrency, localeDate, localeDatePattern, localeNumber, localePercent, localeUnit };
|
|
181
|
+
//# 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;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,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC;AACpC;AAEA;;;;;;;AAOG;AACH,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