Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
nam-hle authored Jun 6, 2022
1 parent a84befb commit c65a1bb
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 153 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
119 changes: 58 additions & 61 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,80 @@
declare namespace prettyMilliseconds {
interface Options {
/**
Number of digits to appear after the seconds decimal point.
export interface Options {
/**
Number of digits to appear after the seconds decimal point.
@default 1
*/
readonly secondsDecimalDigits?: number;
@default 1
*/
readonly secondsDecimalDigits?: number;

/**
Number of digits to appear after the milliseconds decimal point.
/**
Number of digits to appear after the milliseconds decimal point.
Useful in combination with [`process.hrtime()`](https://nodejs.org/api/process.html#process_process_hrtime).
Useful in combination with [`process.hrtime()`](https://nodejs.org/api/process.html#process_process_hrtime).
@default 0
*/
readonly millisecondsDecimalDigits?: number;
@default 0
*/
readonly millisecondsDecimalDigits?: number;

/**
Keep milliseconds on whole seconds: `13s` → `13.0s`.
/**
Keep milliseconds on whole seconds: `13s` → `13.0s`.
Useful when you are showing a number of seconds spent on an operation and don't want the width of the output to change when hitting a whole number.
Useful when you are showing a number of seconds spent on an operation and don't want the width of the output to change when hitting a whole number.
@default false
*/
readonly keepDecimalsOnWholeSeconds?: boolean;
@default false
*/
readonly keepDecimalsOnWholeSeconds?: boolean;

/**
Only show the first unit: `1h 10m` → `1h`.
/**
Only show the first unit: `1h 10m` → `1h`.
Also ensures that `millisecondsDecimalDigits` and `secondsDecimalDigits` are both set to `0`.
Also ensures that `millisecondsDecimalDigits` and `secondsDecimalDigits` are both set to `0`.
@default false
*/
readonly compact?: boolean;
@default false
*/
readonly compact?: boolean;

/**
Number of units to show. Setting `compact` to `true` overrides this option.
/**
Number of units to show. Setting `compact` to `true` overrides this option.
@default Infinity
*/
readonly unitCount?: number;
@default Infinity
*/
readonly unitCount?: number;

/**
Use full-length units: `5h 1m 45s` → `5 hours 1 minute 45 seconds`.
/**
Use full-length units: `5h 1m 45s` → `5 hours 1 minute 45 seconds`.
@default false
*/
readonly verbose?: boolean;
@default false
*/
readonly verbose?: boolean;

/**
Show milliseconds separately. This means they won't be included in the decimal part of the seconds.
/**
Show milliseconds separately. This means they won't be included in the decimal part of the seconds.
@default false
*/
readonly separateMilliseconds?: boolean;
@default false
*/
readonly separateMilliseconds?: boolean;

/**
Show microseconds and nanoseconds.
/**
Show microseconds and nanoseconds.
@default false
*/
readonly formatSubMilliseconds?: boolean;
@default false
*/
readonly formatSubMilliseconds?: boolean;

/**
Display time using colon notation: `5h 1m 45s` → `5:01:45`. Always shows time in at least minutes: `1s` → `0:01`
/**
Display time using colon notation: `5h 1m 45s` → `5:01:45`. Always shows time in at least minutes: `1s` → `0:01`
Useful when you want to display time without the time units, similar to a digital watch.
Useful when you want to display time without the time units, similar to a digital watch.
Setting `colonNotation` to `true` overrides the following options to `false`:
- `compact`
- `formatSubMilliseconds`
- `separateMilliseconds`
- `verbose`
Setting `colonNotation` to `true` overrides the following options to `false`:
- `compact`
- `formatSubMilliseconds`
- `separateMilliseconds`
- `verbose`
@default false
*/
readonly colonNotation?: boolean;
}
@default false
*/
readonly colonNotation?: boolean;
}

