GithubHelp home page GithubHelp logo

schema-compare's Introduction

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)

A quirk of the way this works is that you can perform a strict equality check on objects / arrays by treating them as simple values in the schema itself. For example, schemaCompare({foo: null}, {foo: []}, {foo: []}) === false but var arr = []; schemaCompare({foo: null}, {foo: arr}, {foo: arr}) === true.

Conversely, a schema of {} will return true for any arguments.

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.

schema-compare's People

Watchers

James Cloos avatar Peter Liapin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.