1.0.15 • Published 1 year ago

@scripthungry/better-switch v1.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

RelativeCI

@scripthungry/better-switch

A simple alternative to the switch statement for matching strings and returning values

To install and use

npm i @scripthungry/better-switch

In your code:

import betterSwitch from '@scripthungry/better-switch'

Examples

Javascript:

Simple example:

const returnVal = betterSwitch('test', {
  test: () => 'test value', 
  default: () => 'default value' 
}) // returnVal === 'test value'

Non-existent key returns default key value:

const returnVal2 = betterSwitch('non existent key', { 
  test: () => 'test value', 
  default: () => true 
}) // returnVal2 === true

Error due to non existent key and no default key:

const returnVal3 = betterSwitch('non existent key', { 
  test: () => 'test value', 
  test2: 'test2 value' 
})

Typescript:

Enforcing a consistent return type using type inference from the first return value:

const returnVal = betterSwitch('test', { 
  test: () => 'test value', 
  default: () => 'default value' 
}) // returnVal === 'test value'

Enforcing a specified consistent return type:

const returnVal = betterSwitch<string>('test', { 
  test: () => 'test value', 
  default: () => 'default value' 
}) // returnVal === 'test value'

Typescript error because the default key does not return the same type as the first key:

const returnVal = betterSwitch('test', { 
  test: () => 'test value', 
  default: () => true 
})

Allowing unknown return types:

const returnVal = betterSwitch<unknown>('non existent key', {
  test: () => 'test value', 
  default: () => true 
}) // returnVal === true

Allowing multiple specified return types

const returnVal = betterSwitch<string | boolean>('non existent key', { 
  test: () => 'test value', 
  default: () => true 
}) // returnVal === true
1.0.15

1 year ago

1.0.14

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago