UNPKG

3.4 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.NumberLocalizer = exports.DateLocalizer = void 0;
5
6var _dates = _interopRequireDefault(require("./dates"));
7
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
10// assumes both are supported or none
11let supportStyles = false;
12new Intl.DateTimeFormat(undefined, {
13 // @ts-ignore
14 get dateStyle() {
15 supportStyles = true;
16 }
17
18});
19const dateShort = {
20 day: 'numeric',
21 month: 'numeric',
22 year: 'numeric'
23};
24const timeShort = {
25 hour: 'numeric',
26 minute: 'numeric'
27};
28
29const getFormatter = (culture, options) => Intl.DateTimeFormat(culture, options).format;
30/**
31 * A `react-widgets` Localizer using native `Intl` APIs.
32 *
33 */
34
35
36class IntlDateLocalizer {
37 constructor({
38 culture = undefined,
39 firstOfWeek = 0
40 } = {}) {
41 this.culture = culture;
42
43 this.firstOfWeek = () => firstOfWeek;
44
45 function normalizeFormat(date, format) {
46 return typeof format === 'function' ? format(date, culture) : date.toLocaleString(culture, format);
47 }
48
49 const formats = {
50 date: getFormatter(culture, supportStyles ? {
51 dateStyle: 'short'
52 } : dateShort),
53 time: getFormatter(culture, supportStyles ? {
54 timeStyle: 'short'
55 } : timeShort),
56 datetime: getFormatter(culture, supportStyles ? {
57 dateStyle: 'short',
58 timeStyle: 'short'
59 } : Object.assign({}, dateShort, timeShort)),
60 header: getFormatter(culture, {
61 month: 'short',
62 year: 'numeric'
63 }),
64 weekday: getFormatter(culture, {
65 weekday: 'narrow'
66 }),
67 dayOfMonth: getFormatter(culture, {
68 day: '2-digit'
69 }),
70 month: getFormatter(culture, {
71 month: 'short'
72 }),
73 year: getFormatter(culture, {
74 year: 'numeric'
75 }),
76 decade: date => `${this.year(date)} - ${this.year(_dates.default.endOf(date, 'decade'))}`,
77 century: date => `${this.year(date)} - ${this.year(_dates.default.endOf(date, 'century'))}`
78 };
79 Object.keys(formats).forEach(key => {
80 this[key] = (date, format) => format ? normalizeFormat(date, format) : formats[key](date);
81 });
82 }
83
84 toFormattedParts(date, format = {
85 dateStyle: 'short',
86 timeStyle: 'short'
87 }) {
88 return Intl.DateTimeFormat(this.culture, format).formatToParts(date).filter(p => p.type !== 'timeZoneName');
89 }
90
91 parse(value) {
92 return new Date(value);
93 }
94
95}
96
97exports.DateLocalizer = IntlDateLocalizer;
98
99/**
100 * A number localization strategy based on `Intl.NumberFormat`.
101 */
102class IntlNumberLocalizer {
103 constructor({
104 culture = undefined
105 } = {}) {
106 var _$toLocaleString$m;
107
108 this.culture = culture;
109 const decimal = 'formatToParts' in Intl.NumberFormat(culture) ? Intl.NumberFormat(culture).formatToParts(1.1)[1].value : ((_$toLocaleString$m = 1.1.toLocaleString(culture).match(/[^\d]/)) == null ? void 0 : _$toLocaleString$m[0]) || '.';
110 const formatter = Intl.NumberFormat(culture).format;
111
112 this.decimalCharacter = () => decimal;
113
114 this.format = (num, format) => {
115 if (format) {
116 return typeof format === 'function' ? format(num, culture) : num.toLocaleString(culture, format);
117 }
118
119 return formatter(num);
120 };
121 }
122
123 parse(value) {
124 return parseFloat(value.replace(this.decimalCharacter(), '.'));
125 }
126
127}
128
129exports.NumberLocalizer = IntlNumberLocalizer;
\No newline at end of file