This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Improve formatting of route distance/duration #359
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
export function formatDuration(sec) { | ||
sec = Math.max(60, sec); // For duration < 60s, return '1 min' | ||
let min = Math.round(sec / 60); | ||
|
||
if (min < 60) { | ||
return `${min} min`; | ||
} | ||
|
||
const hour = Math.floor(min / 60); | ||
min = min - 60 * hour; | ||
let ret = `${hour} h`; | ||
if (min > 0 && hour < 10) { | ||
ret += ` ${min < 10 ? '0' : ''}${min} min`; | ||
} | ||
return ret; | ||
} | ||
|
||
export function formatDistance(m) { | ||
if (m > 99000) { | ||
return `${Math.round(m / 1000)} km`; | ||
} | ||
if (m > 1000) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had a debate about this but I still prefer using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seriously, this pyramid was ridiculous…
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That wasn't my point. I didn't like that pyramid either. |
||
return `${(m / 1000).toFixed(1).replace('.', ',')} km`; | ||
} | ||
if (m > 5) { | ||
return `${m.toFixed(0)} m`; | ||
} | ||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { formatDuration, formatDistance } from '../../src/libs/route_utils'; | ||
|
||
describe('route_utils', () => { | ||
describe('formatDuration', () => { | ||
const cases = [ | ||
{ seconds: 0, result: '1 min' }, | ||
{ seconds: 37, result: '1 min' }, | ||
{ seconds: 125, result: '2 min' }, | ||
{ seconds: 3600, result: '1 h' }, | ||
{ seconds: 5100, result: '1 h 25 min' }, | ||
{ seconds: 36000, result: '10 h' }, | ||
]; | ||
|
||
cases.map(({ seconds, result }) => | ||
test(`Formats ${seconds} seconds as '${result}'`, () => { | ||
expect(formatDuration(seconds)).toEqual(result); | ||
}) | ||
); | ||
}); | ||
|
||
describe('formatDistance', () => { | ||
const cases = [ | ||
{ meters: 0, result: '' }, | ||
{ meters: 15, result: '15 m' }, | ||
{ meters: 500, result: '500 m' }, | ||
{ meters: 1234, result: '1,2 km' }, | ||
{ meters: 9999, result: '10,0 km' }, | ||
{ meters: 123456, result: '123 km' }, | ||
]; | ||
|
||
cases.map(({ meters, result }) => | ||
test(`Formats ${meters} meters as '${result}'`, () => { | ||
expect(formatDistance(meters)).toEqual(result); | ||
}) | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised eslint didn't say that it should be const.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nevermind. Didn't see below. :)