Skip to content

Commit

Permalink
rendering negative time in timeutils.renderSpan (#4489)
Browse files Browse the repository at this point in the history
* rendering negative time

* adding negative sign
  • Loading branch information
patnir authored Dec 18, 2023
1 parent 9171d09 commit 569dd6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ironfish/src/utils/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ describe('TimeUtils', () => {
expect(TimeUtils.renderSpan(330000)).toEqual('5m 30s')
expect(TimeUtils.renderSpan(7530000)).toEqual('2h 5m')
})

it('should render negative times', () => {
expect(TimeUtils.renderSpan(-0.005)).toEqual('-0.005ms')
expect(TimeUtils.renderSpan(-0)).toEqual('0ms')
expect(TimeUtils.renderSpan(-1000)).toEqual('-1s')
expect(TimeUtils.renderSpan(-1010)).toEqual('-1s 10ms')
expect(TimeUtils.renderSpan(-1150)).toEqual('-1s 150ms')
expect(TimeUtils.renderSpan(-330000)).toEqual('-5m 30s')
expect(TimeUtils.renderSpan(-7530000)).toEqual('-2h 5m')
})
})
})
10 changes: 9 additions & 1 deletion ironfish/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ const renderSpan = (
hideMilliseconds?: boolean
},
): string => {
const isNegative = time < 0

time = Math.abs(time)

if (time < 1) {
return `${MathUtils.round(time, 4)}ms`
return `${isNegative ? '-' : ''}${MathUtils.round(time, 4)}ms`
}

const parts = []
Expand Down Expand Up @@ -87,6 +91,10 @@ const renderSpan = (
magnitude = Math.max(magnitude, 1)
}

if (isNegative && parts.length > 0) {
parts[0] = `-${parts[0]}`
}

return parts.join(' ')
}

Expand Down

0 comments on commit 569dd6c

Please sign in to comment.