javascript-time-ago
Version:
Localized relative date/time formatting
259 lines (247 loc) โข 12.1 kB
JavaScript
;
var _twitter = _interopRequireDefault(require("./twitter.js"));
var _TimeAgo = _interopRequireDefault(require("../TimeAgo.js"));
var _index = require("../steps/index.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
describe('style/twitter', function () {
it('should fallback from "mini" to "narrow"', function () {
var timeAgo = new _TimeAgo["default"]('ccp');
timeAgo.format(Date.now() - 3 * _index.hour * 1000, 'twitter').should.include(' ๐๐ฎ๐๐ด๐ ๐๐๐ฌ');
});
it('should format Twitter style relative time (English) (round: "round")', function () {
var timeAgo = new _TimeAgo["default"]('en');
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(-secondsPassed * 1000, _objectSpread({
now: 0
}, _twitter["default"]));
};
formatDatePastBy(0.49).should.equal('0s');
formatDatePastBy(0.5).should.equal('1s');
formatDatePastBy(59.49).should.equal('59s');
formatDatePastBy(59.5).should.equal('1m');
formatDatePastBy(1.49 * _index.minute).should.equal('1m');
formatDatePastBy(1.5 * _index.minute).should.equal('2m');
// โฆ
formatDatePastBy(59.49 * _index.minute).should.equal('59m');
formatDatePastBy(59.5 * _index.minute).should.equal('1h');
formatDatePastBy(1.49 * _index.hour).should.equal('1h');
formatDatePastBy(1.5 * _index.hour).should.equal('2h');
// โฆ
formatDatePastBy(23.49 * _index.hour).should.equal('23h');
});
it('should format Twitter style relative time (English) (round: "floor")', function () {
var timeAgo = new _TimeAgo["default"]('en');
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(-secondsPassed * 1000, _objectSpread(_objectSpread({
now: 0
}, _twitter["default"]), {}, {
round: 'floor'
}));
};
formatDatePastBy(0).should.equal('0s');
formatDatePastBy(0.9).should.equal('0s');
formatDatePastBy(1).should.equal('1s');
formatDatePastBy(59.9).should.equal('59s');
formatDatePastBy(60).should.equal('1m');
formatDatePastBy(1.9 * _index.minute).should.equal('1m');
formatDatePastBy(2 * _index.minute).should.equal('2m');
formatDatePastBy(2.9 * _index.minute).should.equal('2m');
formatDatePastBy(3 * _index.minute).should.equal('3m');
// โฆ
formatDatePastBy(59.9 * _index.minute).should.equal('59m');
formatDatePastBy(60 * _index.minute).should.equal('1h');
formatDatePastBy(1.9 * _index.hour).should.equal('1h');
formatDatePastBy(2 * _index.hour).should.equal('2h');
formatDatePastBy(2.9 * _index.hour).should.equal('2h');
formatDatePastBy(3 * _index.hour).should.equal('3h');
// โฆ
formatDatePastBy(23.9 * _index.hour).should.equal('23h');
});
it('should format Twitter style relative time (English) (absolute dates)', function () {
var timeAgo = new _TimeAgo["default"]('en');
// April 10th, 2016, 12:00.
var now = new Date(2016, 3, 10, 12, 0).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(_index.day + 2 * _index.minute + _index.hour).should.equal('Apr 9');
// โฆ
// `month` is about 30.5 days.
formatDatePastBy(_index.month * 3).should.equal('Jan 10');
formatDatePastBy(_index.month * 4).should.equal('Dec 10, 2015');
formatDatePastBy(_index.year).should.equal('Apr 11, 2015');
// Test future dates.
// `month` is about 30.5 days.
formatDatePastBy(-1 * _index.month * 8).should.equal('Dec 10');
formatDatePastBy(-1 * _index.month * 9).should.equal('Jan 9, 2017');
});
it('should format Twitter style relative time (Russian)', function () {
var timeAgo = new _TimeAgo["default"]('ru');
var now = new Date(2016, 3, 10, 22, 59).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(0).should.equal('0 ั');
formatDatePastBy(1).should.equal('1 ั');
formatDatePastBy(_index.minute).should.equal('1 ะผะธะฝ');
formatDatePastBy(_index.hour).should.equal('1 ั');
formatDatePastBy(_index.day + 62 * _index.minute).should.equal('9 ะฐะฟั.');
formatDatePastBy(_index.year).should.equal('11 ะฐะฟั. 2015 ะณ.');
});
it('should format Twitter style relative time (Korean)', function () {
var timeAgo = new _TimeAgo["default"]('ko');
var now = new Date(2016, 3, 10, 22, 59).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(_index.minute).should.equal('1๋ถ');
formatDatePastBy(_index.hour).should.equal('1์๊ฐ');
formatDatePastBy(_index.day + 62 * _index.minute).should.equal('4์ 9์ผ');
formatDatePastBy(_index.year).should.equal('2015๋
4์ 11์ผ');
});
it('should format Twitter style relative time (German)', function () {
var timeAgo = new _TimeAgo["default"]('de');
var now = new Date(2016, 3, 10, 22, 59).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(_index.minute).should.equal('1 Min.');
formatDatePastBy(_index.hour).should.equal('1 Std.');
formatDatePastBy(_index.day + 62 * _index.minute).should.equal('9. Apr.');
formatDatePastBy(_index.year).should.equal('11. Apr. 2015');
});
it('should format Twitter style relative time (French)', function () {
var timeAgo = new _TimeAgo["default"]('fr');
var now = new Date(2016, 3, 10, 22, 59).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(_index.minute).should.equal('1 min.');
formatDatePastBy(_index.hour).should.equal('1 h');
formatDatePastBy(_index.day + 62 * _index.minute).should.equal('9 avr.');
formatDatePastBy(_index.year).should.equal('11 avr. 2015');
});
it('should format Twitter style relative time (Chinese)', function () {
var timeAgo = new _TimeAgo["default"]('zh');
var now = new Date(2016, 3, 10, 22, 59).getTime();
var formatDatePastBy = function formatDatePastBy(secondsPassed) {
return timeAgo.format(now - secondsPassed * 1000, _objectSpread({
now: now
}, _twitter["default"]));
};
formatDatePastBy(_index.minute).should.equal('1ๅ้');
formatDatePastBy(_index.hour).should.equal('1ๅฐๆถ');
formatDatePastBy(_index.day + 62 * _index.minute).should.equal('4ๆ9ๆฅ');
formatDatePastBy(_index.year).should.equal('2015ๅนด4ๆ11ๆฅ');
});
// This test won't pass because `Intl.DateTimeFormat` is read at
// initialization time, not at run time.
// it('should fall back to generic style when Intl.DateTimeFormat is not available', () => {
// const DateTimeFormat = Intl.DateTimeFormat
// Intl.DateTimeFormat = undefined
//
// const timeAgo = new TimeAgo('en')
// timeAgo.format(Date.now() - 365 * 24 * hour * 1000, 'twitter').should.equal('1yr')
//
// Intl.DateTimeFormat = DateTimeFormat
// })
it('should support timestamp argument on `yearMonthAndDay.test()`', function () {
var timeAgo = new _TimeAgo["default"]('en');
timeAgo.format(0, 'twitter').should.equal('Jan 1, 1970');
});
it('should round as "floor"', function () {
var timeAgo = new _TimeAgo["default"]('en');
var test = function test(time, result) {
return timeAgo.format(time, 'twitter', {
round: 'floor',
now: 0
}).should.equal(result);
};
test(2001, '2s');
test(2000, '2s');
test(1999, '1s');
test(1001, '1s');
test(1000, '1s');
test(999, '0s');
test(0, '0s');
test(-999, '0s');
test(-1000, '1s');
test(-1001, '1s');
test(-1999, '1s');
test(-2000, '2s');
test(-2001, '2s');
});
it('should get time to next update (round: "floor")', function () {
var timeAgo = new _TimeAgo["default"]('en');
// April 10th, 2018, 12:00.
var date = new Date(2018, 3, 10, 12, 0);
// April 10th, 2016, 12:00 (two years earlier).
var now = new Date(2016, 3, 10, 12, 0).getTime();
timeAgo.format(date, 'twitter', {
now: now,
getTimeToNextUpdate: true,
getTimeToNextUpdateUncapped: true
}).should.deep.equal(['Apr 10, 2018',
// Updates on Jan 1st, 2018, 00:00.
new Date(2018, 0, 1).getTime() - now]);
// 1st, 2018, 00:00.
now = new Date(2018, 0, 1).getTime();
timeAgo.format(date, 'twitter', {
now: now,
getTimeToNextUpdate: true,
getTimeToNextUpdateUncapped: true,
round: 'floor'
}).should.deep.equal(['Apr 10',
// Updates after April 9th, 2018, 12:00.
new Date(2018, 3, 9, 12, 0).getTime() + 1 - now]);
// After April 9th, 2018, 12:00.
now = new Date(2018, 3, 9, 12, 0).getTime() + 1;
timeAgo.format(date, 'twitter', {
now: now,
getTimeToNextUpdate: true,
round: 'floor'
}).should.deep.equal(['23h',
// Updates in an hour.
60 * 60 * 1000]);
});
it('should get time to next update (round: "round")', function () {
var timeAgo = new _TimeAgo["default"]('en');
// April 10th, 2018, 12:00.
var date = new Date(2018, 3, 10, 12, 0);
var now;
// 1st, 2018, 00:00.
now = new Date(2018, 0, 1).getTime();
timeAgo.format(date, 'twitter', {
now: now,
getTimeToNextUpdate: true,
getTimeToNextUpdateUncapped: true
}).should.deep.equal(['Apr 10',
// Updates after April 9th, 2018, 11:30.
new Date(2018, 3, 9, 12, 0).getTime() + 30 * 60 * 1000 + 1 - now]);
// After April 9th, 2018, 12:00.
now = new Date(2018, 3, 9, 12, 0).getTime() + 30 * 60 * 1000 + 1;
timeAgo.format(date, 'twitter', {
now: now,
getTimeToNextUpdate: true
}).should.deep.equal(['23h',
// Updates in an hour.
60 * 60 * 1000]);
});
});
//# sourceMappingURL=twitter.test.js.map