Skip to content

Commit

Permalink
chore(transformField): refactor format funcs, overload typescript defs
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Feb 1, 2021
1 parent 0de4847 commit 565a5b1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/lib/mutateJson.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mutateJson from './mutateJson';
import transformDate from './transformDate';
import transformField from './transformField';
import { quoteSummary_dateFields } from '../modules/quoteSummary';

describe('mutateJson', () => {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/lib/transformField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
65 changes: 42 additions & 23 deletions src/lib/transformField.ts
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);
}

0 comments on commit 565a5b1

Please sign in to comment.