New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

json-api-denormalizr

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-api-denormalizr

[![CircleCI](https://circleci.com/gh/willsoto/json-api-denormalizr.svg?style=svg&circle-token=0b410de6b9ef748768a5d2cd9fc7f3eec658a27c)](https://circleci.com/gh/willsoto/json-api-denormalizr)

0.1.3
Version published
Weekly downloads
435
-37.68%
Maintainers
1
Weekly downloads
 
Created

json-api-denormalizr

CircleCI

Getting Started

npm install json-api-denormalizr --save

Documentation

Quickstart

Given the following store / state in your Redux application:

{
  "entities": {
    "postBlock": {
      "2620": {
        "id": "2620",
        "attributes": {
          "text": "I am great!",
          "id": "2620"
        },
        "relationships": {
          "question": {
            "data": {
              "id": "295",
              "type": "question"
            }
          }
        }
      }
    },
    "question": {
      "295": {
        "id": "295",
        "attributes": {
          "text": "How are you?",
          "id": "295"
        }
      }
    }
  }
}
import { denormalize } from 'json-api-denormalizr';

denormalize(state, {
  id: '2620',
  attributes: {
    text: 'I am great!',
    id: '2620'
  },
  relationships: {
    question: {
      data: {
        id: '295',
        type: 'question'
      }
    }
  }
});

// equivalent to:
denormalize(state, state.entities.postBlock['2620'])

Will result in:

{
  "id": "2620",
  "question": {
    "id": "295",
    "text": "How are you?"
  },
  "text": "I am great!"
}

API

denormalize(state, entity, options = {})

  • state: your application state
  • entity: the top level entity
  • options:
    • pathToEntities:
      • Where all your entities are located in the store. Assumes all your entities are at the same level of the store.

Usage with Reselect

Quick example using selectors:

import { createSelector } from 'reselect';
import { denormalize } from 'json-api-denormalizr';

const entitiesSelector = state => state.entities;
const booksSelector = state => state.entities.books;

export const bookSelector = bookId => {
  return createSelector([entitiesSelector, booksSelector], (entities, books) => {
    const book = books[bookId];

    return denormalize(entities, book, {
      pathToEntities: 'entities'
    });
  });
};

Contributing

npm test

FAQs

Package last updated on 03 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts