UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

217 lines (190 loc) 8.1 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _moment = require('moment'); var _moment2 = _interopRequireDefault(_moment); var _DateFormat = require('../api/DateFormat'); var _DateFormat2 = _interopRequireDefault(_DateFormat); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Utility class with functions that help manipulate dates. */ var DateUtils = function () { function DateUtils() { _classCallCheck(this, DateUtils); } /** * Given a JavaScript Date object, return a formatted date in the specified * locale using the format specified. */ DateUtils.formatDate = function formatDate(date, format) { var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en'; var m = (0, _moment2.default)(date).locale(locale); switch (format) { case _DateFormat2.default.ATTIVIO: return m.format(); case _DateFormat2.default.SHORT_DATE: return m.format('L'); case _DateFormat2.default.MEDIUM_DATE: return m.format('MMM D, YYYY'); case _DateFormat2.default.LONG_DATE: return m.format('dddd, MMMM D, YYYY'); case _DateFormat2.default.SHORT_TIME: return m.format('LT'); case _DateFormat2.default.LONG_TIME: return m.format('LTS'); case _DateFormat2.default.SHORT_MONTH: return m.format('MMM YYYY'); case _DateFormat2.default.LONG_MONTH: return m.format('MMMM YYYY'); case _DateFormat2.default.SHORT_YEAR: return m.format('\u2019YY'); // preceeding apostrophe case _DateFormat2.default.LONG_YEAR: return m.format('YYYY'); case _DateFormat2.default.SHORT: return m.format('L LT'); case _DateFormat2.default.MEDIUM: return m.format('MMM D, YYYY LT'); case _DateFormat2.default.LONG: return m.format('dddd, MMMM D, YYYY LTS'); case _DateFormat2.default.DAY_OF_MONTH: return m.format('MMMM D'); case _DateFormat2.default.HOUR: return m.format('h:00A'); case _DateFormat2.default.AGO: return m.fromNow(); case _DateFormat2.default.ISO_8601: default: return m.format(); } }; /** * Given a JavaScript Date object, return a formatted date in the specified * locale using the moment.js format string specified. */ DateUtils.formatDateCustom = function formatDateCustom(date, formatString) { var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en'; var m = (0, _moment2.default)(date).locale(locale); return m.format(formatString); }; /** * Given a string, return a Date object created by parsing it. */ DateUtils.stringToDate = function stringToDate(dateString) { return (0, _moment2.default)(dateString).toDate(); }; /** * Given a string, return a formatted date in the specified locale using * the format specified. */ DateUtils.formatDateString = function formatDateString(dateString, format) { var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en'; if (dateString) { var date = DateUtils.stringToDate(dateString); return DateUtils.formatDate(date, format, locale); } return ''; }; /** * Format the date range presented for display in places such as * tooltips on TimeSeries charts. Based on the difference in the * start and end times, it will display either a single value or * a range, with varying precision. For example, if the points are * an hour apart, it will format based on the halfway point with * precision to the hour, e.g., "March 14, 2017 2:00 AM." In some * cases, the result will be a range, such as if they're a week * apart, in which case you might get "June 14 - 21, 2018." * * The point is to not clutter the formatted value with unnecessary * information, essentially. * * Here is a complete list of the possible formats: * * * April 2, 2018 3:14 AM * * March 2, 2018 3:00PM * * March 2, 2018 * * December 28, 2017 - January 4, 2018 * * March 28 - April 4, 2018 * * June 14 - 21, 2018 * * March, 2018 * * December, 2017 - January, 2018 * * April - May, 2017 * * 2016 - 2019 */ DateUtils.formatDateRange = function formatDateRange(start, end) { var diff = end - start; var middle = (start + end) / 2; var startMoment = (0, _moment2.default)(start); var endMoment = (0, _moment2.default)(end); var result = void 0; if (diff <= 1000 * 60) { // If they're a minute apart, format tho the nearest minute (we never care about seconds...) // E.g., March 2, 2018 3:14PM result = (0, _moment2.default)(middle).format('LL LT'); } else if (diff <= 1000 * 60 * 60 * 12) { // If they're an hour or up to 12 hours apart, format to the hour with minutes set to 00 // E.g., March 2, 2018 3:00PM result = (0, _moment2.default)(middle).format('LL h:00 A'); } else if (diff <= 1000 * 60 * 60 * 24) { // If they're a day apart, format to the date // E.g., March 2, 2018 result = (0, _moment2.default)(middle).format('LL'); } else if (diff <= 1000 * 60 * 60 * 24 * 7) { // If they're a week apart, format to the range of the first day to the last day if (startMoment.year() !== endMoment.year()) { // Need to include both years // E.g., Decembeer 28, 2017 - January 3, 2018 var formattedStart = (0, _moment2.default)(start).format('LL'); var formattedEnd = (0, _moment2.default)(end).format('LL'); result = formattedStart + ' - ' + formattedEnd; } else if (startMoment.month() !== endMoment.month()) { // Need to include both months but only one year // E.g., March 28 - April 3, 2018 var _formattedStart = startMoment.format('MMMM D'); var _formattedEnd = endMoment.format('LL'); result = _formattedStart + ' - ' + _formattedEnd; } else { // Otherwise, we can just include the momnth and year once // E.g., June 14 - 21, 2018 var _formattedStart2 = startMoment.format('MMMM D'); var _formattedEnd2 = endMoment.format('D, YYYY'); result = _formattedStart2 + ' - ' + _formattedEnd2; } } else if (diff <= 1000 * 60 * 60 * 24 * 30) { // If they're a month apart, format to just the month // E.g., March, 2018 result = (0, _moment2.default)(middle).format('MMMM, YYYY'); } else if (diff <= 1000 * 60 * 60 * 24 * 366) { // If they'e a year apart, format to the range of months if (startMoment.year() !== endMoment.year()) { // If they're in different years, include both years // E.g., December, 2017 - January, 2018 var _formattedStart3 = startMoment.format('MMMM, YYYY'); var _formattedEnd3 = endMoment.format('MMMM, YYYY'); result = _formattedStart3 + ' - ' + _formattedEnd3; } else if (startMoment.month() === 0 && endMoment.month() === 11) { // Special case of January - December of the same year... // Just show the year // E.g., 2017 result = startMoment.format('YYYY'); } else { // Otherwise show both months and include the year only once // E.g., March - April, 2018 var _formattedStart4 = startMoment.format('MMMM'); var _formattedEnd4 = endMoment.format('MMMM, YYYY'); result = _formattedStart4 + ' - ' + _formattedEnd4; } } else { // If they're more than a year apart, format to the range of years... // E.g., 2017 - 2018 var _formattedStart5 = startMoment.format('YYYY'); var _formattedEnd5 = endMoment.format('YYYY'); result = _formattedStart5 + ' - ' + _formattedEnd5; } return result; }; return DateUtils; }(); exports.default = DateUtils; module.exports = exports['default'];