Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Created April 28, 2018 02:40
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 jamesplease/bed66031c0e3a61c9a2babb572f87683 to your computer and use it in GitHub Desktop.
Save jamesplease/bed66031c0e3a61c9a2babb572f87683 to your computer and use it in GitHub Desktop.
// Given a flat resource and its schema, returns a full resource.
export default function fullFromFlat({ resource, schema }) {
const attributes = Object.values(schema.attributes);
const metas = Object.values(schema.meta);
const newAttributes = {};
attributes.map(attribute => {
if (resource[attribute]) {
newAttributes[attribute] = resource[attribute];
}
});
const newMeta = {};
metas.map(meta => {
if (resource[meta]) {
newMeta[meta] = resource[meta];
}
});
return {
[schema.idAttribute]: resource[schema.idAttribute],
resourceType: schema.resourceType,
attributes: newAttributes,
meta: newMeta,
relationships: {},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment