An open API service indexing awesome lists of open source software.

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

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'
// },
// ];

```