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

js-matcher

v0.0.3

Published

A basic pattern matcher for JavaScript Objects.

Downloads

6

Readme

JS Pattern Matcher

JS Pattern Matcher is a naive pattern matcher. No special tricks or optimizations.

Install

Install via NPM by running

npm install js-matcher

API Docs

match(pattern, obj)

  • pattern - the pattern to match against.
  • obj - object to match against the pattern.

Match recursively walks through the pattern space and the object to be matched against checking that each part match. The pattern space can specify variables that can be matched against multiple times and will only match if the same variable matches itself on every occurance. Variables are specified by a string with '$' as the first character.

You can use variables as keys to objects. Since there could be more than one result with this case the variable needs to be determined. ie. it must appear somewhere else in the pattern that isn't the key to an object. ( { '$a': 1, x: '$a' } )

Examples

Ones that will match and return true:

var matcher = require('js-matcher')
  , match = matcher.match
  , rest = matcher.rest
match({ a: 1 }, { a: 1})
match([1,2,3,4], [1,2,3,4])
match([1, rest], [1, 2, 3, 4])
match({ a: 1 }, { a: 1, b: 3, c: 3})
match({ a: '$v', d: [1,2,'$v']}, { a: 1, b: 3, c: 3, d: [1,2,1]})
match({ '$v': 3, d: [1,2,'$v']}, { a: 1, b: 3, c: 3, d: [1,2,'b']})
match(['$v',2], [1,2])

Ones that will return false:

match({ a: 2 }, { a: 1})
match([1,2,7,4], [1,2,3,4])
match([4, rest], [1, 2, 3, 4])

mapMatch(patterns, obj, defaultVal)

  • patterns - an array of patterns - val pairs. The first part of the pair is a pattern to run in match. The second is the value to return if that pattern matched.
  • obj - the object to run against the list of patterns.
  • defaultVal - the value to return if none of the patterns matched.

mapMatch runs through the list and returns the first value of a [pattern, value] where the pattern matches.

var matcher = require('js-matcher')
  , mapMatch = matcher.mapMatch
  , patterns =
    [ [ { a: 123 }, 'hi there' ]
    , [ { b: 454 }, 'b is 454' ]
    , [ { a: [1, 2] }, "123's and abc's"]
    ]

mapMatch(patterns, { a: 123, b: 454 }, "I wasn't matched") // 'hi there'
mapMatch(patterns, { d: 83, b: 454 }, "I wasn't matched") // 'b is 454'
mapMatch(patterns, { a: [1, 2], b: [454] }, "I wasn't matched") // "123's and abc's"
mapMatch(patterns, { a: [3, 2, 4], b: [454] }, "I wasn't matched") // "I wasn't matched"

rest

Place holder for the end of an array. When added to the end of an array in a pattern it signifies that the array to be matched can have more elements then the pattern array.