Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Last active January 23, 2020 16:20
Show Gist options
  • Save mattlockyer/3bda07228e59018e09ba9f163a4ec5ba to your computer and use it in GitHub Desktop.
Save mattlockyer/3bda07228e59018e09ba9f163a4ec5ba to your computer and use it in GitHub Desktop.
Turn JSON / POJO into Firestore Fields for REST API
//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