Skip to content

Commit

Permalink
Fix NaN microseconds and nanoseconds and require Node.js 18 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 7, 2024
1 parent 2f907a6 commit b222100
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export interface TimeComponents {
export type TimeComponents = {
days: number;
hours: number;
minutes: number;
seconds: number;
milliseconds: number;
microseconds: number;
nanoseconds: number;
}
};

/**
Parse milliseconds into an object.
Expand Down
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const toZeroIfInfinity = value => Number.isFinite(value) ? value : 0;

export default function parseMilliseconds(milliseconds) {
if (typeof milliseconds !== 'number') {
throw new TypeError('Expected a number');
}

return {
days: Math.trunc(milliseconds / 86400000),
hours: Math.trunc(milliseconds / 3600000) % 24,
minutes: Math.trunc(milliseconds / 60000) % 60,
seconds: Math.trunc(milliseconds / 1000) % 60,
milliseconds: Math.trunc(milliseconds) % 1000,
microseconds: Math.trunc(milliseconds * 1000) % 1000,
nanoseconds: Math.trunc(milliseconds * 1e6) % 1000
days: Math.trunc(milliseconds / 86_400_000),
hours: Math.trunc(milliseconds / 3_600_000 % 24),
minutes: Math.trunc(milliseconds / 60_000 % 60),
seconds: Math.trunc(milliseconds / 1000 % 60),
milliseconds: Math.trunc(milliseconds % 1000),
microseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1000) % 1000),
nanoseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1e6) % 1000),
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import parseMilliseconds, {TimeComponents} from './index.js';
import parseMilliseconds, {type TimeComponents} from './index.js';

const components: TimeComponents = parseMilliseconds(3000);

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,8 +36,8 @@
"interval"
],
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"ava": "^6.0.1",
"tsd": "^0.30.3",
"xo": "^0.56.0"
}
}
48 changes: 34 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('parse milliseconds into an object', t => {
seconds: 1,
milliseconds: 400,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 55), {
Expand All @@ -19,7 +19,7 @@ test('parse milliseconds into an object', t => {
seconds: 55,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 67), {
Expand All @@ -29,7 +29,7 @@ test('parse milliseconds into an object', t => {
seconds: 7,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 60 * 5), {
Expand All @@ -39,7 +39,7 @@ test('parse milliseconds into an object', t => {
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 60 * 67), {
Expand All @@ -49,7 +49,7 @@ test('parse milliseconds into an object', t => {
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 60 * 60 * 12), {
Expand All @@ -59,7 +59,7 @@ test('parse milliseconds into an object', t => {
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 60 * 60 * 40), {
Expand All @@ -69,7 +69,7 @@ test('parse milliseconds into an object', t => {
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(1000 * 60 * 60 * 999), {
Expand All @@ -79,27 +79,47 @@ test('parse milliseconds into an object', t => {
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds((1000 * 60) + 500 + 0.345678), {
t.deepEqual(parseMilliseconds((1000 * 60) + 500 + 0.345_678), {
days: 0,
hours: 0,
minutes: 1,
seconds: 0,
milliseconds: 500,
microseconds: 345,
nanoseconds: 678
nanoseconds: 678,
});

t.deepEqual(parseMilliseconds(0.000543), {
t.deepEqual(parseMilliseconds(0.000_543), {
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 543
nanoseconds: 543,
});

t.deepEqual(parseMilliseconds(Number.MAX_VALUE), {
days: 2.080_663_350_535_087_5e+300,
hours: 8,
minutes: 8,
seconds: 48,
milliseconds: 368,
microseconds: 0,
nanoseconds: 0,
});

t.deepEqual(parseMilliseconds(Number.MIN_VALUE), {
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0,
});
});

Expand All @@ -109,7 +129,7 @@ test('handle negative millisecond values', t => {
'hours',
'minutes',
'seconds',
'milliseconds'
'milliseconds',
];

const times = [
Expand All @@ -122,7 +142,7 @@ test('handle negative millisecond values', t => {
1000 * 60 * 67,
1000 * 60 * 60 * 12,
1000 * 60 * 60 * 40,
1000 * 60 * 60 * 999
1000 * 60 * 60 * 999,
];

for (const milliseconds of times) {
Expand Down

0 comments on commit b222100

Please sign in to comment.