1.0.8 • Published 4 years ago

normalize-api v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

normalize-api

This function pulls out models from nested object. And removes model nesing alltogether. Makes data easy to store in redux store

Single object

import normalize from 'normalize-api'


const post = {
  id: 1,
  title: 'My first post',
  user: { id: 4, name: 'Post Writer' },
  comments: [
    {
      id: 1,
      message: 'First comment',
      post_id: 1,
      user: { id: 1, name: 'User 1' }
    },
    {
      id: 2,
      message: 'Second comment',
      post_id: 1,
      user: { id: 2, name: 'User 2' }
    },
    {
      id: 3,
      message: 'Third comment',
      post_id: 1,
      user: { id: 2, name: 'User 2' }
    }
  ]
}

const models = normalize(post, 'post')

/* response of normalize
models = {
  post: [
    { id: 1, title: 'My first post', user_id: 4 },
  ],
  comments: [
    { id: 1, post_id: 1, message: 'First comment', user_id: 1 },
    { id: 2, post_id: 1, message: 'Second comment', user_id: 2 },
    { id: 3, post_id: 1, message: 'Third comment', user_id: 2 }
  ],
  user: [
    { id: 1, name: 'User 1', },
    { id: 2, name: 'User 2' },
    { id: 4, name: 'Post Writer' },
  ]
}
*/

Array of objects

import normalize from 'normalize-api'

const posts = [
  {
    id: 1,
    title: 'My first post',
    user: { id: 4, name: 'Post Writer' },
    comments: [
      {
        id: 1,
        message: 'First comment',
        post_id: 1,
        user: { id: 1, name: 'User 1' }
      },
      {
        id: 2,
        message: 'Second comment',
        post_id: 1,
        user: { id: 2, name: 'User 2' }
      },
      {
        id: 3,
        message: 'Third comment',
        post_id: 1,
        user: { id: 2, name: 'User 2' }
      }
    ]
  },
  {
    id: 2,
    title: 'My second post',
    comments: [
      {
        id: 4,
        message: 'Fourth comment',
        post_id: 2,
        user: { id: 1, name: 'User 1' }
      },
      {
        id: 5,
        message: 'Fifth comment',
        post_id: 2,
        user: { id: 2, name: 'User 2' }
      },
      {
        id: 6,
        message: 'Sixth comment',
        post_id: 2,
        user: { id: 2, name: 'User 2' }
      }
    ]
  }
]

const models = normalize(post,'post')

/* response of normalize
models = {
  posts: [
    { id: 1, title: 'My first post', user_id: 4 },
    { id: 2, title: 'My second post', user_id: 4 }
  ],
  comments: [
    { id: 1, post_id: 1, message: 'First comment', user_id: 1 },
    { id: 2, post_id: 1, message: 'Second comment', user_id: 2 },
    { id: 3, post_id: 1, message: 'Third comment', user_id: 2 },
    { id: 1, post_id: 2, message: 'Fourth comment', user_id: 1 },
    { id: 2, post_id: 2, message: 'Fifth comment', user_id: 2 },
    { id: 3, post_id: 2, message: 'Sixth comment', user_id: 2 }
  ],
  user: [
    { id: 1, name: 'User 1', },
    { id: 2, name: 'User 2' },
    { id: 4, name: 'Post Writer' },
  ]
}
 */
1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

2.0.0

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago