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

match-json

v1.3.7

Published

A light assertion library built with JSON APIs in mind.

Downloads

176,769

Readme

match-json Build Status

match-json is a light assertion library built with JSON APIs in mind.

JSON API's can only carry JSON types: strings, numbers, booleans, arrays and objects. This library uses that in favor to use functions and regexp to assert JSON API's. You should bring your favorite test library.

Install

npm install match-json

Usage

Match JSON Primitives.

// Numbers
match(3.1415, 3.1415); // => true

//Strings
match("Uno Dos Tres", "Uno Dos Tres"); // => true

// Booleans
match(false, false); // => true

// And with undefined and null values
match(undefined, undefined); // => true
match(null, null); // => true

Match Structures (objects and arrays).

match({ name: "Link", color: "green" }, { name: "Link", color: "green" }); // => true
match(["deku", "goron", "zora"], ["deku", "goron", "zora"]); // => true

But the cool part starts here

Matching using Functions

match({ name: "Samus" }, (hero) => hero.name.length >= 5); // => true

Matching using regular expressions

match(
  "[email protected]",
  /[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}/
); // => true

Also, match-json can check JSON types using constructor functions

match(5, Number); // => true

match("Hola, mundo", String); // => true

match(false, Boolean); // => true

And everything together!

match(
  {
    name: { first: "Walter", last: "White" },
    age: 51,
    breakingBad: true,
  },
  {
    name: { first: /[\w]*/, last: "White" },
    age: (age) => age > 18,
    breakingBad: Boolean,
  }
); // => true

Partials

In an object, the default behavior of partial interpret as an error any extra field received. Partial mode ignores potential extra fields received.

Partial mode is enabled by using the partial function exposed from match-json instead of match-json itself. The rest of functionality is not changed.

NOTE: Only objects (and not arrays) are affected by partial mode.

import match, { partial } from 'match-json';

// No partial mode
match({ id : 5, name: 'john' }, { id: Number }) // => false

// Partial mode
partial({ id : 5, name: 'john' }, { id: Number }) // => true

Bake

match-json also provides a bake function that can be used to predefine an expected pattern.

const nameIsLarge = match.bake({ name: (name) => name.length > 10 });
nameIsLarge("Tom"); // => false
nameIsLarge("Tooooooooom"); // => true

Signatures

Match's signature

  • match( a : T, b : T, partialMode : boolean? ) : boolean
  • match( a : T, test : RegExp, partialMode : boolean? ) : boolean
  • match( a : T, test : PredicateFunction, partialMode : boolean? ) : boolean
  • match( a : T, test : JSONTypeConstructorFunction, partialMode : boolean? ) : boolean

Bake's signature

  • bake( a: T, partialMode : boolean? ) : PredicateFunction

  • ( where PredicateFunction = ( w : T ) : boolean )

  • ( where JSONTypeConstructorFunction = Number, String OR Boolean )

Notes

  • Is worth to mention that you only can use JSON-data as the first argument of the function. Not functions or RegExp.
  • I made this for test my API endpoints, thats why it only accepts to test JSON data.

Contribution

Feel free to open an issue and/or make a PR if you found a bug or think in a way this lib or even the README can be improved.

License

MIT