UNPKG

@pubsweet/ui

Version:

React component library for use in pubsweet apps

74 lines (62 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _moment = _interopRequireDefault(require("moment")); var _propTypes = _interopRequireDefault(require("prop-types")); var _recompose = require("recompose"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var getDuration = function getDuration(timestamp) { var today = (0, _moment["default"])(); var stamp = (0, _moment["default"])(timestamp); return _moment["default"].duration(today.diff(stamp)); }; var D = function D(_ref) { var children = _ref.children, timestamp = _ref.timestamp, timeAgo = _ref.timeAgo; return children(timestamp, timeAgo); }; var DateParser = (0, _recompose.compose)((0, _recompose.setDisplayName)('DateParser'), (0, _recompose.withHandlers)({ renderTimestamp: function renderTimestamp(_ref2) { var timestamp = _ref2.timestamp, _ref2$dateFormat = _ref2.dateFormat, dateFormat = _ref2$dateFormat === void 0 ? 'DD.MM.YYYY' : _ref2$dateFormat, _ref2$humanizeThresho = _ref2.humanizeThreshold, humanizeThreshold = _ref2$humanizeThresho === void 0 ? 0 : _ref2$humanizeThresho; return function () { if (!timestamp) return ''; var duration = getDuration(timestamp); if (duration.asDays() < humanizeThreshold) { return "".concat(duration.humanize(), " ago"); } return (0, _moment["default"])(timestamp).format(dateFormat); }; }, renderTimeAgo: function renderTimeAgo(_ref3) { var timestamp = _ref3.timestamp; return function () { if (!timestamp) return ''; var duration = getDuration(timestamp); return duration.humanize(); }; } }), (0, _recompose.withProps)(function (_ref4) { var renderTimestamp = _ref4.renderTimestamp, renderTimeAgo = _ref4.renderTimeAgo; return { timeAgo: renderTimeAgo(), timestamp: renderTimestamp() }; }))(D); var _default = DateParser; exports["default"] = _default; DateParser.propTypes = { /** The date string. Can be any date parsable by momentjs. */ timestamp: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number, Date]).isRequired, /** Format of the rendered date. */ dateFormat: _propTypes["default"].string, /** Humanize duration threshold */ humanizeThreshold: _propTypes["default"].number };