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

schema-compare

v1.0.1

Published

Deep compare two objects based on a schema

Downloads

7

Readme

schema-compare

A small utility to do "partial" deep comparisons of two objects.

Why

React's pure render mixin wasn't cutting it for us, but I didn't want to do a deep inspection of everything on the props every time either. I hand wrote some shouldComponentUpdate functions and wanted to generalize. This gave me something that I could configure at use-time to allow the kind of comparison I had in mind.

How

npm install schema-compare

var schemaCompare = require('schema-compare');
var schema = {
    foo: [],
    bar: [{ baz: null }]
    quux: null
};

// ...
schemaCompare(schema, obj1, obj2);

The way it works is as follows:

  • If given a simple value (not an object or an Array instance), it will perform a strict comparison of the two arguments (schemaCompare(1, 2, 3) === false, schemaCompare(null, 'foo', 'foo') === true)
  • If given an array, it will compare the length and then contents of the arguments (schemaCompare([], [1], [1, 1]) === false, schemaCompare([], [1], [2]) === false, schemaCompare([], [1, 2, 3], [1, 2, 3]) === true)
  • If given an object, it will verify the presence of the specified keys on the arguments and compare their values. Unspecified keys are not compared. (schemaCompare({foo:null}, {foo: 1}, {foo: 2}) === false, schemaCompare({foo:null}, {foo: 1, bar: 2}, {foo: 1, bar: 3}) === true)
  • The first index of an array in the schema and the values of the keys in the schema will recursively call schemaCompare to compare the associated data from the arguments (schemaCompare([ {foo:null} ], [{foo:1}, {foo:2}], [{foo:1}, {foo:2}]) === true, schemaCompare([ {foo:null} ], [{foo:1}, {foo:2}], [{foo:1}, {foo:3}]) === false)

When you need to specify a key in an object to be compared, but the values are simple values, you can specify anything except an array or an object. I make a point of using null in the examples since typeof null === 'object' to illustrate that this case is covered.

Multiple arguments to an array in the schema definition is erroneous; only the first index will be checked. No error checking is done on the structure of the data you pass: if you pass data that doesn't conform to your schema, you will likely get not-very-helpful error messages sourced in this module.

Tests

npm test for unit tests npm run cov for unit tests with coverage report (currently 100%)

The tests are autogenerated from permutations of many different kinds of values and augmented by some targeted error and shortcut cases.