npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

denormalizer

v1.0.0

Published

State denormalization for normalizr

Downloads

9

Readme

denormalizer: the other half of normalizr

The normalizr library offers excellent features for normalizing deeply-nested data structures into a simple entity store. Returning the normalized data to its denormalized shape is not as easy, however. denormalizer makes this process transparent: pass in the entity schema that you used to normalize, an entity collection, and it will return the denormalized values.

Installation

npm install --save denormalizer

denormalizer has no dependencies and a small codebase.

Usage

Imagine you have the following normalized entities:

const entities = {
  articles: {
    1: {
      id: 1,
        title: 'Some Article',
        author: 1
    },
    2: {
      id: 2,
        title: 'Other Article',
        author: 1
    }
  },
  authors: {
    1: {
      id: 1,
        name: 'Dan'
    }
  }
}

And you want to denormalize it so that authors appear inline with the article they authored:

[{
  id: 1,
  title: 'Some Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}, {
  id: 2,
  title: 'Other Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}]

We set up our schema using the normalizr primitives:

const article = new Schema('articles');
const user = new Schema('users');

article.define({ author: user });

We can then call denormalize on the collection of entities to retrieve the denormalized value:

import { denormalize } from 'denormalizer';

const article = denormalize(article, entities, 1);

article should look something like this:

{
  id: 1,
  title: 'Some Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}

Or, to denormalize an array of articles:

denormalize(arrayOf(article), entities, [1,2]);

Broken references

Sometimes, an entity will reference another entity that does not exist in the store. Perhaps it was removed from the store, perhaps it never existed, or perhaps it's being retrieved from the server and hasn't yet arrived. For these faulty references, denormalizer will return an object that contains just the entity's identifier and a key indicating that it is an unresolved entity.

You can check for this value using the isResolved functon provided by the library:

import { denormalize, isResolved } from 'denormalizer';

const empty = denormalize(article, {}, 1); // { id: 1, __resolved: false }
isResolved(empty) // false

A more complex example:

const entities = {
  articles {
    3: {
      id: 3,
      title: 'A final article',
      author: 6 // this author isn't in the store yet
    }
  }
};

const article = denormalize(article, entities, 3);

article now contains the value:

{
  id: 3,
  title: 'A final article',
  author: {
    id: 6,
    __resolved: false
  }
}

Implementation

To avoid circular reference hell and denormalizing huge entity trees, denormalizer uses ES5 getters to dereference foreign entities. When you first retrieve a foreign entity value, it is denormalized and cached internally so that future references happen quickly. In principle, these objects behave just like regular objects, but in practice sometimes this dereferencing strategy can confuse some JS libraries. For example, nodejs' deepEquals function in the assertions and unit testing module.