Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiansandqvist committed Aug 18, 2019
2 parents 8edce57 + 206c156 commit f7b67d3
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 48 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ var ago = require('s-ago');
var now = new Date();
var yesterday = new Date(now - (24 * 60 * 60 * 1000));
var hoursAgo = new Date(now - (6 * 60 * 60 * 1000));
var yesterday = new Date(now - (24 * 60 * 60 * 1000));
var inADay = new Date(now + (6 * 60 * 60 * 1000));
var inSixHours = new Date(now + (6 * 60 * 60 * 1000));

ago(now); // 'just now'
ago(yesterday); // 'yesterday'
ago(hoursAgo); // '6 hours ago'

ago(inADay); // 'in a day'
ago(inSixHours); // 'in 6 hours'
```

Output is as follows:
Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ ago(yesterday); // 'yesterday'
ago(hoursAgo); // '6 hours ago'
*/
function format(diff, divisor, unit, prev) {
function format(diff, divisor, unit, prev, inv, inverted) {
var val = Math.round(diff / divisor);
if (inverted) {
return val <= 1 ? inv : 'in ' + val + ' ' + unit + 's';
}
return val <= 1 ? prev : val + ' ' + unit + 's ago';
}
var units = [
{ max: 2760000, value: 60000, name: 'minute', prev: 'a minute ago' },
{ max: 72000000, value: 3600000, name: 'hour', prev: 'an hour ago' },
{ max: 518400000, value: 86400000, name: 'day', prev: 'yesterday' },
{ max: 2419200000, value: 604800000, name: 'week', prev: 'last week' },
{ max: 28512000000, value: 2592000000, name: 'month', prev: 'last month' } // max: 11 months
{ max: 2760000, value: 60000, name: 'minute', prev: 'a minute ago', "in": 'in a minute' },
{ max: 72000000, value: 3600000, name: 'hour', prev: 'an hour ago', "in": 'in an hour' },
{ max: 518400000, value: 86400000, name: 'day', prev: 'yesterday', "in": 'in a day' },
{ max: 2419200000, value: 604800000, name: 'week', prev: 'last week', "in": 'in a week' },
{ max: 28512000000, value: 2592000000, name: 'month', prev: 'last month', "in": 'in a month' } // max: 11 months
];
module.exports = function ago(date) {
var inverted = Date.now() < date.getTime();
var diff = Math.abs(Date.now() - date.getTime());
// less than a minute
if (diff < 60000)
return 'just now';
for (var i = 0; i < units.length; i++) {
if (diff < units[i].max) {
return format(diff, units[i].value, units[i].name, units[i].prev);
return format(diff, units[i].value, units[i].name, units[i].prev, units[i]["in"], inverted);
}
}
// `year` is the final unit.
Expand All @@ -43,5 +47,5 @@ module.exports = function ago(date) {
// name: 'year',
// prev: 'last year'
// }
return format(diff, 31536000000, 'year', 'last year');
return format(diff, 31536000000, 'year', 'last year', 'in a year', inverted);
};
21 changes: 12 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@ ago(hoursAgo); // '6 hours ago'
*/

function format(diff: number, divisor: number, unit: string, prev: string) {
function format(diff: number, divisor: number, unit: string, prev: string, inv: string, inverted: boolean) {
var val = Math.round(diff / divisor);
if (inverted) {
return val <= 1 ? inv : 'in ' + val + ' ' + unit + 's';
}
return val <= 1 ? prev : val + ' ' + unit + 's ago';
}

const units = [
{ max: 2760000, value: 60000, name: 'minute', prev: 'a minute ago' }, // max: 46 minutes
{ max: 72000000, value: 3600000, name: 'hour', prev: 'an hour ago' }, // max: 20 hours
{ max: 518400000, value: 86400000, name: 'day', prev: 'yesterday' }, // max: 6 days
{ max: 2419200000, value: 604800000, name: 'week', prev: 'last week' }, // max: 28 days
{ max: 28512000000, value: 2592000000, name: 'month', prev: 'last month' } // max: 11 months
{ max: 2760000, value: 60000, name: 'minute', prev: 'a minute ago', in: 'in a minute' }, // max: 46 minutes
{ max: 72000000, value: 3600000, name: 'hour', prev: 'an hour ago', in: 'in an hour' }, // max: 20 hours
{ max: 518400000, value: 86400000, name: 'day', prev: 'yesterday', in: 'in a day' }, // max: 6 days
{ max: 2419200000, value: 604800000, name: 'week', prev: 'last week', in: 'in a week' }, // max: 28 days
{ max: 28512000000, value: 2592000000, name: 'month', prev: 'last month', in: 'in a month' } // max: 11 months
];

export = function ago(date: Date): string {

const inverted = Date.now() < date.getTime()
const diff = Math.abs(Date.now() - date.getTime());

// less than a minute
if (diff < 60000) return 'just now';

for (var i = 0; i < units.length; i++) {
if (diff < units[i].max) {
return format(diff, units[i].value, units[i].name, units[i].prev);
return format(diff, units[i].value, units[i].name, units[i].prev, units[i].in, inverted);
}
}

Expand All @@ -48,6 +51,6 @@ export = function ago(date: Date): string {
// name: 'year',
// prev: 'last year'
// }
return format(diff, 31536000000, 'year', 'last year');
return format(diff, 31536000000, 'year', 'last year', 'in a year', inverted);

};
167 changes: 136 additions & 31 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,102 +8,207 @@ test('just now', (t) => {
});

test('minute', (t) => {
const minute = new Date(timestamp - (60 * 1000));
const minute2 = new Date(timestamp - ((60 * 1000) + 5000));
const minute = new Date(timestamp.valueOf() - (60 * 1000));
const minute2 = new Date(timestamp.valueOf() - ((60 * 1000) + 5000));
t.is(ago(minute), 'a minute ago');
t.is(ago(minute2), 'a minute ago');
});

test('minute future', (t) => {
const timestamp = new Date();
const minute = new Date(timestamp.valueOf() + (60 * 1000));
const minute2 = new Date(timestamp.valueOf() + ((60 * 1000) + 5000));
t.is(ago(minute), 'in a minute');
t.is(ago(minute2), 'in a minute');
});

test('minutes', (t) => {
const minutes = new Date(timestamp - (2 * 60 * 1000));
const minutes2 = new Date(timestamp - (20 * 60 * 1000));
const minutes3 = new Date(timestamp - (45 * 60 * 1000));
const minutes = new Date(timestamp.valueOf() - (2 * 60 * 1000));
const minutes2 = new Date(timestamp.valueOf() - (20 * 60 * 1000));
const minutes3 = new Date(timestamp.valueOf() - (45 * 60 * 1000));
t.is(ago(minutes), '2 minutes ago');
t.is(ago(minutes2), '20 minutes ago');
t.is(ago(minutes3), '45 minutes ago');
});

test('minutes future', (t) => {
const timestamp = new Date();
const minutes = new Date(timestamp.valueOf() + (2 * 60 * 1000));
const minutes2 = new Date(timestamp.valueOf() + (20 * 60 * 1000));
const minutes3 = new Date(timestamp.valueOf() + (45 * 60 * 1000));
t.is(ago(minutes), 'in 2 minutes');
t.is(ago(minutes2), 'in 20 minutes');
t.is(ago(minutes3), 'in 45 minutes');
});

test('hour', (t) => {
const underHour = new Date(timestamp - (50 * 60 * 1000));
const hour = new Date(timestamp - (60 * 60 * 1000));
const underHour = new Date(timestamp.valueOf() - (50 * 60 * 1000));
const hour = new Date(timestamp.valueOf() - (60 * 60 * 1000));
t.is(ago(underHour), 'an hour ago');
t.is(ago(hour), 'an hour ago');
});

test('hour future', (t) => {
const timestamp = new Date();
const withinHour = new Date(timestamp.valueOf() + (50 * 60 * 1000));
const hour = new Date(timestamp.valueOf() + (60 * 60 * 1000));
t.is(ago(withinHour), 'in an hour');
t.is(ago(hour), 'in an hour');
});

test('hours', (t) => {
const hours = new Date(timestamp - (2 * 60 * 60 * 1000));
const hours2 = new Date(timestamp - (19 * 60 * 60 * 1000));
const hours = new Date(timestamp.valueOf() - (2 * 60 * 60 * 1000));
const hours2 = new Date(timestamp.valueOf() - (19 * 60 * 60 * 1000));
t.is(ago(hours), '2 hours ago');
t.is(ago(hours2), '19 hours ago');
});

test('yesterday', (t) => {
const yesterday = new Date(timestamp - (20 * 60 * 60 * 1000));
const yesterday = new Date(timestamp.valueOf() - (20 * 60 * 60 * 1000));
t.is(ago(yesterday), 'yesterday');
});

test('day future', (t) => {
const timestamp = new Date();
const yesterday = new Date(timestamp.valueOf() + (20 * 60 * 60 * 1000));
t.is(ago(yesterday), 'in a day');
});

test('days', (t) => {
const days = new Date(timestamp - (2 * 24 * 60 * 60 * 1000));
const days2 = new Date(timestamp - (5 * 24 * 60 * 60 * 1000));
const days = new Date(timestamp.valueOf() - (2 * 24 * 60 * 60 * 1000));
const days2 = new Date(timestamp.valueOf() - (5 * 24 * 60 * 60 * 1000));
t.is(ago(days), '2 days ago');
t.is(ago(days2), '5 days ago');
});

test('days future', (t) => {
const timestamp = new Date();
const days = new Date(timestamp.valueOf() + (2 * 24 * 60 * 60 * 1000));
const days2 = new Date(timestamp.valueOf() + (5 * 24 * 60 * 60 * 1000));
t.is(ago(days), 'in 2 days');
t.is(ago(days2), 'in 5 days');
});

test('week', (t) => {
const week = new Date(timestamp - (6 * 24 * 60 * 60 * 1000));
const week = new Date(timestamp.valueOf() - (6 * 24 * 60 * 60 * 1000));
t.is(ago(week), 'last week');
});

test('week future', (t) => {
const timestamp = new Date();
const week = new Date(timestamp.valueOf() + (6 * 24 * 60 * 60 * 1000));
t.is(ago(week), 'in a week');
});

test('weeks', (t) => {
const weeks = new Date(timestamp - (2 * 7 * 24 * 60 * 60 * 1000));
const weeks2 = new Date(timestamp - (3 * 7 * 24 * 60 * 60 * 1000));
const weeks = new Date(timestamp.valueOf() - (2 * 7 * 24 * 60 * 60 * 1000));
const weeks2 = new Date(timestamp.valueOf() - (3 * 7 * 24 * 60 * 60 * 1000));
t.is(ago(weeks), '2 weeks ago');
t.is(ago(weeks2), '3 weeks ago');
});

test('weeks future', (t) => {
const timestamp = new Date();
const weeks = new Date(timestamp.valueOf() + (2 * 7 * 24 * 60 * 60 * 1000));
const weeks2 = new Date(timestamp.valueOf() + (3 * 7 * 24 * 60 * 60 * 1000));
t.is(ago(weeks), 'in 2 weeks');
t.is(ago(weeks2), 'in 3 weeks');
});

test('month', (t) => {
const month = new Date(timestamp - (4 * 7 * 24 * 60 * 60 * 1000));
const month2 = new Date(timestamp - (30 * 24 * 60 * 60 * 1000));
const month = new Date(timestamp.valueOf() - (4 * 7 * 24 * 60 * 60 * 1000));
const month2 = new Date(timestamp.valueOf() - (30 * 24 * 60 * 60 * 1000));
t.is(ago(month), 'last month');
t.is(ago(month2), 'last month');
});

test('month future', (t) => {
const timestamp = new Date();
const month = new Date(timestamp.valueOf() + (4 * 7 * 24 * 60 * 60 * 1000));
const month2 = new Date(timestamp.valueOf() + (30 * 24 * 60 * 60 * 1000));
t.is(ago(month), 'in a month');
t.is(ago(month2), 'in a month');
});

test('months', (t) => {
const months = new Date(timestamp - (2 * 30 * 24 * 60 * 60 * 1000));
const months2 = new Date(timestamp - (10 * 30 * 24 * 60 * 60 * 1000));
const months = new Date(timestamp.valueOf() - (2 * 30 * 24 * 60 * 60 * 1000));
const months2 = new Date(timestamp.valueOf() - (10 * 30 * 24 * 60 * 60 * 1000));
t.is(ago(months), '2 months ago');
t.is(ago(months2), '10 months ago');
});

test('months future', (t) => {
const timestamp = new Date();
const months = new Date(timestamp.valueOf() + (2 * 30 * 24 * 60 * 60 * 1000));
const months2 = new Date(timestamp.valueOf() + (10 * 30 * 24 * 60 * 60 * 1000));
t.is(ago(months), 'in 2 months');
t.is(ago(months2), 'in 10 months');
});

test('year', (t) => {
const year = new Date(timestamp - (350 * 24 * 60 * 60 * 1000));
const year2 = new Date(timestamp - (1.5 * 360 * 24 * 60 * 60 * 1000));
const year = new Date(timestamp.valueOf() - (350 * 24 * 60 * 60 * 1000));
const year2 = new Date(timestamp.valueOf() - (1.5 * 360 * 24 * 60 * 60 * 1000));
t.is(ago(year), 'last year');
t.is(ago(year2), 'last year');
});

test('year future', (t) => {
const timestamp = new Date();
const year = new Date(timestamp.valueOf() + (350 * 24 * 60 * 60 * 1000));
const year2 = new Date(timestamp.valueOf() + (1.5 * 360 * 24 * 60 * 60 * 1000));
t.is(ago(year), 'in a year');
t.is(ago(year2), 'in a year');
});

test('years', (t) => {
const years = new Date(timestamp - (2 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp - (20 * 365 * 24 * 60 * 60 * 1000));
const years3 = new Date(timestamp - (100 * 365 * 24 * 60 * 60 * 1000));
const years4 = new Date(timestamp - (100000 * 365 * 24 * 60 * 60 * 1000));
const years = new Date(timestamp.valueOf() - (2 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp.valueOf() - (20 * 365 * 24 * 60 * 60 * 1000));
const years3 = new Date(timestamp.valueOf() - (100 * 365 * 24 * 60 * 60 * 1000));
const years4 = new Date(timestamp.valueOf() - (100000 * 365 * 24 * 60 * 60 * 1000));
t.is(ago(years), '2 years ago');
t.is(ago(years2), '20 years ago');
t.is(ago(years3), '100 years ago');
t.is(ago(years4), '100000 years ago');
});
test('years future', (t) => {
const timestamp = new Date();
const years = new Date(timestamp.valueOf() + (2 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp.valueOf() + (20 * 365 * 24 * 60 * 60 * 1000));
const years3 = new Date(timestamp.valueOf() + (100 * 365 * 24 * 60 * 60 * 1000));
const years4 = new Date(timestamp.valueOf() + (100000 * 365 * 24 * 60 * 60 * 1000));
t.is(ago(years), 'in 2 years');
t.is(ago(years2), 'in 20 years');
t.is(ago(years3), 'in 100 years');
t.is(ago(years4), 'in 100000 years');
});

test('boundaries', (t) => {
const minutes = new Date(timestamp - (1.8 * 60 * 1000));
const minutes2 = new Date(timestamp - (14.7 * 60 * 1000));
const hours = new Date(timestamp - (1.8 * 60 * 60 * 1000));
const hours2 = new Date(timestamp - (18.8 * 60 * 60 * 1000));
const years = new Date(timestamp - (1.8 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp - (14.7 * 365 * 24 * 60 * 60 * 1000));
const minutes = new Date(timestamp.valueOf() - (1.8 * 60 * 1000));
const minutes2 = new Date(timestamp.valueOf() - (14.7 * 60 * 1000));
const hours = new Date(timestamp.valueOf() - (1.8 * 60 * 60 * 1000));
const hours2 = new Date(timestamp.valueOf() - (18.8 * 60 * 60 * 1000));
const years = new Date(timestamp.valueOf() - (1.8 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp.valueOf() - (14.7 * 365 * 24 * 60 * 60 * 1000));
t.is(ago(minutes), '2 minutes ago');
t.is(ago(minutes2), '15 minutes ago');
t.is(ago(hours), '2 hours ago');
t.is(ago(hours2), '19 hours ago');
t.is(ago(years), '2 years ago');
t.is(ago(years2), '15 years ago');
});

test('boundaries future', (t) => {
const timestamp = new Date();
const minutes = new Date(timestamp.valueOf() + (1.8 * 60 * 1000));
const minutes2 = new Date(timestamp.valueOf() + (14.7 * 60 * 1000));
const hours = new Date(timestamp.valueOf() + (1.8 * 60 * 60 * 1000));
const hours2 = new Date(timestamp.valueOf() + (18.8 * 60 * 60 * 1000));
const years = new Date(timestamp.valueOf() + (1.8 * 365 * 24 * 60 * 60 * 1000));
const years2 = new Date(timestamp.valueOf() + (14.7 * 365 * 24 * 60 * 60 * 1000));
t.is(ago(minutes), 'in 2 minutes');
t.is(ago(minutes2), 'in 15 minutes');
t.is(ago(hours), 'in 2 hours');
t.is(ago(hours2), 'in 19 hours');
t.is(ago(years), 'in 2 years');
t.is(ago(years2), 'in 15 years');
});

0 comments on commit f7b67d3

Please sign in to comment.