UNPKG

@formatjs/intl

Version:

Internationalize JS apps. This library provides an API to format dates, numbers, and strings, including pluralization and handling translations.

118 lines (117 loc) 4.47 kB
import { __assign } from "tslib"; import { IntlFormatError } from './error'; import { filterProps, getNamedFormat } from './utils'; var DATE_TIME_FORMAT_OPTIONS = [ 'formatMatcher', 'timeZone', 'hour12', 'weekday', 'era', 'year', 'month', 'day', 'hour', 'minute', 'second', 'timeZoneName', 'hourCycle', 'dateStyle', 'timeStyle', 'calendar', // 'dayPeriod', 'numberingSystem', 'fractionalSecondDigits', ]; export function getFormatter(_a, type, getDateTimeFormat, options) { var locale = _a.locale, formats = _a.formats, onError = _a.onError, timeZone = _a.timeZone; if (options === void 0) { options = {}; } var format = options.format; var defaults = __assign(__assign({}, (timeZone && { timeZone: timeZone })), (format && getNamedFormat(formats, type, format, onError))); var filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults); if (type === 'time' && !filteredOptions.hour && !filteredOptions.minute && !filteredOptions.second && !filteredOptions.timeStyle && !filteredOptions.dateStyle) { // Add default formatting options if hour, minute, or second isn't defined. filteredOptions = __assign(__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' }); } return getDateTimeFormat(locale, filteredOptions); } export function formatDate(config, getDateTimeFormat) { var _a = []; for (var _i = 2; _i < arguments.length; _i++) { _a[_i - 2] = arguments[_i]; } var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b; var date = typeof value === 'string' ? new Date(value || 0) : value; try { return getFormatter(config, 'date', getDateTimeFormat, options).format(date); } catch (e) { config.onError(new IntlFormatError('Error formatting date.', config.locale, e)); } return String(date); } export function formatTime(config, getDateTimeFormat) { var _a = []; for (var _i = 2; _i < arguments.length; _i++) { _a[_i - 2] = arguments[_i]; } var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b; var date = typeof value === 'string' ? new Date(value || 0) : value; try { return getFormatter(config, 'time', getDateTimeFormat, options).format(date); } catch (e) { config.onError(new IntlFormatError('Error formatting time.', config.locale, e)); } return String(date); } export function formatDateTimeRange(config, getDateTimeFormat) { var _a = []; for (var _i = 2; _i < arguments.length; _i++) { _a[_i - 2] = arguments[_i]; } var from = _a[0], to = _a[1], _b = _a[2], options = _b === void 0 ? {} : _b; var fromDate = typeof from === 'string' ? new Date(from || 0) : from; var toDate = typeof to === 'string' ? new Date(to || 0) : to; try { return getFormatter(config, 'dateTimeRange', getDateTimeFormat, options).formatRange(fromDate, toDate); } catch (e) { config.onError(new IntlFormatError('Error formatting date time range.', config.locale, e)); } return String(fromDate); } export function formatDateToParts(config, getDateTimeFormat) { var _a = []; for (var _i = 2; _i < arguments.length; _i++) { _a[_i - 2] = arguments[_i]; } var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b; var date = typeof value === 'string' ? new Date(value || 0) : value; try { return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged } catch (e) { config.onError(new IntlFormatError('Error formatting date.', config.locale, e)); } return []; } export function formatTimeToParts(config, getDateTimeFormat) { var _a = []; for (var _i = 2; _i < arguments.length; _i++) { _a[_i - 2] = arguments[_i]; } var value = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b; var date = typeof value === 'string' ? new Date(value || 0) : value; try { return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date); // TODO: remove this when https://github.com/microsoft/TypeScript/pull/50402 is merged } catch (e) { config.onError(new IntlFormatError('Error formatting time.', config.locale, e)); } return []; }