remove auto conversion of XML attributes based on value

This commit is contained in:
Michal Dorner 2021-01-24 21:05:34 +01:00
parent 1a9ca69197
commit 40b5f476c7
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
6 changed files with 44 additions and 55 deletions

20
src/utils/parse-utils.ts Normal file
View file

@ -0,0 +1,20 @@
export function parseNetDuration(str: string): number {
// matches dotnet duration: 00:00:00.0010000
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`)
}
const [_, hourStr, minStr, secStr] = durationMatch
return (parseInt(hourStr) * 3600 + parseInt(minStr) * 60 + parseFloat(secStr)) * 1000
}
export function parseIsoDate(str: string): Date {
const isoDateRe = /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/
if (str === undefined || !isoDateRe.test(str)) {
throw new Error(`Invalid format: "${str}" is not ISO date`)
}
return new Date(str)
}

View file

@ -1,27 +0,0 @@
const isoDateRe = /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
export function parseAttribute(str: string | undefined): string | Date | number | undefined {
if (str === '' || str === undefined) {
return str
}
if (isoDateRe.test(str)) {
return new Date(str)
}
const durationMatch = str.match(durationRe)
if (durationMatch !== null) {
const [_, hourStr, minStr, secStr] = durationMatch
return (parseInt(hourStr) * 3600 + parseInt(minStr) * 60 + parseFloat(secStr)) * 1000
}
const num = parseFloat(str)
if (isNaN(num)) {
return str
}
return num
}