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

@densebrain/ts-when

v0.8.0

Published

JavaScript functional implementation of switch/case written in TypeScript

Downloads

45

Readme

*** THIS IS A FORK - ALL CREDIT GOES TO THE AUTHOR NAWORDAR - I WILL EVENTUALLY DO A PR TO SYNC ***

CircleCI codecov Maintainability devDependencies Status NPM version

Note

This is a fork of a great library when-switch which seems no longer maintained.

Usage

You can convert a ts-when use in a functional way, using a single expression:

Strict Equality

import when from 'ts-switch'

const getDrinkPrice = drink =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', 1.8)
    .else(2.0)

Assertion Matching

You can return a value when given assertion is correct

const x = when
  .true(someVariable === true, "some variable is true")
  .else("some variable is false")

Structural Matching

You can use match method with any object exposing a test method.

Regular Expressions

const getCaseStyle = text =>
  when(text)
    .match(/^([A-Z][a-z]*)+$/, 'UpperCamelCase')
    .match(/^([a-z]+[A-Z][a-z]*)+$/, 'LowerCamelCase')
    .match(/^([a-z]+_[a-z]+)+$/, 'SnakeCase')
    .else('Unknown')

Custom Type Guard Matcher

type SpaceObject = { x: number; y: number; z: number }
type Cube = SpaceObject & { width: number }
type Sphere = SpaceObject & { radius: number }

const SpaceObjectSchema = {
  test: (_: any): _ is SpaceObject =>
    typeof _.x === 'number' &&
    typeof _.y === 'number' &&
    typeof _.z === 'number'
}

const CubeSchema = {
  test: (_: any): _ is Cube =>
    typeof _.width === 'number' && SpaceObjectSchema.test(_)
}

const SphereSchema = {
  test: (_: any): _ is Sphere =>
    typeof _.radius === 'number' && SpaceObjectSchema.test(_)
}

const getObjectVolume = (object: SpaceObject) =>
  // Each match handler will receive correct static type
  when(object)
    .match(CubeSchema, cube => cube.width ** 3)
    .match(SphereSchema, sphere => Math.PI * 3 / 4 * sphere.radius ** 3)
    .else(_ => null)

match and is can both be used in the same when expression.

TypeScript

when is fully compatible with TypeScript, and will check the types you return in each is expression:

const getDrinkPrice = (drink: 'Pepsi' | 'Coke' | 'Orangina'): number =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', 1.8)
    .else(2.0)

Here the return type of the when expression will be number

Union types

For each is or else expression added to the current when expression, the type is added as an union to the previous type.

const getDrinkPrice = (drink: 'Pepsi' | 'Coke' | 'Orangina') =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', true)
    .else('Free')

Here the return type of getDrinkPrice expression will be number | string | boolean