From 565a5b1d4b1c676829d31a31dd745f9c2a5c84db Mon Sep 17 00:00:00 2001 From: Gadi Cohen Date: Mon, 1 Feb 2021 09:39:23 +0200 Subject: [PATCH] chore(transformField): refactor format funcs, overload typescript defs --- src/lib/mutateJson.spec.ts | 6 ++-- src/lib/transformField.spec.ts | 22 ++++++++++++ src/lib/transformField.ts | 65 ++++++++++++++++++++++------------ 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/lib/mutateJson.spec.ts b/src/lib/mutateJson.spec.ts index 3885f231..005869f1 100644 --- a/src/lib/mutateJson.spec.ts +++ b/src/lib/mutateJson.spec.ts @@ -1,5 +1,5 @@ import mutateJson from './mutateJson'; -import transformDate from './transformDate'; +import transformField from './transformField'; import { quoteSummary_dateFields } from '../modules/quoteSummary'; describe('mutateJson', () => { @@ -9,7 +9,7 @@ describe('mutateJson', () => { earnings: { earningsDate: [ 1619568000, 1620000000 ] } }; - mutateJson(obj, quoteSummary_dateFields, transformDate); + mutateJson(obj, quoteSummary_dateFields, transformField); expect(obj.earnings.earningsDate[0]).toBeInstanceOf(Date); expect(obj.earnings.earningsDate[1]).toBeInstanceOf(Date); @@ -30,7 +30,7 @@ describe('mutateJson', () => { } }; - mutateJson(obj, quoteSummary_dateFields, transformDate); + mutateJson(obj, quoteSummary_dateFields, transformField); const array = obj.cashflowStatementHistoryQuarterly.cashflowStatements; expect(array[0].endDate).toBeInstanceOf(Date); diff --git a/src/lib/transformField.spec.ts b/src/lib/transformField.spec.ts index 279d1c63..47bee2f6 100644 --- a/src/lib/transformField.spec.ts +++ b/src/lib/transformField.spec.ts @@ -32,6 +32,28 @@ describe('transformField', () => { }); + describe('ISODate', () => { + + it('converts valid input', () => { + const input = '2021-02-01T07:38:07.751Z'; + const date = transformField(input, 'dateStr'); + expect(date).toBeInstanceOf(Date); + expect(date.getTime()).toBe(new Date(input).getTime()); + }); + + }); + + describe('rawNumberObj', () => { + + it('converts valid input', () => { + const input = { raw: 12.1, fmt: "12.1" }; + const raw = transformField(input, 'rawNumberObj'); + expect(raw).toBe(input.raw); + }); + + }); + + describe('try multi', () => { it('works', () => { diff --git a/src/lib/transformField.ts b/src/lib/transformField.ts index e9590743..c6b367f4 100644 --- a/src/lib/transformField.ts +++ b/src/lib/transformField.ts @@ -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); }