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 🙏

© 2026 – Pkg Stats / Ryan Hefner

switchr

v1.0.4

Published

The switch statement alternative

Downloads

17

Readme

Switchr

Switchr provides a simple, clean, and flexible alternative to switch statements.

Installation

npm install switchr

Usage

Methods

Basic Usage

const basicSwitchr = switchr({
  foo: 'foo foo',
  buzz: (str) => {
    return 'buzz buzz ' + str;
  },
  default: 'foobarbuzz'
});

basicSwitchr.get('foo'); // 'foo foo'
basicSwitchr.get('buzz', 'baby'); // 'buzz buzz baby'
basicSwitchr.get('non-existent'); // 'foobarbuzz'

Initialization

  • Import
import switchr from 'switchr'; /* ES6 */

/* OR */

const switchr = require('switchr').default; /* require */
  • Switchr can be initialized with either an object or array for ultimate flexibility:
// Object
const switchrObject = switchr({
  foo: 'foo',
  bar: 'bar'
});

// Array
const switchrArray = switchr([
  {
    key: 'foo',
    value: 'foo'
  },
  {
    key: 'bar',
    value: 'bar'
  }
]);
  • Initializing with an array will allow common functionality between multiple cases using the keys property:
const switchrArray = switchr([
  {
    keys: ['one', 'two', 'three'],
    value: 1
  },
  {
    key: 'four',
    value: 4
  }
]);

switchrArray.get('one'); // 1
switchrArray.get('two'); // 1
switchrArray.get('three'); // 1
switchrArray.get('four'); // 4
  • It also accepts functions as values
const switchrWithFunction = switchr({
  foo: (a, b) => {
    return a + b;
  },
});

const switchrWithArrowFunction = switchr({
  bar: (a, b) => {
    return a - b;
  }
});

Get

  • The get method retrieves simple values:
const getSwitchr = switchr({
  getMe: 'You got me!'
});

getSwitchr.get('getMe'); // 'You got me!'
  • Or executes functions
const getFunctionsSwitchr = switchr({
  getMeFn: () => { // Without arguments
    return 'You got me again!';
  },
  sum: (a, b) => { // With arguments
    return a + b;
  }
});

getSwitchr.get('getMeFn'); // 'You got me again!'
getSwitchr.get('sum', 10, 15); // 25
  • get will ignore arguments passed in when the function doesn't expect them:
getSwitchr.get('getMeFn', 10, 15) // 'You got me again!'
  • Use a default key to handle cases that don't exist
const switchrWithDefault = switchr({
  foo: 'foo',
  default: 'foobar'
});

switchrWithDefault.get('doesntExist') // 'foobar'
  • If no default key exists, get will return null:
const switchrWithoutDefault = switchr({
  foo: 'foo'
});

switchrWithDefault.get('doesntExist') // null

Set

  • The set method adds a new case by passing in a key and value:
const setSwitchr = switchr({
  foo: 'foo'
});
setSwitchr.set('bar', 'bar');
setSwitchr.get('bar'); // 'bar'
  • set also accepts a callback function:
function multiply(a, b) {
  return a * b;
}

const setSwitchr = switchr({
  foo: 'foo'
});

setSwitchr.set('multiply', multiply);
setSwitchr.get('multiply', 10, 20); // 200
  • If a key already exists, set will update that value:
const updateSwitchr = switchr({
  updateMe: 'old value'
});

updateSwitchr.set('updateMe', 'new value!');
updateSwitchr.get('updateMe'); // 'new value!'

Has

  • The has method checks to see if a case exists:
const hasSwitchr = switchr({
  foo: 'foo',
});

hasSwitchr.has('foo'); // true
hasSwitchr.has('bar'); // false

List

  • The list method returns an array available keys:
const listSwitchr = switchr({
  foo: 'foo',
  bar: 'bar',
  buzz: () => {
    return 'buzz buzz';
  },
  default: 'default value'
});

listSwitchr.list(); // ['foo', 'bar', 'buzz', 'default'];

TypeScript Support

Switcher has full TypeScript support

License

MIT