diff --git a/__tests__/utils/date.spec.ts b/__tests__/utils/date.spec.ts index 93ad7a8..1ca762f 100644 --- a/__tests__/utils/date.spec.ts +++ b/__tests__/utils/date.spec.ts @@ -3,19 +3,19 @@ * Contract: i@hust.cc */ -import { toTimestamp, formatDiff, diffSec, nextInterval } from '../../src/utils/date'; +import { toDate, formatDiff, diffSec, nextInterval } from '../../src/utils/date'; import { getLocale } from '../../src/locales'; describe('date', () => { test('toTimestamp', () => { - expect(typeof toTimestamp('1992-08-01')).toBe('number'); - expect(typeof toTimestamp(712627200000)).toBe('number'); + expect(toDate('1992-08-01')).toBeInstanceOf(Date); + expect(toDate(712627200000)).toBeInstanceOf(Date); - expect(typeof toTimestamp('2017-2-5 3:57:52UTC')).toBe('number'); - expect(typeof toTimestamp('2017-2-5T3:57:52Z')).toBe('number'); + expect(toDate('2017-2-5 3:57:52UTC')).toBeInstanceOf(Date); + expect(toDate('2017-2-5T3:57:52Z')).toBeInstanceOf(Date); - expect(typeof toTimestamp()).toBe('number'); + expect(toDate()).toBeInstanceOf(Date); }); test('diffSec', () => { diff --git a/gh-pages/timeago.full.min.js b/gh-pages/timeago.full.min.js index 8f65019..2369137 100644 --- a/gh-pages/timeago.full.min.js +++ b/gh-pages/timeago.full.min.js @@ -1 +1 @@ -!function(s,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((s=s||self).timeago={})}(this,function(s){"use strict";var o=[60,60,24,7,365/7/12,12];function e(s){return s instanceof Date?+s:!isNaN(s)||/^\d+$/.test(s)?+new Date(parseInt(s)):(s=(s||"").trim().replace(/\.\d+/,"").replace(/-/,"/").replace(/-/,"/").replace(/(\d)T(\d)/,"$1 $2").replace(/Z/," UTC").replace(/([+-]\d\d):?(\d\d)/," $1$2"),+new Date(s))}function m(s,n){for(var e=s<0?1:0,a=s=Math.abs(s),t=0;s>=o[t]&&t=o[e]&&e=o[t]&&t=o[e]&&e=c[a]&&a=c[n]&&n=c[a]&&a=c[n]&&n { * @param locale * @returns {*} */ -export const getLocale = (locale) => { +export const getLocale = (locale: string): LocaleFunc => { return Locales[locale] || en_US; }; diff --git a/src/utils/date.ts b/src/utils/date.ts index 8610073..5fe1c46 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -12,10 +12,10 @@ const SEC_ARRAY = [60, 60, 24, 7, 365 / 7 / 12, 12]; * @param input * @returns {*} */ -export function toTimestamp(input?: Date | string | number): number { - if (input instanceof Date) return +input; +export function toDate(input?: Date | string | number): Date { + if (input instanceof Date) return input; // @ts-ignore - if (!isNaN(input) || /^\d+$/.test(input)) return +new Date(parseInt(input)); + if (!isNaN(input) || /^\d+$/.test(input)) return new Date(parseInt(input)); input = (input || '') // @ts-ignore .trim() @@ -25,7 +25,7 @@ export function toTimestamp(input?: Date | string | number): number { .replace(/(\d)T(\d)/, '$1 $2') .replace(/Z/, ' UTC') // 2017-2-5T3:57:52Z -> 2017-2-5 3:57:52UTC .replace(/([+-]\d\d):?(\d\d)/, ' $1$2'); // -04:00 -> -0400 - return +new Date(input); + return new Date(input); } /** @@ -48,6 +48,7 @@ export function formatDiff(diff: number, localeFunc: LocaleFunc): string { for (; diff >= SEC_ARRAY[idx] && idx < SEC_ARRAY.length; idx++) { diff /= SEC_ARRAY[idx]; } + // Math.floor diff = ~~diff; idx *= 2; @@ -62,9 +63,9 @@ export function formatDiff(diff: number, localeFunc: LocaleFunc): string { * @param relativeDate * @returns */ -export function diffSec(date: TDate, relativeDate): number { - relativeDate = relativeDate ? toTimestamp(relativeDate) : +new Date(); - return (relativeDate - toTimestamp(date)) / 1000; +export function diffSec(date: TDate, relativeDate: TDate): number { + relativeDate = relativeDate ? toDate(relativeDate) : new Date(); + return (+relativeDate - +toDate(date)) / 1000; } /** @@ -86,6 +87,5 @@ export function nextInterval(diff: number): number { } d = d % rst; d = d ? rst - d : rst; - // Math.ceil - return ~~d; + return Math.ceil(d); } diff --git a/src/utils/dom.ts b/src/utils/dom.ts index a77b77d..0b0b9c2 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -1,5 +1,4 @@ -const ATTR_TIMEAGO_TID = 'timeago-tid'; -const ATTR_DATETIME = 'datetime'; +const ATTR_TIMEAGO_TID = 'timeago-id'; /** * get the datetime attribute, `datetime` are supported. @@ -7,7 +6,7 @@ const ATTR_DATETIME = 'datetime'; * @returns {*} */ export function getDateAttribute(node: HTMLElement): string { - return node.getAttribute(ATTR_DATETIME); + return node.getAttribute('datetime'); } /** @@ -26,5 +25,5 @@ export function setTimerId(node: HTMLElement, timerId: number): void { * @param node */ export function getTimerId(node: HTMLElement): number { - return ~~node.getAttribute(ATTR_TIMEAGO_TID); + return parseInt(node.getAttribute(ATTR_TIMEAGO_TID)); }