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

switch-js

v0.0.8

Published

Use switch and case as chainable functions

Downloads

5

Readme

switch-js

中文文档

Install

npm i switch-js

Example

const switcher = require('switch-js');
const valueToTest = 'foo';

switcher(valueToTest)
    .case('foo', () => console.log('foo!'))
    .case('bar', () => console.log('bar!'))
    .default(() => console.log('no match found in given cases!'))
    .always(() => console.log('this is cool!'));

// result:
// foo!
// this is cool!

Check for multiple cases:

switcher(valueToTest)
    .cases(['foo', 'bar'], () => console.log('foo or bar!'))
    .default(() => console.log('no match found in given cases!'));

// result: foo or bar!

Check if value is matched by a function:

switcher(valueToTest)
    .caseWhen(value => typeof value === 'string', () => console.log('a string!'))
    .caseWhen(value => typeof value === 'number', () => console.log('a number!'))
    .default(() => console.log('other datatypes!'));

// result: a string!

Use to to set value:

const valueInUppercase = switcher(valueToTest)
    .case('foo').to('FOO!')
    .case('bar').to('BAR!')
    .value('NO MATCH FOUND!');

// result: FOO!

// .value('NO MATCH FOUND!')
// has the same effect as
// .default().to('NO MATCH FOUND!').value()

Use mapTo to set the value by a function:

const valueInUppercase = switcher(valueToTest)
    .cases(['foo', 'bar']).mapTo(value => value.toUpperCase())
    .default().mapTo(value => value.toUpperCase() + ' IS NOT A MATCH!')
    .value();

// result: 'FOO'

API

switch(value)

start a switch chain

  • value Value to check

case(caseValue[, matchedCallback][, notMatchedCallback])

The case method you know

  • caseValue
  • matchedCallback Function, optional. Invoked if caseValue equals deeply to value
  • notMatchedCallback Function, optional. Invoked otherwise

cases(caseValues[, matchedCallback][, notMatchedCallback])

Check for multiple case values

  • caseValues Array of caseValue
  • matchedCallback Function, optional. Invoked if caseValues includes value
  • notMatchedCallback Function, optional. Invoked otherwise

caseWhen(matcher[, matchedCallback][, notMatchedCallback])

Check by a function

  • matcher A match function used to check if value is a match
  • matchedCallback Function, optional. Invoked if matcher(value) returns a truthy value
  • notMatchedCallback Function, optional. Invoked otherwise

default([noMatchFoundCallback, matchFoundCallback])

The default method you know

  • noMatchFoundCallback Function, optional. Invoked if no match is found
  • matchFoundCallback Function, optional. Invoked otherwise

always(callback)

Invoked anyway

  • callback Function

to(valueToReturn)

Set a new value to return by value() if previous case is a match or it follows default()

  • valueToReturn

mapTo(mapper)

Similiar to to() but set value by a mapper function

  • mapper Function | String. If a string is passed, it will take as a path and invoke _.get(valueToTest, path) from lodash

value(defaultValue)

Return the value

  • defaultValue Default value will return if no match is found