UNPKG

react-widgets-date-fns

Version:

A date-fns localizer for react-widgets

96 lines (68 loc) 2.38 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _formatWithOptions = _interopRequireDefault(require("date-fns/fp/formatWithOptions")); var _parseWithOptions = _interopRequireDefault(require("date-fns/fp/parseWithOptions")); var _addYears = _interopRequireDefault(require("date-fns/fp/addYears")); var _enUS = _interopRequireDefault(require("date-fns/locale/en-US")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const endOfDecade = (0, _addYears.default)(10); const endOfCentury = (0, _addYears.default)(100); class DateFnsLocalizer { constructor(locale) { this.locale = locale || _enUS.default; const { options } = this.locale; this.firstOfWeek = () => options && options.weekStartsOn || 0; } firstOfWeek() { var _this$locale$options$, _this$locale$options; return (_this$locale$options$ = (_this$locale$options = this.locale.options) == null ? void 0 : _this$locale$options.weekStartsOn) != null ? _this$locale$options$ : 0; } date(date, format) { return this.format(date, format || 'P'); } time(date, format) { return this.format(date, format || 'pp'); } datetime(date, format) { return this.format(date, format || 'Pp'); } header(date, format) { return this.format(date, format || 'MMMM yyyy'); } weekday(date, format) { return this.format(date, format || 'cccccc'); } dayOfMonth(date, format) { return this.format(date, format || 'DD'); } month(date, format) { return this.format(date, format || 'MMM'); } year(date, format) { return this.format(date, format || 'YYYY'); } decade(date, format) { return format ? this.format(date, format) : `${this.format(date, 'YYYY')} - ${this.format(endOfDecade(date), 'YYYY')}`; } century(date, format) { return format ? this.format(date, format) : `${this.format(date, 'YYYY')} - ${this.format(endOfCentury(date), 'YYYY')}`; } format(value, format) { return (0, _formatWithOptions.default)({ locale: this.locale }, format, value); } parse(value, format) { const result = (0, _parseWithOptions.default)({ locale: this.locale }, new Date(), format, value); if (result.toString() === 'Invalid Date') { return null; } return result; } } exports.default = DateFnsLocalizer;