https://github.com/dielduarte/minimal-normalize
normalizing your data before sending to your store
https://github.com/dielduarte/minimal-normalize
Last synced: 4 months ago
JSON representation
normalizing your data before sending to your store
- Host: GitHub
- URL: https://github.com/dielduarte/minimal-normalize
- Owner: dielduarte
- Created: 2018-03-10T16:54:20.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2022-12-07T10:39:35.000Z (about 3 years ago)
- Last Synced: 2025-08-10T08:36:54.603Z (6 months ago)
- Language: JavaScript
- Size: 702 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Minimal normalize
normalizing your data before sending to your store
## How to use
First install
```js
yarn add minimal-normalize
```
import `normalize` function and uses to normalizing your data before sending to your store, example:
```js
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: 'user1@example.com'
},
{
id: '234234',
name: 'user 2',
email: 'user2@example.com'
}
]
```
And normalized by id, the results will be like:
```
{
123123: {
id: '123123',
name: 'user 1',
email: 'user1@example.com'
},
234234: {
id: '234234',
name: 'user 2',
email: 'user2@example.com'
}
}
```
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:
```js
normalize(newArray, 'id', prevObj);
```
## unnormalize helper
unnormalize is a simple helper to return a array passing a normalized object.
```js
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'
// },
// ];
```