/**
Expand All @@ -86,7 +84,7 @@ Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 2
@example
```
import prettyMilliseconds = require('pretty-ms');
import prettyMilliseconds from 'pretty-ms';
prettyMilliseconds(1337000000);
//=> '15d 11h 23m 20s'
Expand Down Expand Up @@ -118,9 +116,8 @@ prettyMilliseconds(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))
//=> '35m'
```
*/
declare function prettyMilliseconds(
export default function prettyMilliseconds(
milliseconds: number,
options?: prettyMilliseconds.Options
options?: Options
): string;

export = prettyMilliseconds;
63 changes: 31 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
const parseMilliseconds = require('parse-ms');
import parseMilliseconds from 'parse-ms';

const pluralize = (word, count) => count === 1 ? word : `${word}s`;

const SECOND_ROUNDING_EPSILON = 0.0000001;
const SECOND_ROUNDING_EPSILON = 0.000_000_1;

module.exports = (milliseconds, options = {}) => {
export default function prettyMilliseconds(milliseconds, options = {}) {
if (!Number.isFinite(milliseconds)) {
throw new TypeError('Expected a finite number');
}
Expand Down Expand Up @@ -60,52 +59,52 @@ module.exports = (milliseconds, options = {}) => {
add(parsed.minutes, 'minute', 'm');

if (
options.separateMilliseconds ||
options.formatSubMilliseconds ||
(!options.colonNotation && milliseconds < 1000)
options.separateMilliseconds
|| options.formatSubMilliseconds
|| (!options.colonNotation && milliseconds < 1000)
) {
add(parsed.seconds, 'second', 's');
if (options.formatSubMilliseconds) {
add(parsed.milliseconds, 'millisecond', 'ms');
add(parsed.microseconds, 'microsecond', 'µs');
add(parsed.nanoseconds, 'nanosecond', 'ns');
} else {
const millisecondsAndBelow =
parsed.milliseconds +
(parsed.microseconds / 1000) +
(parsed.nanoseconds / 1e6);
const millisecondsAndBelow
= parsed.milliseconds
+ (parsed.microseconds / 1000)
+ (parsed.nanoseconds / 1e6);

const millisecondsDecimalDigits =
typeof options.millisecondsDecimalDigits === 'number' ?
options.millisecondsDecimalDigits :
0;
const millisecondsDecimalDigits
= typeof options.millisecondsDecimalDigits === 'number'
? options.millisecondsDecimalDigits
: 0;

const roundedMiliseconds = millisecondsAndBelow >= 1 ?
Math.round(millisecondsAndBelow) :
Math.ceil(millisecondsAndBelow);
const roundedMiliseconds = millisecondsAndBelow >= 1
? Math.round(millisecondsAndBelow)
: Math.ceil(millisecondsAndBelow);

const millisecondsString = millisecondsDecimalDigits ?
millisecondsAndBelow.toFixed(millisecondsDecimalDigits) :
roundedMiliseconds;
const millisecondsString = millisecondsDecimalDigits
? millisecondsAndBelow.toFixed(millisecondsDecimalDigits)
: roundedMiliseconds;

add(
Number.parseFloat(millisecondsString, 10),
Number.parseFloat(millisecondsString),
'millisecond',
'ms',
millisecondsString
millisecondsString,
);
}
} else {
const seconds = (milliseconds / 1000) % 60;
const secondsDecimalDigits =
typeof options.secondsDecimalDigits === 'number' ?
options.secondsDecimalDigits :
1;
const secondsDecimalDigits
= typeof options.secondsDecimalDigits === 'number'
? options.secondsDecimalDigits
: 1;
const secondsFixed = floorDecimals(seconds, secondsDecimalDigits);
const secondsString = options.keepDecimalsOnWholeSeconds ?
secondsFixed :
secondsFixed.replace(/\.0+$/, '');
add(Number.parseFloat(secondsString, 10), 'second', 's', secondsString);
const secondsString = options.keepDecimalsOnWholeSeconds
? secondsFixed
: secondsFixed.replace(/\.0+$/, '');
add(Number.parseFloat(secondsString), 'second', 's', secondsString);
}

if (result.length === 0) {
Expand All @@ -122,4 +121,4 @@ module.exports = (milliseconds, options = {}) => {
}

return options.colonNotation ? result.join('') : result.join(' ');
};
}
20 changes: 10 additions & 10 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {expectType} from 'tsd';
import prettyMilliseconds = require('.');
import prettyMilliseconds from './index.js';

expectType<string>(prettyMilliseconds(1335669000));
expectType<string>(prettyMilliseconds(1335669000, {secondsDecimalDigits: 1}));
expectType<string>(prettyMilliseconds(1_335_669_000));
expectType<string>(prettyMilliseconds(1_335_669_000, {secondsDecimalDigits: 1}));
expectType<string>(
prettyMilliseconds(1335669000, {millisecondsDecimalDigits: 2})
prettyMilliseconds(1_335_669_000, {millisecondsDecimalDigits: 2}),
);
expectType<string>(
prettyMilliseconds(1335669000, {keepDecimalsOnWholeSeconds: true})
prettyMilliseconds(1_335_669_000, {keepDecimalsOnWholeSeconds: true}),
);
expectType<string>(prettyMilliseconds(1337, {compact: true}));
expectType<string>(prettyMilliseconds(1335669000, {unitCount: 2}));
expectType<string>(prettyMilliseconds(1335669000, {verbose: true}));
expectType<string>(prettyMilliseconds(1_335_669_000, {unitCount: 2}));
expectType<string>(prettyMilliseconds(1_335_669_000, {verbose: true}));
expectType<string>(
prettyMilliseconds(1335669000, {separateMilliseconds: true})
prettyMilliseconds(1_335_669_000, {separateMilliseconds: true}),
);
expectType<string>(
prettyMilliseconds(1335669000, {formatSubMilliseconds: true})
prettyMilliseconds(1_335_669_000, {formatSubMilliseconds: true}),
);
expectType<string>(
prettyMilliseconds(1335669000, {colonNotation: true})
prettyMilliseconds(1_335_669_000, {colonNotation: true}),
);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -39,11 +41,11 @@
"hrtime"
],
"dependencies": {
"parse-ms": "^2.1.0"
"parse-ms": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"ava": "^3.15.0",
"tsd": "^0.19.0",
"xo": "^0.47.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install pretty-ms
## Usage

```js
const prettyMilliseconds = require('pretty-ms');
import prettyMilliseconds from 'pretty-ms';

prettyMilliseconds(1337000000);
//=> '15d 11h 23m 20s'
Expand Down
Loading

0 comments on commit c65a1bb

Please sign in to comment.