(val: any)
| 11 | } |
| 12 | |
| 13 | function encodeHelper(val: any): any { |
| 14 | if (typeof val === "string") { |
| 15 | return { stringValue: val }; |
| 16 | } |
| 17 | if (val === !!val) { |
| 18 | return { booleanValue: val }; |
| 19 | } |
| 20 | if (Number.isInteger(val)) { |
| 21 | return { integerValue: val }; |
| 22 | } |
| 23 | // Integers are handled above, the remaining numbers are treated as doubles |
| 24 | if (typeof val === "number") { |
| 25 | return { doubleValue: val }; |
| 26 | } |
| 27 | if (val instanceof Date && !Number.isNaN(val)) { |
| 28 | return { timestampValue: val.toISOString() }; |
| 29 | } |
| 30 | if (Array.isArray(val)) { |
| 31 | const encodedElements = []; |
| 32 | for (const v of val) { |
| 33 | const enc = encodeHelper(v); |
| 34 | if (enc) { |
| 35 | encodedElements.push(enc); |
| 36 | } |
| 37 | } |
| 38 | return { |
| 39 | arrayValue: { values: encodedElements }, |
| 40 | }; |
| 41 | } |
| 42 | if (val === null) { |
| 43 | return { nullValue: "NULL_VALUE" }; |
| 44 | } |
| 45 | if (val instanceof Buffer || val instanceof Uint8Array) { |
| 46 | return { bytesValue: val }; |
| 47 | } |
| 48 | if (isPlainObject(val)) { |
| 49 | return { |
| 50 | mapValue: { fields: encodeFirestoreValue(val) }, |
| 51 | }; |
| 52 | } |
| 53 | throw new FirebaseError( |
| 54 | `Cannot encode ${val} to a Firestore Value. ` + |
| 55 | "The emulator does not yet support Firestore document reference values or geo points.", |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | export function encodeFirestoreValue(data: any): Record<string, any> { |
| 60 | return Object.entries(data).reduce( |
no test coverage detected
searching dependent graphs…