-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(transformField): refactor format funcs, overload typescript defs
- Loading branch information
Showing
3 changed files
with
67 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,74 @@ | ||
export default function transformDate(input:any, format:string): Date { | ||
if (format === 'epoch') { | ||
const formats: {[key:string]:Function} = { | ||
|
||
epoch(input: number) { | ||
if (typeof input !== 'number') | ||
throw new Error("convertDate(input, format), format=epoch but typeof input !== 'number'"); | ||
throw new Error("transformField(input, format), format=epoch but typeof input !== 'number'"); | ||
// TODO, check range? 1604620800 | ||
return new Date(input * 1000); | ||
} | ||
}, | ||
|
||
if (format === 'dateObj') { | ||
dateObj(input: { raw: number; fmt?: string }) { | ||
if (typeof input !== 'object') | ||
throw new Error("convertDate(input, format), format=dateObj but typeof input !== 'object'"); | ||
throw new Error("transformField(input, format), format=dateObj but typeof input !== 'object'"); | ||
if (typeof input.raw !== 'number') | ||
throw new Error("convertDate(input, format), format=dateObj but typeof input.raw !== 'number'"); | ||
throw new Error("transformField(input, format), format=dateObj but typeof input.raw !== 'number'"); | ||
return new Date(input.raw * 1000); | ||
} | ||
}, | ||
|
||
// 2020-01-01 | ||
if (format === 'dateStr') { | ||
dateStr(input: string) { | ||
// 2020-01-01 | ||
if (typeof input !== 'string') | ||
throw new Error("convertDate(input, format), format=dateStr but typeof input !== 'string'"); | ||
throw new Error("transformField(input, format), format=dateStr but typeof input !== 'string'"); | ||
if (!input.match(/^\d{4,4}-\d{2,2}-\d{2,2}/)) | ||
throw new Error("convertDate(input, format), format=dateStr but typeof input doesn't match 'YYYY-MM-DD'"); | ||
throw new Error("transformField(input, format), format=dateStr but typeof input doesn't match 'YYYY-MM-DD'"); | ||
return new Date(input); | ||
} | ||
}, | ||
|
||
if (format === 'ISODate') { | ||
ISODate(input: string) { | ||
// 2021-02-05T00:00:00.000Z | ||
if (typeof input !== 'string') | ||
throw new Error("convertDate(input, format), format=ISODate but typeof input !== 'string'"); | ||
throw new Error("transformField(input, format), format=ISODate but typeof input !== 'string'"); | ||
if (!input.match(/^\d{4,4}-\d{2,2}-\d{2,2}T\d{2,2}:\d{2,2}:\d{2,2}\.\d{3,3}Z/)) | ||
throw new Error("convertDate(input, format), format=ISODate but got input " + input); | ||
throw new Error("transformField(input, format), format=ISODate but got input " + input); | ||
return new Date(input); | ||
} | ||
}, | ||
|
||
rawNumberObj(input: { raw: number; fmt?: string }) { | ||
if (typeof input !== 'object') | ||
throw new Error("transformField(input, format), format=rawNumberObj but typeof input !== 'object'"); | ||
if (typeof input.raw !== 'number') | ||
throw new Error("transformField(input, format), format=rawNumberObj but typeof input.raw !== 'number'"); | ||
return input.raw; | ||
}, | ||
|
||
} | ||
|
||
export default function transformField(input:any, format:"rawNumberObj"): number; | ||
export default function transformField(input:any, format:"epoch"|"dateObj"|"dateStr"|"ISODate"): Date; | ||
export default function transformField(input:any, format:"epoch|ISODate"): Date; | ||
export default function transformField(input:any, format:string): Date; | ||
export default function transformField(input:any, format:string): Date|number { | ||
if (formats[format]) | ||
return formats[format](input); | ||
|
||
if (format.match(/\|/)) { | ||
const formats = format.split('|'); | ||
let date; | ||
let value; | ||
let failed = new Array(formats.length); | ||
for (let format of formats) { | ||
try { | ||
date = transformDate(input, format) | ||
value = transformField(input, format) | ||
break; | ||
} catch (error) { | ||
failed.push({ format, error }) | ||
} | ||
} | ||
if (!date) { | ||
if (!value) { | ||
const data = { input, format, failed }; | ||
throw new Error("convertDate(input, format) failed: " + JSON.stringify(data, null, 2)); | ||
throw new Error("transformField(input, format) failed: " + JSON.stringify(data, null, 2)); | ||
} | ||
return date; | ||
return value; | ||
} | ||
|
||
throw new Error("convertDate(input, format) but unknown format: " + format); | ||
throw new Error("transformField(input, format) but unknown format: " + format); | ||
} |