Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iagodahlem/de2226bb0b6231da26caab9f7d98116f to your computer and use it in GitHub Desktop.
Save iagodahlem/de2226bb0b6231da26caab9f7d98116f to your computer and use it in GitHub Desktop.
Scalable Frontend - The State Layer - Normalization Functions
const replaceRelationById = (entities, relation, idKey = 'id') => entities.map(item => ({
...item,
[relation]: item[relation][idKey],
}))
const extractRelation = (entities, relation) => entities.map(entity => entity[relation])
const byId = (entities, idKey = 'id') => entities
.reduce((obj, entity) => ({
...obj,
[entity[idKey]]: entity,
}), {})
const allIds = (entities, idKey = 'id') => [...new Set(entities.map(entity => entity[idKey]))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment