From fb2369abb3a876675c7e941092f96460e2da45f8 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Mon, 15 Apr 2024 09:22:51 +0200 Subject: [PATCH] fix: update typings for mosTime, undefined was allowed in types, but not in code. --- .../src/mosTypes/__tests__/mosTime.spec.ts | 2 + packages/model/src/mosTypes/mosTime.ts | 90 +++++++++---------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/packages/model/src/mosTypes/__tests__/mosTime.spec.ts b/packages/model/src/mosTypes/__tests__/mosTime.spec.ts index 99202678..97d30fe3 100644 --- a/packages/model/src/mosTypes/__tests__/mosTime.spec.ts +++ b/packages/model/src/mosTypes/__tests__/mosTime.spec.ts @@ -59,6 +59,8 @@ describe('MosTime', () => { // @ts-expect-error bad input expect(() => mosTypes.mosTime.create(null)).toThrowError(/Invalid input/) // @ts-expect-error bad input + expect(() => mosTypes.mosTime.create(undefined)).toThrowError(/Invalid input/) + // @ts-expect-error bad input expect(() => mosTypes.mosTime.create(false)).toThrowError(/Invalid input/) // test input format date, number and various strings diff --git a/packages/model/src/mosTypes/mosTime.ts b/packages/model/src/mosTypes/mosTime.ts index aff753e2..e6b6988b 100644 --- a/packages/model/src/mosTypes/mosTime.ts +++ b/packages/model/src/mosTypes/mosTime.ts @@ -15,52 +15,50 @@ export function create(timestamp: AnyValue, strict: boolean): IMOSTime { let _timezone = '' let _timezoneOffset = 0 - if (timestamp !== undefined) { - // create date from time-string or timestamp number - if (typeof timestamp === 'number') { + if (timestamp === undefined) throw new Error(`MosTime: Invalid input: "${timestamp}"`) + + // create date from time-string or timestamp number + if (typeof timestamp === 'number') { + time = new Date(timestamp) + } else if (typeof timestamp === 'string') { + // formats: + // YYYY-MM-DD'T'hh:mm:ss[,ddd]['Z'] + // Sun Feb 25 2018 08:59:08 GMT+0100 (CET) + // 2018-02-25T08:00:45.528Z + + let _timezoneZuluIndicator = '' + let _timezoneDeclaration = '' + + // parse out custom Z indicator (mos-centric) + const customFormatParseResult = parseMosCustomFormat(timestamp) + // parse out custom timezones (mos local-local centric format) + const timezoneParseResult = parseTimeOffset(timestamp) + + if (customFormatParseResult !== false) { + _timezone = customFormatParseResult.timezoneIndicator + _timezoneOffset = customFormatParseResult.timezoneOffset + _timezoneZuluIndicator = customFormatParseResult.timezoneIndicator + + const r = customFormatParseResult + const dateStr = `${r.yy}-${r.mm}-${r.dd}T${r.hh}:${r.ii}:${r.ss}${ + r.ms ? '.' + r.ms : '' + }${_timezoneZuluIndicator}${_timezoneDeclaration}` + time = new Date(dateStr) + } else if (timezoneParseResult !== false) { + _timezoneDeclaration = timezoneParseResult.timezoneDeclaration + + time = new Date(timestamp) + } else { + // try to parse the time directly with Date, for Date-supported formats time = new Date(timestamp) - } else if (typeof timestamp === 'string') { - // formats: - // YYYY-MM-DD'T'hh:mm:ss[,ddd]['Z'] - // Sun Feb 25 2018 08:59:08 GMT+0100 (CET) - // 2018-02-25T08:00:45.528Z - - let _timezoneZuluIndicator = '' - let _timezoneDeclaration = '' - - // parse out custom Z indicator (mos-centric) - const customFormatParseResult = parseMosCustomFormat(timestamp) - // parse out custom timezones (mos local-local centric format) - const timezoneParseResult = parseTimeOffset(timestamp) - - if (customFormatParseResult !== false) { - _timezone = customFormatParseResult.timezoneIndicator - _timezoneOffset = customFormatParseResult.timezoneOffset - _timezoneZuluIndicator = customFormatParseResult.timezoneIndicator - - const r = customFormatParseResult - const dateStr = `${r.yy}-${r.mm}-${r.dd}T${r.hh}:${r.ii}:${r.ss}${ - r.ms ? '.' + r.ms : '' - }${_timezoneZuluIndicator}${_timezoneDeclaration}` - time = new Date(dateStr) - } else if (timezoneParseResult !== false) { - _timezoneDeclaration = timezoneParseResult.timezoneDeclaration - - time = new Date(timestamp) - } else { - // try to parse the time directly with Date, for Date-supported formats - time = new Date(timestamp) - } - } else if (typeof timestamp === 'object') { - if (timestamp instanceof Date) { - time = timestamp - } else if (timestamp?._mosTime !== undefined) { - time = new Date(timestamp._mosTime) - } else if (!strict) { - time = new Date() - } else { - throw new Error(`MosTime: Invalid input: "${timestamp}"`) - } + } + } else if (typeof timestamp === 'object') { + if (timestamp instanceof Date) { + time = timestamp + } else if (timestamp?._mosTime !== undefined) { + time = new Date(timestamp._mosTime) + } else if (!strict) { + time = new Date() } else { throw new Error(`MosTime: Invalid input: "${timestamp}"`) } @@ -80,7 +78,7 @@ export function create(timestamp: AnyValue, strict: boolean): IMOSTime { validate(iMosTime, strict) return iMosTime } -export type AnyValue = Date | number | string | IMOSTime | undefined +export type AnyValue = Date | number | string | IMOSTime export function validate(_mosDuration: IMOSTime, _strict: boolean): void { // nothing