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

zwitch

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zwitch

Handle values based on a property

  • 2.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.3M
increased by6.37%
Maintainers
1
Weekly downloads
 
Created

What is zwitch?

The zwitch npm package is a utility for handling polymorphic dispatch based on a property value. It allows you to define a default handler and specific handlers for different values of a given property. This can be particularly useful for tasks like routing, handling different types of data structures, or implementing pattern matching in your code.

What are zwitch's main functionalities?

Handling different types of messages

This feature demonstrates how zwitch can be used to handle different types of messages based on their 'type' property. It shows setting up handlers for specific types and handling invalid or unknown types.

{"const zwitch = require('zwitch');\nconst handle = zwitch('type');\n\nhandle.invalid = (value) => { console.error('Invalid type:', value); };\nhandle.unknown = (value) => { console.error('Unknown type:', value); };\nhandle.handlers.string = (value) => { console.log('String:', value); };\nhandle.handlers.number = (value) => { console.log('Number:', value); };\n\nhandle({type: 'string', value: 'Hello, world!'});\nhandle({type: 'number', value: 42});\nhandle({type: 'boolean', value: true});"}

Default and specific handling

This feature illustrates using zwitch to define a default handler and a specific handler for the 'foo' key. It demonstrates how unhandled keys are processed by the default handler.

{"const zwitch = require('zwitch');\nconst handle = zwitch('key');\n\nhandle.default = (value) => { console.log('Default handler:', value); };\nhandle.handlers.foo = (value) => { console.log('Handled foo:', value); };\n\nhandle({key: 'foo', value: 'bar'});\nhandle({key: 'baz', value: 'qux'});"}

Other packages similar to zwitch

Keywords

FAQs

Package last updated on 16 Nov 2022

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