Skip to content

Commit

Permalink
use typescript, simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiansandqvist committed Sep 8, 2018
1 parent 90394ef commit 42bda34
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 58 deletions.
29 changes: 1 addition & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![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 (26 SLOC) to convert Date objects into human readable relative timestamps, such as '4 minutes ago' or 'yesterday'.
This is the smallest, fully unit tested module (25 SLOC) to convert Date objects into human readable relative timestamps, such as '4 minutes ago' or 'yesterday'.

## Usage
```javascript
Expand Down Expand Up @@ -33,30 +33,3 @@ Less than 1 minute | `just now`
2-11 months | # `months ago`
11-23 months | `last year`
2+ years | # `years ago`

Don't like the thresholds or output? You can customize them. Thresholds are set via each object's `max` key, and the output format for the previous 1 unit of time (eg. `yesterday` or `last week`) is set in each object's `prev` key.

The default is as follows:
```javascript
ago.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: Infinity, value: 31536000000, name: 'year', prev: 'last year' }
];
```

If you want it to display `a week ago` instead of `last week` (and do the same for months and years), you would set ago.units to the following before running `ago`.

```javascript
ago.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: 'a week ago' }, // max: 28 days
{ max: 28512000000, value: 2592000000, name: 'month', prev: 'a month ago' }, // max: 11 months
{ max: Infinity, value: 31536000000, name: 'year', prev: 'a year ago' }
];
```
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function ago(date: Date): string;
57 changes: 29 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
/*
Usage:
Expand All @@ -13,36 +14,36 @@ ago(yesterday); // 'yesterday'
ago(hoursAgo); // '6 hours ago'
*/

exports.__esModule = true;
function format(diff, divisor, unit, prev) {
var val = Math.round(diff / divisor);
return val <= 1 ? prev : val + ' ' + unit + 's ago';
var val = Math.round(diff / divisor);
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
];
function ago(date) {

var diff = Math.abs(Date.now() - date.getTime());
var units = ago.units;

if (diff < 60000) { // less than a minute
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);
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);
}
}
}

// `year` is the final unit.
// same as:
// {
// max: Infinity,
// value: 31536000000,
// name: 'year',
// prev: 'last year'
// }
return format(diff, 31536000000, 'year', 'last year');
}

ago.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: Infinity, value: 31536000000, name: 'year', prev: 'last year' }
];

module.exports = ago;
exports["default"] = ago;
53 changes: 53 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Usage:
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));
ago(now); // 'just now'
ago(yesterday); // 'yesterday'
ago(hoursAgo); // '6 hours ago'
*/

function format(diff: number, divisor: number, unit: string, prev: string) {
var val = Math.round(diff / divisor);
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
];

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

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);
}
}

// `year` is the final unit.
// same as:
// {
// max: Infinity,
// value: 31536000000,
// name: 'year',
// prev: 'last year'
// }
return format(diff, 31536000000, 'year', 'last year');

}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"version": "1.3.0",
"description": "Human readable relative times (eg. 4 minutes ago)",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"coverage": "nyc --reporter=lcov --reporter=text ava",
"test": "ava"
"build": "tsc --declaration index.ts",
"coverage": "npm run build && nyc --reporter=lcov --reporter=text ava",
"test": "npm run build && ava"
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"lib": ["es2015"],
"noUnusedLocals": true,
"strict": true
}
// "include": []
}

0 comments on commit 42bda34

Please sign in to comment.