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

pnmg

v1.0.1

Published

Powerful and lightweight deep pattern matching for Javascript

Downloads

6

Readme

Powerful and lightweight deep pattern matching for Javascript

pnmg

Code Climate bitHound Overall Score

Pattern matching consists of specifying patterns to which some data should conform and then checking to see if it does. Way to select behaviors based on the structure of a value in a similar way to destructuring.

The pnmg provides deep matching functionality with usefull extra types. Leads to write functional, immutable and expressive code.

switch with ~~blackja...~~ declarative style and deep matching.

Installation

NPM

npm install --save pnmg

Patterns

Object Patterns

Object patterns match objects with certain properties. Additional properties may be present on the matched object. Examples:

import {match} from 'pnmg';

const arg = {/* data */};

match(arg)
  .when({}, () => {/* Match empty object */})
  .when({users: Array}, ({users}) => users.map(user => user.id))
  .when({x: 42, y: String}, obj => {/* Match object where x is 42 and y is String */})
  .when(Object, () => {/* Match any object */})
  .default(notObj => {/* default handler if none of patterns confirmed */});

Array Patterns

Examples:

import {match, Guard} from 'pnmg';

match(arg)
  .when([], () => {/* match an empty array */})
  .when([true, false], () => 'First el is true, second is false')
  .when([{WUF: 'WUFF'}], () => 'First element is Object with WUF attr with WUFF value')
  .when(Guard(arr => arr.length > 4), () => 'Array length greater than 4')
  .when(Array, ([head, ...tail]) => `Head is ${head} and tails is ${tail}`)
  .default(notArray => `${notArray} not Array`);

Literal Patterns

Literal patterns are string, number, boolean, null, and undefined literals and matches exactly that value. Examples:

match(arg)
  .when('WUF', str => {/* match the String value "WUF" */})
  .when(42, () => {/* match the Number value 42 */})
  .when(true, () => {/* match true */})
  .when(Boolean, () => {/* match any Boolean */})
  .default(() => `${arg} not matched`);

Nested Patterns

Patterns can nest. Example:

const isCool = data => match(data)
  .when({info: {cool: true}}, handleCoolData)
  .default(() => 'Not cool');

Types

Defined

You can use Defined if you need to match data that could have any value except undefined.

Guard

Use Guard when value need to match certain condition.

Example:

import {match, Defined, Guard} from 'pnmg';

match(data)
  .when({pos: Guard(x => x > 0)}, arr => {/* Object with 'pos' attr and value > 0 */})
  .when([Defined, 42, Defined], arr => {/* array with at least 3 elements and the second is 42 */})
  .default(() => {/* not match */});
}