-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptor.js
43 lines (34 loc) · 965 Bytes
/
adaptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict'
const isArrayValue = (obj) => obj.hasOwnProperty('arrayValue')
const isMapValue = (obj) => obj.hasOwnProperty('mapValue')
/**
* Converts Firestore doc into JS values without value types.
*/
exports.getSnapshotData = (input) => {
return Array.isArray(input)
? input.map((obj) => extractObject(obj))
: extractObject(input)
}
const extractObject = (value) => {
const output = {}
if (value) {
Object.keys(value).forEach((k) => {
const valueField = value[k]
output[k] = extractValueField(valueField)
})
}
return output
}
const extractValueField = (valueField) => {
if (isArrayValue(valueField)) {
return (valueField.arrayValue.values || []).map((v) => extractValueField(v))
}
if (isMapValue(valueField)) {
return extractObject(valueField.mapValue.fields)
}
if (!(valueField instanceof Object)) {
return valueField
}
const key = Object.keys(valueField)[0]
return valueField[key]
}