swutch

A simple switch type utility.

Usage no npm install needed!

<script type="module">
  import swutch from 'https://cdn.skypack.dev/swutch';
</script>

README

swutch

A simple switch type utility.

swutch

-noun

  1. a word used in place of switch, by accident, of course
  2. a spelling error where the "i" is replaced by a "u" due to one being extremely delusional from late night conversations

Origin: 2011 English

Hey Barry, I think we might not have enough time to go to the store, cook, and get to the movies on time. Maybe we should "swutch" something up?

Source: urbandictionary.com

Usage

import swutch from 'swutch'

const result = swutch([
    [condition1, result1],
    [condition2, result2],
    [condition3, result3],
], defaultValue)

Examples

Using an array

import swutch from 'swutch'

const result = swutch([
    [ 1 + 1 === 4, 'Not true' ],
    [ 2 + 3 === 5, 'Two plus three does equal five' ],
    [ 5 + 7 === 24, 'Not true.' ],
], 'None evaluated to true')

// result equals 'Two plus three does equal five'

Using an object

import swutch from 'swutch'

const getStatement = animal => swutch({
    'dog': 'Your favourite animal is a dog.',
    'snake': 'Your favourite animal is a snake.',
    'dog': 'Your favourite animal is a dog.',
}, animal, 'You don't have a favourite animal!')

getStatement('dog')
// returns 'Your favourite animal is a dog.'

getStatement('snake')
// returns 'Your favourite animal is a snake.'

getStatement()
// returns 'You don't have a favourite animal!'