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

redp

v1.0.1

Published

Red Phosphorus - Patter Match Library for ES2015 Era

Downloads

4

Readme

redp

Red Phosphorus - Patter Match Library for ES2015 Era

Build Status

example

import {match, when} from 'redp'

// create function to calculate expression tree
const calc = match.fn({
  [when({type: 'binary', operator: '+'})]: ({left, right}) => calc(left) + calc(right),
  [when({type: 'binary', operator: '*'})]: ({left, right}) => calc(left) * calc(right),
  [when({type: 'unary' , operator: '-'})]: ({value}) => -calc(value),
  [when({type: 'number'})]               : ({value}) => value,
})

// -100 + 200 ==> 100
console.log(calc({
  type: 'binary',
  operator: '+',
  left: {
    type: 'unary',
    operator: '-',
    value: {
      type: 'number',
      value: 100,
    },
  },
  right: {
    type: 'number',
    value: 200,
  },
}))

You could be look for more examples in example/ and test/.

install

$ npm install --save redp

API

import {match, when, Pattern} from 'redp`

match(value, pattern)

Shortcut to match.fn(pattern)(value).

match.fn(patterns)

Create new function to match patterns. patterns should be an Object. Its keys should be created by when function. Returned function accept one argument then try to match patterns. Matching some pattern, return it. If not, return undefined.

when(pattern)

Create pattern key from pattern. pattern is either some javascript value ("string" or true or /regexp/ or (arrow) => func or ...) or Pattern.* function's result. If pattern is some javascript value, it is passed to Pattern.from.

Pattern key is not reusable. If you want to reuse pattern, you could use Pattern.from.

when.and(...patterns)

Shortcut to when(Pattern.and(...patterns)).

when.or(...patterns)

Shortcut to when(Pattern.or(...patterns)).

Pattern

This is a factory for pattern object.

export const Pattern = {
  // Wild card pattern. It can match all values.
  _: ...,

  // Rest pattern. It can match no items or some rest items of array (cannot object!).
  rest: ...,

  // Create pattern object from `object`.
  from(object) {
    // If `object` is pattern object already, return `object`.
    // If `object` is a `RegExp`, create regexp pattern.
    // If `object` is an `Array`, create array pattern.
    // If `object` is a javascript object, create object pattern.
    // If `object` is a function, create condition pattern.
    // Otherwise, create value pattern.
    ...
  },

  // Create regexp pattern.
  regexp(regexp) { ...  },

  // Create array pattern.
  array(array) { ... },

  // Create object pattern.
  object(object) { ... },

  // Create condition pattern.
  condition(predicate) { ... },

  // Create value pattern.
  value(value) { ... },

  // Create 'and' pattern.
  and(...patterns) { ... },

  // Create 'or' pattern.
  or(...patterns) { ... },

  // If `object` is pattern object, return `true`,
  // otherwise return `false`.
  is(object) { ... },
}

note

This library uses ES2015 Symbol and Map. If you want to run this on not ES2015 environment, you can try to use some polyfills. (However, it is not tested. I'm waiting your pull-request)

license

MIT License: http://makenowjust.mit-license.org/2016

(C) 2016 TSUYUSATO Kitsune