-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite in typescript and update tests
- Loading branch information
Showing
13 changed files
with
2,792 additions
and
2,422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@istanbuljs/nyc-config-typescript" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>; |
Oops, something went wrong.