Skip to content

Commit

Permalink
Merge pull request #84 from Shazwazza/patch-1
Browse files Browse the repository at this point in the history
Fixes #83 - parsing .NET duration without milliseconds throws error
  • Loading branch information
dorny authored Mar 23, 2021
2 parents 51956ba + 3a0bb83 commit 40df413
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
37 changes: 37 additions & 0 deletions __tests__/utils/parse-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {parseNetDuration} from '../../src/utils/parse-utils'

describe('parseNetDuration', () => {
it('returns 0 for 00:00:00', () => {
const ms = parseNetDuration('00:00:00')
expect(ms).toBe(0)
})

it('returns 0 for 00:00:00.0000000', () => {
const ms = parseNetDuration('00:00:00.0000000')
expect(ms).toBe(0)
})

it('returns 123 for 00:00:00.123', () => {
const ms = parseNetDuration('00:00:00.123')
expect(ms).toBe(123)
})

it('returns 12 * 1000 for 00:00:12', () => {
const ms = parseNetDuration('00:00:12')
expect(ms).toBe(12 * 1000)
})

it('returns 12 * 60 * 1000 for 00:12:00', () => {
const ms = parseNetDuration('00:12:00')
expect(ms).toBe(12 * 60 * 1000)
})

it('returns 12 * 60 * 60 * 1000 for 12:00:00', () => {
const ms = parseNetDuration('12:00:00')
expect(ms).toBe(12 * 60 * 60 * 1000)
})

it('throws when string has invalid format', () => {
expect(() => parseNetDuration('12:34:56 not a duration')).toThrowError(/^Invalid format/)
})
})
3 changes: 1 addition & 2 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/utils/parse-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export function parseNetDuration(str: string): number {
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
const durationRe = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)$/
const durationMatch = str.match(durationRe)
if (durationMatch === null) {
throw new Error(`Invalid format: "${str}" is not NET duration`)
Expand Down

0 comments on commit 40df413

Please sign in to comment.