Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-switch

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-switch

More intuitive version of a Javascript switch statement.

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Better switch statement

Travis David David Dev npm

The native Javascript switch statement doesn't really fit into the common coding style of Javascript code. Therefore this module aims to handle such use cases in a more intuitive way. This module does not offer the "fall-through" mechanism of a native switch statement.

Usage

npm install -S better-switch

Pass an object as first argument. The default part will be executed in case nothing else matches. If default is not specified and nothing matches it will return undefined.

var betterSwitch = require('better-switch');

var map = {
  foo: function () {
    return 'bar';
  },
  baz: 42,
  default: 'default'
};

var result = betterSwitch('foo', map); // => bar

The associated value can be of any type. If it is a function it will be executed and the result returned. With ES2015 you can make it even clearer:

const baz = 42,
const map = {
  foo: () => 'bar',
  baz,
  default: 'default'
};
const result = betterSwitch('baz', map); // => 42

If you want to return a function you have to wrap it in a function. This is required, because otherwise the function will be executed.

const map = {
  func: function() {
    return function() {
      // do stuff
    }
  }
}

Keywords

FAQs

Package last updated on 08 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc