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

patterns

v1.0.3

Published

Match a string against a list of patterns

Downloads

1,100

Readme

patterns

Match a string against a list of patterns.

build status js-standard-style

The name of this module was previosuly match-patterns, but Pavel Lang have been generous to give me the patterns name on NPM. If you are looking for the previous module it have been renamed design-patterns.

Installation

npm install patterns

Example usage

Add patterns using the .add() function:

var patterns = require('patterns')()

patterns.add('mathias', 'foo') // a pattern can be a string
patterns.add(/(tom|thomas)/, 'bar') // or a RegExp object
patterns.add('anders', 'baz')

var match = patterns.match('thomas')

if (match) console.log(match.value) // outputs 'bar'

The module can also be seeded with an array of patterns if you don't care about a 2nd argument that you can supply to the .add() function:

var patterns = require('patterns')([
  /foo/,
  /bar/,
  /baz/
])

if (patterns.match('foobar')) {
  console.log('success!')
}

API

patterns.add(pattern[, value])

Arguments:

  • pattern - The pattern as either a string or a RegExp object
  • value (optional) - Returned as part of the match object when calling the .match() function. Can be of any type

If the pattern is a string it will be matched using the murl module. If the pattern is a RegExp object it will be matched as is.

patterns.match(target)

Arguments:

  • target - The string that should be matched against each pattern

Runs through each pattern in order and returns a match object for the first match. If no pattern matches null is returned.

patterns.matchAll(target)

Arguments

  • target - The string that should be matched against each pattern

Runs through each pattern in order and returns an array of match objects. If no pattern matches, an empty array is returned.

Example match object

{
  pattern: '/Users/{name}', // the matched pattern (1st argument to `.add()` function)
  target: '/Users/watson',  // the target string
  params: {                 // the named parameters from the pattern
    name: 'watson'
  },
  value: value,             // the value (2nd argument to `.add()` function)
  next: function () {...}   // function to skip this match and continue
}

Note that if the pattern is a RegExp object, params will be the result of the native <str>.match(<ptn>) function (an array).

The next-function

The match.next function can be used to skip the found match and continue matching the string against the patterns:

patterns.add(/foo/, 1)
patterns.add(/baz/, 2)
patterns.add(/bar/, 3)

var values = []

var match = patterns.match('foobar')
while (match) {
  values.push(match.value)
  match = match.next()
}

console.log(values) // [1, 3]

Example: A complete HTTP router

In this example the patterns module is used as a simple but powerful HTTP route matcher:

var http = require('http')
var url = require('url')
var patterns = require('patterns')()

patterns.add('GET /foo', fn1)
patterns.add('GET /foo/{id}', fn2)
patterns.add('POST /foo/{id}', fn3)

http.createServer(function (req, res) {
  var path = url.parse(req.url).pathname
  var match = patterns.match(req.method + ' ' + path)

  if (!match) {
    res.writeHead(404)
    res.end()
    return
  }

  var fn = match.value // expects the value to be a function
  req.params = match.params

  fn(req, res)
}).listen(8080)

License

MIT