Skip to content

Commit 40df413

Browse files
authored
Merge pull request #84 from Shazwazza/patch-1
Fixes #83 - parsing .NET duration without milliseconds throws error
2 parents 51956ba + 3a0bb83 commit 40df413

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {parseNetDuration} from '../../src/utils/parse-utils'
2+
3+
describe('parseNetDuration', () => {
4+
it('returns 0 for 00:00:00', () => {
5+
const ms = parseNetDuration('00:00:00')
6+
expect(ms).toBe(0)
7+
})
8+
9+
it('returns 0 for 00:00:00.0000000', () => {
10+
const ms = parseNetDuration('00:00:00.0000000')
11+
expect(ms).toBe(0)
12+
})
13+
14+
it('returns 123 for 00:00:00.123', () => {
15+
const ms = parseNetDuration('00:00:00.123')
16+
expect(ms).toBe(123)
17+
})
18+
19+
it('returns 12 * 1000 for 00:00:12', () => {
20+
const ms = parseNetDuration('00:00:12')
21+
expect(ms).toBe(12 * 1000)
22+
})
23+
24+
it('returns 12 * 60 * 1000 for 00:12:00', () => {
25+
const ms = parseNetDuration('00:12:00')
26+
expect(ms).toBe(12 * 60 * 1000)
27+
})
28+
29+
it('returns 12 * 60 * 60 * 1000 for 12:00:00', () => {
30+
const ms = parseNetDuration('12:00:00')
31+
expect(ms).toBe(12 * 60 * 60 * 1000)
32+
})
33+
34+
it('throws when string has invalid format', () => {
35+
expect(() => parseNetDuration('12:34:56 not a duration')).toThrowError(/^Invalid format/)
36+
})
37+
})

dist/index.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/parse-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export function parseNetDuration(str: string): number {
2-
// matches dotnet duration: 00:00:00.0010000
3-
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
2+
const durationRe = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)$/
43
const durationMatch = str.match(durationRe)
54
if (durationMatch === null) {
65
throw new Error(`Invalid format: "${str}" is not NET duration`)

0 commit comments

Comments
 (0)