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

congruence

v1.6.11

Published

Validate the structure of Javascript objects using semantic templates. Written as an underscore/lodash mixin.

Downloads

53

Readme

congruence

NPM version License Build status Dependency Status

Validate Javascript objects using semantic templates. Written as an underscore/lodash mixin.

Install

$ npm install congruence --save

Introduction

_.mixin(require('congruence'));
var template = { module: _.isString,   version: semver.valid };
var object =   { module: 'abc', version: 'v1.0.0'     };
assert.isTrue(_.congruent(template, object));

Above, the object is congruent to the template because object.module is a string, and semver.valid[1] returns true for object.version.

It's like regular expressions for Javascript objects. Easily test the structure of Javascript objects using expressive templates. Designed as an lodash mixin.

Usage

Use this module to check the congruence of Javascript structures and validity of values using semantic templates. Suppose an object:

var obj = {
  megahertz: 266,
  message: 'hello world'
};

We use the built-in lodash matching functions to build a template (an isometry) that we can validate against. Here is a template that matches obj above:

var matchingTemplate = {
  megahertz: _.isNumber
  message: _.isString
};

But this will not match:

var failedTemplate = {
  megahertz: 500,
  foo: _.isFunction
};

Both properties will fail validation. If a non-function is given in the template value, it will be used as a strict equality check. megahertz is not equal to 500 so that fails. And the template requires foo to be a function, but obj.foo is not even defined.

Any lodash isXYZ function can be used as a predicate; you can also define your own, e.g.

var template = {
  a: function (list) {
    return _.all(list, function (value) {
      return (value % 2) > 0;
    });
  }
},
object = {
  a: [ 1, 3, 5 ]
};
assert.isTrue(_.congruent(template, object));

Examples

A. Simple Template Congruence

var object = {
  a: 3.1415926535,
  foo: {
    bar: {
      b: 'hello world',
      c: [ 1, 1, 2, 3, 5, 8 ],
      d: new Date()
    }
  }
};
var matchingTemplate = {
  a: 3.1415926535,
  foo: _.congruent({
    bar: _.congruent({
      b: _.isString,
      c: _.isArray,
      d: _.compose(_.not, _.isFunction)
    })
  })
};

assert.isTrue(_.congruent(matchingTemplate, object));

B. Simple Template Similarity

var template = {
  id: 57,
  name: 'Travis'
};
var object = {
  id: 57,
  name: 'Travis',
  color: 'blue',
  foo: 1
};

// the extra object properties are ignored
assert.isTrue(_.similar(template, object));

3. Full Lodash API

_.congruent(template, object)

Return true if the object matches all of the conditions in the specified template, and the keysets are identical.

| @param | description |:--|:--| | template | the congruence template used to validate the object | object | the object to validate | @return | description | Boolean | true if the object is congruent to the template, false otherwise

var template = {
  <key>: <predicate>
};
var object = {
  <key>: <value>
};

_.congruent(template, object);

_.similar(template, object)

Return true if the object matches all the conditions specified by the template.

| @param | description |:--|:--| | template | the congruence template used to validate the object | object | the object to validate | @return | description | Boolean | true if the object is congruent to the template, false otherwise

var template = {
  <key>: <predicate>
};
var object = {
  <key>: <value>
};

License

MIT