From 19f5e8afd126e0f0297abfbd0cd417ee32661d5b Mon Sep 17 00:00:00 2001 From: big213 Date: Mon, 16 Aug 2021 00:11:13 -0400 Subject: [PATCH] Resolve #23: Add support for hours conversion when auto-formatting times --- .../special/editPersonalBestInterface.vue | 19 +++++++++++++++---- frontend/models/personalBest.ts | 6 +++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/frontend/components/interface/crud/special/editPersonalBestInterface.vue b/frontend/components/interface/crud/special/editPersonalBestInterface.vue index 6116f16..594ef5d 100644 --- a/frontend/components/interface/crud/special/editPersonalBestInterface.vue +++ b/frontend/components/interface/crud/special/editPersonalBestInterface.vue @@ -165,7 +165,7 @@ 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+$/)) { @@ -173,7 +173,7 @@ export default { } // 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)) } }, @@ -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) { @@ -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() } diff --git a/frontend/models/personalBest.ts b/frontend/models/personalBest.ts index ef9bbc5..0baf1bc 100644 --- a/frontend/models/personalBest.ts +++ b/frontend/models/personalBest.ts @@ -118,11 +118,11 @@ export const 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' ) }, ], @@ -130,7 +130,7 @@ export const PersonalBest = >{ 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.