Skip to content

Commit

Permalink
allow max unit param
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiansandqvist committed Jun 3, 2020
1 parent d9b49c0 commit 78a2be0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![NPM version](https://img.shields.io/npm/v/s-ago.svg)](https://www.npmjs.com/package/s-ago) ![Dependencies](https://img.shields.io/david/sebastiansandqvist/s-ago.svg) [![build status](http://img.shields.io/travis/sebastiansandqvist/s-ago.svg)](https://travis-ci.org/sebastiansandqvist/s-ago) [![NPM license](https://img.shields.io/npm/l/s-ago.svg)](https://www.npmjs.com/package/s-ago)

This is the smallest, fully unit tested module to convert Date objects into human readable relative timestamps, such as `'4 minutes ago'`, `'yesterday'`, `'tomorrow'`, or `'in 3 months'`.
This is the smallest, fully unit tested module to convert Date objects into human readable relative timestamps, such as `'4 minutes ago'`, `'yesterday'`, `'tomorrow'`, or `'in 3 months'`. All in 22 lines of TypeScript.

You can optionally specify the maximum unit (eg. `hour`, `day`, `week`) so instead of outputting `'2 weeks ago'` you will see `'14 days ago'`.

## Usage
```javascript
Expand All @@ -14,6 +16,7 @@ var hoursAgo = new Date(now.getTime() - (6 * 60 * 60 * 1000));
var yesterday = new Date(now.getTime() - (24 * 60 * 60 * 1000));
var tomorrow = new Date(now.getTime() + (6 * 60 * 60 * 1000));
var inSixHours = new Date(now.getTime() + (6 * 60 * 60 * 1000));
var inTwoWeeks = new Date(now.getTime() + (2 * 7 * 24 * 60 * 60 * 1000));

// present
ago(now); // 'just now'
Expand All @@ -25,6 +28,10 @@ ago(hoursAgo); // '6 hours ago'
// future
ago(inSixHours); // 'in 6 hours'
ago(tomorrow); // 'tomorrow'

// max unit
ago(inTwoWeeks); // 'in 2 weeks'
ago(inTwoWeeks, 'day'); // 'in 14 days'
```

Output is as follows:
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare const _default: (date: Date) => string;
declare const _default: (date: Date, max?: string) => string;
export = _default;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ var units = [
{ max: 2419200000, value: 604800000, name: 'week', past: 'last week', future: 'in a week' },
{ max: 28512000000, value: 2592000000, name: 'month', past: 'last month', future: 'in a month' } // max: 11 months
];
module.exports = function ago(date) {
module.exports = function ago(date, max) {
var diff = Date.now() - date.getTime();
// less than a minute
if (Math.abs(diff) < 60000)
return 'just now';
for (var i = 0; i < units.length; i++) {
if (Math.abs(diff) < units[i].max) {
if (Math.abs(diff) < units[i].max || (max && units[i].name === max)) {
return format(diff, units[i].value, units[i].name, units[i].past, units[i].future, diff < 0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const units = [
{ max: 28512000000, value: 2592000000, name: 'month', past: 'last month', future: 'in a month' } // max: 11 months
];

export = function ago(date: Date): string {
export = function ago(date: Date, max?: string): string {
const diff = Date.now() - date.getTime();

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

for (var i = 0; i < units.length; i++) {
if (Math.abs(diff) < units[i].max) {
if (Math.abs(diff) < units[i].max || (max && units[i].name === max)) {
return format(diff, units[i].value, units[i].name, units[i].past, units[i].future, diff < 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s-ago",
"version": "2.1.0",
"version": "2.2.0",
"description": "Human readable relative times (eg. 4 minutes ago)",
"main": "index.js",
"types": "index.d.ts",
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ test('years', (t) => {
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));
Expand All @@ -182,6 +183,21 @@ test('years future', (t) => {
t.is(ago(years4), 'in 100000 years');
});

test('max param', (t) => {
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, 'day'), '14 days ago');
t.is(ago(weeks2, 'day'), '21 days ago');
});

test('max param 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, 'day'), 'in 14 days');
t.is(ago(weeks2, 'day'), 'in 21 days');
});

test('boundaries', (t) => {
const minutes = new Date(timestamp.valueOf() - (1.8 * 60 * 1000));
const minutes2 = new Date(timestamp.valueOf() - (14.7 * 60 * 1000));
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
"noUnusedLocals": true,
"strict": true
}
// "include": []
}

0 comments on commit 78a2be0

Please sign in to comment.