Skip to content

Commit

Permalink
rewrite in typescript and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aurbano committed Nov 18, 2019
1 parent c7022df commit eb1daf8
Show file tree
Hide file tree
Showing 13 changed files with 2,792 additions and 2,422 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.md]
trim_trailing_whitespace = false
insert_final_newline = false
3 changes: 3 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@istanbuljs/nyc-config-typescript"
}
75 changes: 75 additions & 0 deletions dist/index.js

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

1 change: 1 addition & 0 deletions dist/index.js.map

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

3 changes: 3 additions & 0 deletions dist/types.js

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

1 change: 1 addition & 0 deletions dist/types.js.map

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

107 changes: 0 additions & 107 deletions index.js

This file was deleted.

23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "readable-timespan",
"version": "1.2.0",
"description": "Human readable timespans",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "nyc ava"
},
"repository": {
Expand All @@ -27,11 +29,20 @@
},
"homepage": "https://github.com/aurbano/readable-timespan",
"devDependencies": {
"ava": "^0.17.0",
"coveralls": "^2.11.15",
"nyc": "^10.0.0"
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"ava": "^2.4.0",
"coveralls": "^3.0.7",
"nyc": "^14.1.1",
"ts-node": "^8.5.2",
"typescript": "^3.7.2"
},
"dependencies": {
"extend": "^3.0.0"
"ava": {
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}
}
99 changes: 99 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Options, Increments } from './types';
/**
* readable-timespans
* Get human-readable timespans from time intervals in milliseconds
*
* @author Alejandro U. Alvarez <https://aurbano.eu>
* @license MIT
*/

/**
* Adjust this definition if you want
* this assumes all months have 30 days, every year
* 365 days, and every month 4 weeks. Since it is only
* to give a very rough estimate of the time elapsed it should
* be fine though.
*/
const MILLISECOND = 1,
SECOND = 1000 * MILLISECOND,
MINUTE = 60 * SECOND,
HOUR = 60 * MINUTE,
DAY = 24 * HOUR,
WEEK = 7 * DAY,
MONTH = 30 * DAY,
YEAR = 365 * DAY;

const defaultSettings: Options = {
lessThanFirst: 'now',
millisecond: 'millisecond',
second: 'second',
minute: 'minute',
hour: 'hour',
day: 'day',
week: 'week',
month: 'month',
year: 'year',
now: 'now',
space: true,
pluralize: true
};

export class Timespan {
settings: Options;
increments: Increments;

constructor(options?: Options) {
this.settings = {
...defaultSettings,
...options,
};

this.increments = [
[MILLISECOND, this.settings.millisecond],
[SECOND, this.settings.second],
[MINUTE, this.settings.minute],
[HOUR, this.settings.hour],
[DAY, this.settings.day],
[WEEK, this.settings.week],
[MONTH, this.settings.month],
[YEAR, this.settings.year],
];
};

public parse(diff: number): string {
let plural = '',
space = ' ',
checkValid = 0,
units = Math.ceil(diff / this.increments[this.increments.length - 1][0]),
unit = this.increments[this.increments.length - 1][1];

// Handle units smaller than the first increment
while (!this.increments[checkValid][1]) {
checkValid++;
}

if (diff < this.increments[checkValid][0]) {
return this.settings.lessThanFirst;
}

for (let i = 1; i < this.increments.length; i++) {
if (!this.increments[i - 1][1]) continue;

if (this.increments[i - 1][0] <= diff && diff < this.increments[i][0]) {
units = Math.ceil(diff / this.increments[i - 1][0]);
unit = this.increments[i - 1][1];
break;
}
}

if (units > 1 && this.settings.pluralize) {
plural = 's';
}

if (!this.settings.space) space = '';

return units + space + unit + plural;
}
}

export default Timespan;
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type Options = {
lessThanFirst?: string,
millisecond?: string | false,
second?: string | false,
minute?: string | false,
hour?: string | false,
day?: string | false,
week?: string | false,
month?: string | false,
year?: string | false,
now?: string,
space?: boolean,
pluralize?: boolean,
};

export type Increments = Array<[number, string | false]>;
Loading

0 comments on commit eb1daf8

Please sign in to comment.