Skip to content

Instantly share code, notes, and snippets.

@PierBover
Created November 20, 2020 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierBover/3c6817788fa3783d73adc6d391d9db61 to your computer and use it in GitHub Desktop.
Save PierBover/3c6817788fa3783d73adc6d391d9db61 to your computer and use it in GitHub Desktop.
Fauna object parser
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