Skip to content

Commit

Permalink
Resolve #23: Add support
Browse files Browse the repository at this point in the history
for hours conversion when auto-formatting times
  • Loading branch information
big213 committed Aug 16, 2021
1 parent e07724c commit 19f5e8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ export default {
if (!val) return
// if pasted value matches the correct format, don't do anything
if (val.match(/^(\d+:)?[1-5]?\d\.\d{2}$/)) return
if (val.match(/^(\d+:)?([0-5]?\d:)?[1-5]?\d\.\d{2}$/)) return
// if val is 1 or more digits only, parse
if (val.match(/^\d+$/)) {
return this.setInputValue('timeElapsed', this.parseTimeString(val))
}
// if val is 1 digit off from a correct string, must be due to a keyboard action. parse
if (val.match(/^(\d+:)?[1-5]?\d\.\d{1,3}$/)) {
if (val.match(/^(\d+:)?(\d{1,2}:)?\d{1,2}\.\d{1,3}$/)) {
return this.setInputValue('timeElapsed', this.parseTimeString(val))
}
},
Expand All @@ -197,7 +197,7 @@ export default {
// remove : and ., then apply them at appropriate places
parseTimeString(str) {
const strParts = [...str.replace(/\./, '').replace(/:/, '')]
const strParts = [...str.replace(/\./g, '').replace(/:/g, '')]
// if length <= 2, pad with 0s so min length is 3
while (strParts.length < 3) {
Expand All @@ -213,9 +213,20 @@ export default {
}
// if length is greater than 5, add the ":"
// and get rid of any leading 0s
if (strParts.length > 5) {
strParts.splice(-5, 0, ':')
// if length is less than 9, no hours, so remove any leading 0s
if (strParts.length < 9) {
while (strParts[0] === '0') {
strParts.shift()
}
}
}
// if length is greater than 8, add another ":"
// and get rid of any leading 0s
if (strParts.length > 8) {
strParts.splice(-8, 0, ':')
while (strParts[0] === '0') {
strParts.shift()
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/models/personalBest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
hint: 'Type in the numbers only, the numbers will be auto-formatted',
inputRules: [
(value) => {
const regEx = /^(\d+:)?[1-5]?\d\.\d{2}$/
const regEx = /^(\d+:)?([0-5]?\d:)?[1-5]?\d\.\d{2}$/
return (
!value ||
regEx.test(value) ||
'Invalid Time Format, must be like 1234:56.78'
'Invalid Time Format, must be like 12:34:56.78'
)
},
],
serialize: serializeTime,
parseValue: (value) => {
if (!value) return null
if (typeof value !== 'string') throw new Error('Invalid value')
const regEx = /^(\d+:)?\d{1,2}\.\d{2}$/
const regEx = /^(\d+:)?([0-5]?\d:)?[1-5]?\d\.\d{2}$/
if (!regEx.test(value)) throw new Error('Invalid value')

// convert string to number of ms.
Expand Down

0 comments on commit 19f5e8a

Please sign in to comment.