Created
November 20, 2020 16:50
-
-
Save PierBover/3c6817788fa3783d73adc6d391d9db61 to your computer and use it in GitHub Desktop.
Fauna object parser
This file contains 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
export function faunaDocToObject (item) { | |
if (Array.isArray(item)) { | |
return item.map(itemArray => faunaDocToObject(itemArray)); | |
} | |
if (item && typeof item === 'object') { | |
const object = {}; | |
for (const key in item) { | |
const value = item[key]; | |
if (key === '@ref') { | |
object.id = value.id | |
object.collection = value.collection['@ref'].id | |
return object; | |
} | |
if (key === '@ts') { | |
return new Date(value); | |
} | |
if (typeof value === 'object') { | |
object[key] = faunaDocToObject(value); | |
continue; | |
} | |
if (Array.isArray(value)) { | |
doc[key] = faunaDocToObject(value) | |
continue; | |
} | |
object[key] = value; | |
} | |
return object; | |
} | |
return item; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment