Created
January 10, 2022 19:51
-
-
Save bingomanatee/96b3e608d4754d65e9ef410cf7b1bce7 to your computer and use it in GitHub Desktop.
CaseRow.js
This file contains hidden or 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
import DateRep from "./DateRep"; | |
import { Prisma } from '@prisma/client' | |
function asInt(value) { | |
const i = parseInt(value); | |
if (isNaN(i)) { | |
return 0; | |
} else { | |
return i; | |
} | |
} | |
function asFloat(value) { | |
try { | |
const i = parseFloat(value); | |
if (isNaN(i)) return null; | |
return i | |
} catch (err) { | |
return null; | |
} | |
} | |
function asDate(date) { | |
const value = new DateRep(date); | |
return value.invalid ? null : value.asDate(); | |
} | |
export default class CaseRow { | |
constructor(data) { | |
Object.assign(this, data); | |
} | |
valueOf() { | |
return ["Date_Published","UID","iso2","iso3","code3","FIPS","Province_State","Country_Region","Last_Update","Latitude","Longitude","Confirmed","Deaths","Recovered","Active","Incident_Rate","People_Tested","People_Hospitalized","Mortality_Rate","Testing_Rate","Hospitalization_Rate","Population" | |
].reduce((out, name) => { | |
out[name] = this[name]; | |
return out; | |
}, {}); | |
} | |
} | |
function defProps(myClass, names, inputFilter, start) { | |
names.forEach((name) => { | |
const localName = "_" + name; | |
const propDef = { | |
configurable: false, | |
enumerable: true, | |
get() { | |
if (!(localName in this)) { | |
this[localName] = start; | |
} | |
return this[localName]; | |
}, | |
set(value) { | |
this[localName] = inputFilter(value); | |
}, | |
}; | |
Object.defineProperty(myClass.prototype, name, propDef); | |
Object.defineProperty(myClass.prototype, localName, { | |
enumerable: false, | |
writable: true, | |
}) | |
}) | |
} | |
defProps(CaseRow, [ | |
'Mortality_Rate', 'Active', 'Population', 'People_Tested', 'Confirmed', 'Deaths', 'UID', 'Recovered', 'People_Hospitalized' | |
], asInt, 0); | |
defProps(CaseRow, | |
['Date_Published', 'Last_Update'], | |
asDate, null | |
) | |
defProps(CaseRow, | |
['Longitude', 'Latitude','Incident_Rate', 'Testing_Rate', 'Hospitalization_Rate'], | |
asFloat, null | |
) | |
/* | |
toInt(row, 'Mortality_Rate', 'Incident_Rate', 'Active', 'Population', 'People_Tested', 'Confirmed', 'Deaths', 'UID', 'Recovered', 'People_Hospitalized'); | |
toDate(row, 'Date_Published', 'Last_Update'); | |
toFloat(row, 'Longitude', 'Latitude', 'Testing_Rate', 'Hospitalization_Rate'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment