1.0.7 • Published 6 years ago

minimal-normalize v1.0.7

Weekly downloads
272
License
MIT
Repository
-
Last release
6 years ago

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: '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:

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

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago