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

fnmatch

v1.0.2

Published

Function-based matching

Downloads

868

Readme

fnMatch

This is a very simple implementation of pattern matching using no syntax extensions. That means you can use it without a transpiler; just include it on your project/website.

Quick example:

You can do this in OCaml:

let rec fib n = match n with
  | 0 -> 1
  | 1 -> 1
  | x -> fib (x-2) + fib (x-1)

JavaScript using fnMatch:

let fib = (n) => match(n)(
  (x = 0) => 1,
  (x = 1) => 1,
  (x) => fib(x-2) + fib(x-1)
);

Installation

  • node
npm i fnMatch
  • web
<script src="https://raw.githubusercontent.com/mrkev/fnMatch/master/dist/fnMatch.js"></script>

Usage

Instead of extending the language, it just uses functions. That's it. Just import fnMatch anywhere:

const { match, func } = require('fnMatch');

It supports:

  • Shape matching through object deconstruction:

let group = {
  course: "CS 3410",
  members: [
    {name: "Ajay", age: 22},
    {name: "Seung", age: 23},
    {name: "Adi", age: 22},
  ],
}

match(group)(
  ({ club, members }) => {}, // doesn't match, missing "club"
  ({ course, members: [first, ...rest]}) => {}, // matches!
  ({ course, members }) => {}, // would match if previous didn't
)
  • Value matching through default value syntax:

match("hello")(
  (_ = "hey") => {}, // doesn't match
  (_ = "world") => {}, // doesn't match
  (_ = "hello") => {}, // matches!
)
  • Binding (of course, these are functions after all!)

match({name: "Ajay"})(
  ({name}) => conosle.log(`Hello ${name}!`), // matches, name is "Ajay"
  () => console.log("Hello, someone!"), // would match if previous didn't
)
  • Alternative syntax: pattern definition before evaluation:

func(
  ({name}) => conosle.log(`Hello ${name}!`), // matches, name is "Ajay"
  () => console.log("Hello, someone!"), // would match if previous didn't
)({name: "Ajay"})

Woah woah woah. Why would I want to do a pattern match by defining the patterns first and passing in the value later? Well, because this is an expression, and this way of doing things is useful for defining functions, a la OCaml's function keyword:

OCaml:

let rec fib = function
  | 0 -> 1
  | 1 -> 1
  | x -> fib (x-2) + fib (x-1)

JavaScript:

let fib = func(
  (_ = 0) => 1
  (_ = 1) => 1
  (x) => fib(x-2) + fib(x-1)
);
  • And of course, "match" and "func" are expressions evaluating to the return the value of the matched function. Here's a more complex example:

const process_response = func(
  ({ status: s = 200, data: d = null }) => Error("Data is null"),
  ({ status: s = 200, data: d = []}) => Error("Data is empty"),
  ({ status: s = 200, data }) => data, // return data
  ({ errors: [h, ...t] }) => Error(`Non-empty errors. ${t.length + 1} total.`),
  ({ status }) => Error(`Status ${status}, no errors reported`),
  (_) => Error("Invalid response"),
)

const print_response = response => {
  const result = process_response(response)
  if (result instanceof Error) throw result;
  console.log(`Recieved ${result}!`)
}

Things to note

  • Patterns are tested in order, from top to bottom:

// This prints "default"
match({ name: "Ajay" })(
  (_) => console.log("default"),
  ({name}) => console.log(name),
)

// This prints "Ajay"
match({ name: "Ajay" })(
  ({name}) => console.log(name),
  (_) => console.log("default"),
)
  • () => {} matches undefined, not all (aka, () => {} !== (_) => {}):

// This prints "undefined"
match()(
  () => console.log('undefined'),
  (_) => console.log('anything'),
);

// This prints "anything"
match(3)(
  () => console.log('undefined'),
  (_) => console.log('anything'),
);
  • Array semantics are a little different from how JavaScript normally works:

Array semantics

For the sake of usability, the semantics used for array matching are different from how calling a function with that destructs an array would have you believe. Let's illustrate with 2 examples:

  • Identifiers will always match a value in the array, whereas on function calls they are assigned undefined if there's nothing for them to match

// Javascrpt
(([x, ...y]) => console.log(x, y))([]) // logs "undefined []"

// fnMatch
func(([x, ...y]) => console.log(x, y))([])   // logs nothing, doesn't match
  • Patterns without a "rest clause" (ie, ...t) will only match patterns of that same length (similar to how Ocaml works)

// Javascrpt
(([x, y]) => console.log(x, y))([1, 2, 3]) // logs "1 2"

// fnMatch
func(([x, y]) => console.log(x, y))([1, 2, 3])   // logs nothing, doesn't match

Disclaimer

This was very quickly thrown together, so it's very much not optimized in any way. See the source for details on maybe not low-hanging, but definitley juicy performance improvements that could be made.

Happy matching!


Build Status Coverage Status