Last active
January 23, 2020 16:20
-
-
Save mattlockyer/3bda07228e59018e09ba9f163a4ec5ba to your computer and use it in GitHub Desktop.
Turn JSON / POJO into Firestore Fields for REST API
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
//only handles integerValue, doubleValue, stringValue and booleanValue | |
const getType = (val) => { | |
if (val === null) return 'nullValue' | |
const t = typeof val | |
switch (t) { | |
case 'number': return n % 1 === 0 ? 'integerValue' : 'doubleValue' | |
case 'string': | |
case 'boolean': | |
return t + 'Value' | |
} | |
} | |
//@param obj json/pojo | |
const getFields = (obj) => Object.keys(obj) | |
.map((k) => ({ [k]: {[getType(obj[k])]: obj[k] }})) | |
.reduce((acc, cv) => ({...acc, ...cv}), {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment