Skip to content

Commit

Permalink
fix: update typings for mosTime, undefined was allowed in types, but …
Browse files Browse the repository at this point in the history
…not in code.
  • Loading branch information
nytamin committed Apr 15, 2024
1 parent dcf23cb commit fb2369a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
2 changes: 2 additions & 0 deletions packages/model/src/mosTypes/__tests__/mosTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 44 additions & 46 deletions packages/model/src/mosTypes/mosTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`)
}
Expand All @@ -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
Expand Down

0 comments on commit fb2369a

Please sign in to comment.