minimal-normalize
v1.0.7
Published
normalizing your data before sending to your store
Downloads
196
Readme
Minimal normalize
normalizing your data before sending to your store
How to use
First install
yarn add minimal-normalize
import normalize
function and uses to normalizing your data before sending to your store, example:
import { normalize } from 'minimal-normalize';
const results = wait fakeRequest('fakeUrlAPI');
dispatch({
type: 'ACTION_NAME',
results: normalize(results.data, 'id')
});
So if you receiving a array of users like:
[
{
id: '123123',
name: 'user 1',
email: '[email protected]'
},
{
id: '234234',
name: 'user 2',
email: '[email protected]'
}
]
And normalized by id, the results will be like:
{
123123: {
id: '123123',
name: 'user 1',
email: '[email protected]'
},
234234: {
id: '234234',
name: 'user 2',
email: '[email protected]'
}
}
With this doing actions to manipulating a data is easier and sometimes faster, your code can be easier to understand and you can write less code :)
OBS: instead of normalizing your data by id, you can uses normalize(array, 'desired key here') and normalizing your data by any key of the object
Merging an existing object
Sometimes you need to concatenate the new results with an existing object, for that just pass the old object as a parameter like:
normalize(newArray, 'id', prevObj);
unnormalize helper
unnormalize is a simple helper to return a array passing a normalized object.
import { unnormalize } from 'minimal-normalize';
const normalizedObject = {
123131321313: {
id: '123131321313',
name: 'name1'
},
728292: {
id: '728292',
name: 'name2'
}
};
console.log(unnormalize(normalizedObject));
//the console is going to be:
// const array = [
// {
// id: '123131321313',
// name: 'name1'
// },
// {
// id: '728292',
// name: 'name2'
// },
// ];