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

tmatch

v5.0.0

Published

This module exists to facilitate the `t.match()` method in [`tap`](http://npm.im/tap).

Downloads

396,813

Readme

tmatch

This module exists to facilitate the t.match() method in tap.

It checks whether a value matches a given "pattern". A pattern is an object with a set of fields that must be in the test object, or a regular expression that a test string must match, or any combination thereof.

The algorithm is borrowed heavily from only-shallow, with some notable differences with respect to the handling of missing properties and the way that regular expressions are compared to strings.

usage

var matches = require('tmatch')

if (!matches(testObject, pattern)) console.log("yay! diversity!");

// somewhat more realistic example..
http.get(someUrl).on('response', function (res) {
  var expect = {
    statusCode: 200,
    headers: {
      server: /express/
    }
  }

  if (!tmatch(res, expect)) {
    throw new Error('Expect 200 status code from express server')
  }
})

details

Copied from the source, here are the details of tmatch's algorithm:

  1. If the object loosely equals the pattern, and either they're both objects or neither objects, then return true. Note that this covers object identity, some type coercion, and matching null against undefined, and avoids some stuff like 1 == [1].
  2. If the object is a RegExp and the pattern is also a RegExp, return true if their source, global, multiline, lastIndex, and ignoreCase fields all match.
  3. If the pattern is a RegExp, then return true if pattern.test(object), casting the object to a string if it is not already a string.
  4. If the pattern is a Set, then return true if all the keys in pattern appear in object.
  5. If the pattern is a Map, then return true if all the keys in pattern are in object, and the values match as well.
  6. If the object is a string and the pattern is a non-empty string, then return true if the string occurs within the object.
  7. If the object and the pattern are both Date objects, then return true if they represent the same date.
  8. If the object is a Date object, and the pattern is a string, then return true if the pattern is parseable as a date that is the same date as the object.
  9. If the object is an arguments object, or the pattern is an arguments object, then cast them to arrays and compare their contents.
  10. If the pattern is the Buffer constructor, then return true if the object is a Buffer.
  11. If the pattern is the Function constructor, then return true if the object is a function.
  12. If the pattern is the String constructor, then return true if the pattern is a string.
  13. If the pattern is the Boolean constructor, then return true if the pattern is a boolean.
  14. If the pattern is the Array constructor, then return true if the pattern is an array.
  15. If the pattern is any function, and then object is an object, then return true if the object is an instanceof the pattern.
  16. At this point, if the object or the pattern are not objects, then return false (because they would have matched earlier).
  17. If the object is a buffer, and the pattern is also a buffer, then return true if they contain the same bytes.
  18. At this point, both object and pattern are object type values, so compare their keys:
    1. Get list of all iterable keys in pattern and object. If both are zero (two empty objects), return true.
    2. Check to see if this pattern and this object have been tested already (to handle cycles). If so, return true, since the check higher up in the stack will catch any mismatch.
    3. For each key in the pattern, match it against the corresponding key in object. Missing keys in object will be resolved to undefined, so it's possible to use {foo:null} as a pattern to ensure that the object doesn't have a foo property.

license

ISC. Go nuts.