Skip to content

Commit

Permalink
chore: compress dist
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Oct 13, 2019
1 parent 6930000 commit 69d8afa
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
12 changes: 6 additions & 6 deletions __tests__/utils/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
* Contract: [email protected]
*/

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', () => {
Expand Down
2 changes: 1 addition & 1 deletion gh-pages/timeago.full.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gh-pages/timeago.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export const register = (locale: string, func: LocaleFunc) => {
* @param locale
* @returns {*}
*/
export const getLocale = (locale) => {
export const getLocale = (locale: string): LocaleFunc => {
return Locales[locale] || en_US;
};
18 changes: 9 additions & 9 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

/**
Expand All @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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);
}
7 changes: 3 additions & 4 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const ATTR_TIMEAGO_TID = 'timeago-tid';
const ATTR_DATETIME = 'datetime';
const ATTR_TIMEAGO_TID = 'timeago-id';

/**
* get the datetime attribute, `datetime` are supported.
* @param node
* @returns {*}
*/
export function getDateAttribute(node: HTMLElement): string {
return node.getAttribute(ATTR_DATETIME);
return node.getAttribute('datetime');
}

/**
Expand All @@ -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));
}

0 comments on commit 69d8afa

Please sign in to comment.