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

switchem

v1.1.2

Published

An extensible, functional switch with a chainable API

Downloads

3

Readme

switchem

An extensible, functional switch with a chainable API

Table of contents

Usage

import switchem, { addCustomCase } from "switchem";

// create switch statements with the chainable API
const sw1 = switchem()
  .is("foo", "I am foo")
  .not("bar", "I am not bar")
  .default("I must be bar!");

console.log(sw1.match("baz")); // I am not bar
console.log(sw1.match("bar")); // I must be bar!

// extend existing switch statements by simply adding to them
const sw2 = sw1.is(value => {
  return value === "bar";
}, "I am actually bar");

console.log(sw2.match("baz")); // I am not bar
console.log(sw2.match("bar")); // I am actually bar

// or add commonly-used cases for reuse
addCustomCase("isDivisibleBy", (testValue, matchValue) => {
  return matchValue % testValue === 0;
});

console.log(
  switchem()
    .isDivisibleBy(7, "divisible by seven")
    .match(49)
); // divisible by seven

switchem API

default

default(defaultValue: any): Switchem

Set the default value for the switch, which is returned if none of the cases match.

const sw = switchem().default("default");

is

is(testValue: any, matchResult: ?any = true): Switchem

Add a case statement that tests for equality with the matchValue.

const sw = switchem()
  // functions are executed passing the switched value
  .is(value => {
    return value % 2 === 0;
  }, "I am an even number")
  // regex values are tested via re.test()
  .is(/bar/g, "I contain bar")
  // NaN value comparisons are supported
  .is(NaN, "I am a NaN")
  // static values test for strict equality
  .is("foo", "I am foo");

console.log(sw.match(4)); // I am an even number
console.log(sw.match("bar")); // I contain bar
console.log(sw.match("foo")); // I am foo

If you provide a function as a matchResult, by default this will be called with both the testValue and the matchValue

const sw = switchem().is('foo', (testValue, matchValue) => {
  return `${matchValue} match found: ${testValue}`.
});

console.log(sw.match('foo')); // foo match found: foo

This setting can be disabled by setting runMatchCallback to false in options.

match

match(matchValue: any): any

Find the match for matchValue based on the existing cases provided in the switch.

const sw = switchem().is("foo", "I am foo");

console.log(sw.match("foo")); // I am foo

merge

merge(...switchems: Array): Switchem

Merge the switchem instances passed into a new, combined instance.

const original = switchem();
const first = switchem()
  .default("defaultValue")
  .is("first", "I am first");
const second = switchem({ runMatchCallback: false }).is(
  "second",
  "I am second"
);

const merged = original.merge(first, second);

// merged is the same as
// switchem({runMatchCallback: false})
//   .default('defaultValue');
//   .is('first', 'I am first')
//   .is('second', 'I am second')

not

not(testValue: any, matchResult: ?any = true): Switchem

Add a case statement that tests for non-equality with the matchValue.

const sw = switchem()
  // functions are executed passing the switched value
  .not(value => {
    return value % 2 === 0;
  }, "I am an odd number")
  // regex values are tested via re.test()
  .not(/bar/g, "I do not contain bar")
  // static values test for strict equality
  .not("foo", "I am not foo");

console.log(sw.match(3)); // I am an odd number
console.log(sw.match("bar")); // I am not foo
console.log(sw.match("foo")); // I do not contain bar

If you provide a function as a matchResult, by default this will be called with both the testValue and the matchValue

const sw = switchem().not('foo', (testValue, matchValue) => {
  return `${matchValue} non-match found: ${testValue}`.
});

console.log(sw.match('bar')); // bar non-match found: foo

This setting can be disabled by setting runMatchCallback to false in options.

Options

runMatchCallback

boolean, defaults to true

When this option is true, then any functions that are used as matchResult values will be executed upon match.

const sw = switchem().is("foo", (testValue, matchValue) => {
  return [matchValue, testValue];
});

console.log(sw.match("foo")); // ['foo', 'foo']

When set to false, the method is returned like standard values.

const sw = switchem({ runMatchCallback: false }).is(
  "foo",
  (testValue, matchValue) => {
    return [matchValue, testValue];
  }
);

console.log(sw.match("foo")); // (testValue, matchValue) => { return [matchValue, testValue]; }

Additional methods

addCustomCase

addCustomCase(name: string, method: function, isNot: ?boolean): void

Adds a custom case method to the switchem prototype, which provides convenient reuse of a commonly-applied case.

// use existing utility methods
import { contains } from "ramda";
import { addCustomCase } from "switchem";

addCustomCase("contains", contains);

const sw1 = switchem().contains("bar", "I contain bar!");

console.log(sw1.match(["foo", "bar", "baz"])); // I contain bar!

// or add your own
addCustomCase(
  "notDivisibleBy",
  (testValue, matchValue) => {
    return matchValue % testValue === 0;
  },
  true
);

const sw2 = switchem().notDivisibleBy(7, "not divisible by seven");

console.log(s2.match(12)); // not divisible by seven

This method will be available for all uses of switchem after execution, so it is recommended to run this method as early in your app initialization as possible.

Browser support

  • Chrome (all versions)
  • Firefox (all versions)
  • Edge (all versions)
  • Opera 15+
  • IE 9+
  • Safari 6+
  • iOS 8+
  • Android 4+

Development

Standard stuff, clone the repo and npm install dependencies. The npm scripts available:

  • build => run rolup to build dist files
  • dev => run webpack dev server to run example app / playground
  • dist => runs build and build-minified
  • lint => run ESLint against all files in the src folder
  • prepublish => runs compile-for-publish
  • prepublish:compile => run lint, test:coverage, transpile:es, transpile:lib, dist
  • test => run AVA test functions with NODE_ENV=test
  • test:coverage => run test but with nyc for coverage checker
  • test:watch => run test, but with persistent watcher
  • transpile:lib => run babel against all files in src to create files in lib
  • transpile:es => run babel against all files in src to create files in es, preserving ES2015 modules (for pkg.module)