Skip to content

Commit

Permalink
v1.0.3 - add timeLeftStr() to return abbr time string
Browse files Browse the repository at this point in the history
  • Loading branch information
pham committed Oct 16, 2020
1 parent fedbc85 commit 082daeb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ export function timeLeft(delta = 0) {
return { days, hours, minutes, seconds };
}

const timeTags = {
days: 'days',
hours: 'hours',
minutes: 'minutes',
seconds: 'seconds',
second: 'second'
};

export function timeLeftStr(delta = 0, tags = timeTags) {
const info = timeLeft(delta);

if (info.days > 1) {
return info.days + ' ' + tags.days;
}

info.hours += info.days * 24;
if (info.hours > 1) {
return info.hours + ' ' + tags.hours;
}

info.minutes += info.hours * 60;
if (info.minutes > 1) {
return info.minutes + ' ' + tags.minutes;
}

info.seconds += info.minutes * 60;
if (info.seconds > 1) {
return info.seconds + ' ' + tags.seconds;
}

return info.seconds + ' ' + (tags.second || tags.seconds);
}

export function nextDow(day: number = 0) {
const time = new Date();
const offset = (day - time.getDay()) * 86400000;
Expand Down Expand Up @@ -56,6 +89,7 @@ export function toPrettyDate(ymd: string, hms = '00:00:00') {
month: 'short',
day: '2-digit',
hour: 'numeric',
minute: 'numeric',
hour12: true
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aquaron/time",
"version": "1.0.2",
"version": "1.0.3",
"description": "Time utilities",
"main": "dist/index",
"typings": "dist/index",
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ describe('toYMD()', () => {
expect(time.toYMD(new Date(...t.date))).toEqual(t.result)
));
});

describe('toPrettyDate()', () => {
const tests = [
{ date: '2020-10-14', time: undefined, result: 'Wednesday, Oct 14, 2020, 12:00 AM' },
{ date: '2020-10-12', time: '12:22:33', result: 'Monday, Oct 12, 2020, 12:22 PM' }
];
tests.forEach( t => test(`Date ${t.date}`, () =>
expect(time.toPrettyDate(t.date, t.time)).toEqual(t.result)
));
});
23 changes: 23 additions & 0 deletions test/timeLeft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ describe('timeLeft()', () => {
expect(time.timeLeft(t.delta)).toEqual(expect.objectContaining(t.result))
));
});

describe('timeLeftStr()', () => {
const tags = {
days: 'ngày',
hours: 'tiếng',
minutes: 'phút',
seconds: 'giây'
};
const tests = [
{ delta: 1000, result: '1 giây', result2: '1 second' },
{ delta: 2000, result: '2 giây', result2: '2 seconds' },
{ delta: 360000, result: '6 phút', result2: '6 minutes' },
{ delta: 93600000, result: '26 tiếng', result2: '26 hours' },
{ delta: 82800000, result: '23 tiếng', result2: '23 hours' },
{ delta: 345600000, result: '4 ngày', result2: '4 days' }
];
tests.forEach( (t) => test(`Time left (vn): ${t.result}`, () =>
expect(time.timeLeftStr(t.delta, tags)).toEqual(t.result)
));
tests.forEach( (t) => test(`Time left (en): ${t.result2}`, () =>
expect(time.timeLeftStr(t.delta)).toEqual(t.result2)
));
});

0 comments on commit 082daeb

Please sign in to comment.