UNPKG

javascript-time-ago

Version:

Localized relative date/time formatting

87 lines (86 loc) 5.56 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { describe, it } from 'mocha'; import { expect } from 'chai'; import roundMinute from './roundMinute.js'; import TimeAgo from '../TimeAgo.js'; import { day, month, year } from '../steps/index.js'; describe('style/round-minute', function () { it('should format relative time (English) (round: "floor")', function () { var timeAgo = new TimeAgo('en'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatInterval = function formatInterval(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread(_objectSpread({ now: now }, roundMinute), {}, { round: 'floor' })); }; expect(formatInterval(0)).to.equal('just now'); expect(formatInterval(0.9)).to.equal('just now'); expect(formatInterval(1)).to.equal('just now'); expect(formatInterval(59.9)).to.equal('just now'); expect(formatInterval(60)).to.equal('1 minute ago'); expect(formatInterval(1.9 * 60)).to.equal('1 minute ago'); expect(formatInterval(2 * 60)).to.equal('2 minutes ago'); expect(formatInterval(2.9 * 60)).to.equal('2 minutes ago'); expect(formatInterval(3 * 60)).to.equal('3 minutes ago'); // … expect(formatInterval(59.9 * 60)).to.equal('59 minutes ago'); expect(formatInterval(60 * 60)).to.equal('1 hour ago'); expect(formatInterval(1.9 * 60 * 60)).to.equal('1 hour ago'); expect(formatInterval(2 * 60 * 60)).to.equal('2 hours ago'); expect(formatInterval(2.9 * 60 * 60)).to.equal('2 hours ago'); expect(formatInterval(3 * 60 * 60)).to.equal('3 hours ago'); // … expect(formatInterval(23.9 * 60 * 60)).to.equal('23 hours ago'); expect(formatInterval(24 * 60 * 60)).to.equal('1 day ago'); expect(formatInterval(1.9 * day)).to.equal('1 day ago'); expect(formatInterval(2 * day)).to.equal('2 days ago'); expect(formatInterval(2.9 * day)).to.equal('2 days ago'); expect(formatInterval(3 * day)).to.equal('3 days ago'); // … expect(formatInterval(6.9 * day)).to.equal('6 days ago'); expect(formatInterval(7 * day)).to.equal('1 week ago'); // … expect(formatInterval(3.9 * 7 * day)).to.equal('3 weeks ago'); expect(formatInterval(4 * 7 * day)).to.equal('4 weeks ago'); expect(formatInterval(30.51 * day)).to.equal('1 month ago'); expect(formatInterval(1.9 * month)).to.equal('1 month ago'); expect(formatInterval(2 * month)).to.equal('2 months ago'); expect(formatInterval(2.9 * month)).to.equal('2 months ago'); expect(formatInterval(3 * month)).to.equal('3 months ago'); // … expect(formatInterval(11.9 * month)).to.equal('11 months ago'); expect(formatInterval(12 * month)).to.equal('1 year ago'); expect(formatInterval(1.99 * year)).to.equal('1 year ago'); expect(formatInterval(2 * year)).to.equal('2 years ago'); // … // Test future dates. expect(formatInterval(-1 * 3 * 60)).to.equal('in 3 minutes'); expect(formatInterval(-1 * month * 8)).to.equal('in 8 months'); }); it('should format relative time (English)', function () { var timeAgo = new TimeAgo('en'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatInterval = function formatInterval(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, roundMinute)); }; expect(formatInterval(0)).to.equal('just now'); expect(formatInterval(0.49)).to.equal('just now'); expect(formatInterval(0.5)).to.equal('just now'); expect(formatInterval(29.9)).to.equal('just now'); expect(formatInterval(30)).to.equal('1 minute ago'); expect(formatInterval(1.49 * 60)).to.equal('1 minute ago'); expect(formatInterval(1.5 * 60)).to.equal('2 minutes ago'); expect(formatInterval(2.49 * 60)).to.equal('2 minutes ago'); expect(formatInterval(2.5 * 60)).to.equal('3 minutes ago'); }); }); //# sourceMappingURL=roundMinute.test.js.map