@brightspace-ui/intl 3.42.4 → 3.43.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.
- package/lib/dateTime.js +10 -6
- package/lib/dateTimeSkeleton.js +74 -0
- package/package.json +1 -1
package/lib/dateTime.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getDocumentLocaleSettings, merge } from './common.js';
|
|
2
|
+
import { formatDateTimeSkeleton } from './dateTimeSkeleton.js';
|
|
2
3
|
import { localeData } from './locale-data/current.js';
|
|
3
4
|
import { resolveSupportedLocale } from './locale-data/supported.js';
|
|
4
5
|
|
|
@@ -869,12 +870,13 @@ export function getDateTimeDescriptor() {
|
|
|
869
870
|
});
|
|
870
871
|
}
|
|
871
872
|
|
|
872
|
-
export function formatTime(date, options) {
|
|
873
|
+
export function formatTime(date, options = {}) {
|
|
874
|
+
|
|
875
|
+
if (options.skeleton) return formatDateTimeSkeleton(date, options, { date: false });
|
|
873
876
|
|
|
874
877
|
const descriptor = getDateTimeDescriptor();
|
|
875
878
|
const settings = getDocumentLocaleSettings();
|
|
876
879
|
|
|
877
|
-
options = options || {};
|
|
878
880
|
const timezone = options.timezone || settings.timezone.name;
|
|
879
881
|
const format = descriptor.formats.timeFormats[options.format]
|
|
880
882
|
|| options.format || descriptor.formats.timeFormats['short'];
|
|
@@ -966,11 +968,12 @@ export function parseTime(input, options) {
|
|
|
966
968
|
|
|
967
969
|
}
|
|
968
970
|
|
|
969
|
-
export function formatDate(date, options) {
|
|
971
|
+
export function formatDate(date, options = {}) {
|
|
972
|
+
|
|
973
|
+
if (options.skeleton) return formatDateTimeSkeleton(date, options, { time: false });
|
|
970
974
|
|
|
971
975
|
const descriptor = getDateTimeDescriptor();
|
|
972
976
|
|
|
973
|
-
options = options || {};
|
|
974
977
|
options.format = options.format || 'short';
|
|
975
978
|
|
|
976
979
|
let format = descriptor.formats.dateFormats[options.format];
|
|
@@ -1049,9 +1052,10 @@ export function parseDate(input) {
|
|
|
1049
1052
|
|
|
1050
1053
|
}
|
|
1051
1054
|
|
|
1052
|
-
export function formatDateTime(date, options) {
|
|
1055
|
+
export function formatDateTime(date, options = {}) {
|
|
1056
|
+
|
|
1057
|
+
if (options.skeleton) return formatDateTimeSkeleton(date, options);
|
|
1053
1058
|
|
|
1054
|
-
options = options || {};
|
|
1055
1059
|
const format = options.format || 'short';
|
|
1056
1060
|
|
|
1057
1061
|
switch (format) {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { getDocumentLocaleSettings } from './common.js';
|
|
2
|
+
import IntlMessageFormat from 'intl-messageformat';
|
|
3
|
+
import { localeData } from './locale-data/current.js';
|
|
4
|
+
|
|
5
|
+
const documentLocaleSettings = getDocumentLocaleSettings();
|
|
6
|
+
|
|
7
|
+
export function formatDateTimeSkeleton(dateTime, options = {}) {
|
|
8
|
+
const { date = true, time = true, locale } = arguments[2] || {};
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
timezone: timeZone =
|
|
12
|
+
options.timeZone
|
|
13
|
+
|| documentLocaleSettings.timezone.identifier
|
|
14
|
+
|| undefined,
|
|
15
|
+
timeZoneName,
|
|
16
|
+
hourCycle
|
|
17
|
+
} = options;
|
|
18
|
+
let {
|
|
19
|
+
format,
|
|
20
|
+
skeleton = '',
|
|
21
|
+
} = options;
|
|
22
|
+
|
|
23
|
+
if (!skeleton) {
|
|
24
|
+
if (date) {
|
|
25
|
+
if (!format || format === 'short') {
|
|
26
|
+
skeleton = localeData.dateFormats.short;
|
|
27
|
+
}
|
|
28
|
+
else if (format === 'medium') {
|
|
29
|
+
skeleton = localeData.dateFormats.medium;
|
|
30
|
+
}
|
|
31
|
+
else if (format === 'full') {
|
|
32
|
+
skeleton = localeData.dateFormats.full;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (time) {
|
|
36
|
+
skeleton += localeData.timeFormats.short;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!skeleton) {
|
|
40
|
+
console.warn(`"${format}" is not a valid format`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
format = null;
|
|
45
|
+
} else {
|
|
46
|
+
if (!options.forceUnsupportedFormat) {
|
|
47
|
+
throw 'Use skeletons only if standard supported formats are inarguably insufficient. You must explicitly opt in with the `forceUnsupportedFormat` option.';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (/[HhKkJYuUrM]|j{2,}/.test(skeleton)) {
|
|
51
|
+
console.warn('Replacing static hour symbols: H/h/K/k/J/jj -> j');
|
|
52
|
+
skeleton = skeleton.replace(/[HhKkJj]+/g, 'j');
|
|
53
|
+
}
|
|
54
|
+
if (!time) skeleton = skeleton.replace(/[HhJjKkmsaZzVvSsBbAOXx]/g, '');
|
|
55
|
+
if (!date) skeleton = skeleton.replace(/[GyYuUrQqMLwWdDFgEec]/g, '');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!format && !skeleton) {
|
|
59
|
+
throw 'No format or skeleton provided. Did you disable `dates` and `times`?';
|
|
60
|
+
}
|
|
61
|
+
const msg = `{dateTime, date, ${format || `::${skeleton}`}}`;
|
|
62
|
+
|
|
63
|
+
const formatters = {
|
|
64
|
+
getDateTimeFormat(locale, opts) {
|
|
65
|
+
return new Intl.DateTimeFormat(locale, {
|
|
66
|
+
...opts,
|
|
67
|
+
...{ timeZone, timeZoneName, hourCycle }
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const dateFormatter = new IntlMessageFormat(msg, locale || documentLocaleSettings.language, null, { formatters });
|
|
72
|
+
const formatted = dateFormatter.format({ dateTime });
|
|
73
|
+
return formatted;
|
|
74
|
+
}
|
package/package.json
CHANGED