You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.5 KiB
99 lines
3.5 KiB
"use strict";
|
|
/*
|
|
* Copyright 2015, Yahoo Inc.
|
|
* Copyrights licensed under the New BSD License.
|
|
* See the accompanying LICENSE file for terms.
|
|
*/
|
|
var __assign = (this && this.__assign) || function () {
|
|
__assign = Object.assign || function(t) {
|
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
s = arguments[i];
|
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
t[p] = s[p];
|
|
}
|
|
return t;
|
|
};
|
|
return __assign.apply(this, arguments);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var utils_1 = require("../utils");
|
|
var DATE_TIME_FORMAT_OPTIONS = [
|
|
'localeMatcher',
|
|
'formatMatcher',
|
|
'timeZone',
|
|
'hour12',
|
|
'weekday',
|
|
'era',
|
|
'year',
|
|
'month',
|
|
'day',
|
|
'hour',
|
|
'minute',
|
|
'second',
|
|
'timeZoneName',
|
|
];
|
|
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 && utils_1.getNamedFormat(formats, type, format, onError)));
|
|
var filteredOptions = utils_1.filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
|
|
if (type === 'time' &&
|
|
!filteredOptions.hour &&
|
|
!filteredOptions.minute &&
|
|
!filteredOptions.second) {
|
|
// Add default formatting options if hour, minute, or second isn't defined.
|
|
filteredOptions = __assign(__assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
|
|
}
|
|
return getDateTimeFormat(locale, filteredOptions);
|
|
}
|
|
exports.getFormatter = getFormatter;
|
|
function formatDate(config, getDateTimeFormat, value, options) {
|
|
if (options === void 0) { options = {}; }
|
|
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
try {
|
|
return getFormatter(config, 'date', getDateTimeFormat, options).format(date);
|
|
}
|
|
catch (e) {
|
|
config.onError(utils_1.createError('Error formatting date.', e));
|
|
}
|
|
return String(date);
|
|
}
|
|
exports.formatDate = formatDate;
|
|
function formatTime(config, getDateTimeFormat, value, options) {
|
|
if (options === void 0) { options = {}; }
|
|
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
try {
|
|
return getFormatter(config, 'time', getDateTimeFormat, options).format(date);
|
|
}
|
|
catch (e) {
|
|
config.onError(utils_1.createError('Error formatting time.', e));
|
|
}
|
|
return String(date);
|
|
}
|
|
exports.formatTime = formatTime;
|
|
function formatDateToParts(config, getDateTimeFormat, value, options) {
|
|
if (options === void 0) { options = {}; }
|
|
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
try {
|
|
return getFormatter(config, 'date', getDateTimeFormat, options).formatToParts(date);
|
|
}
|
|
catch (e) {
|
|
config.onError(utils_1.createError('Error formatting date.', e));
|
|
}
|
|
return [];
|
|
}
|
|
exports.formatDateToParts = formatDateToParts;
|
|
function formatTimeToParts(config, getDateTimeFormat, value, options) {
|
|
if (options === void 0) { options = {}; }
|
|
var date = typeof value === 'string' ? new Date(value || 0) : value;
|
|
try {
|
|
return getFormatter(config, 'time', getDateTimeFormat, options).formatToParts(date);
|
|
}
|
|
catch (e) {
|
|
config.onError(utils_1.createError('Error formatting time.', e));
|
|
}
|
|
return [];
|
|
}
|
|
exports.formatTimeToParts = formatTimeToParts;
|