@sharyn/util.toggle

toggle: Toggles between two values, and defaults to the first one.

Usage no npm install needed!

<script type="module">
  import sharynUtilToggle from 'https://cdn.skypack.dev/@sharyn/util.toggle';
</script>

README

🌹 toggle

toggle: Toggles between two values, and defaults to the first one.

Installation

npm i @sharyn/util.toggle
# or
yarn add @sharyn/util.toggle

You can alternatively install the @sharyn/util package, or the entire sharyn library.

Arguments

currentValue (any): The current value.

firstValue (any): The first value.

secondValue (any): The second value.

Returns

any: The other option.

Example

toggle(current, 'a', 'b') // if current === 'a', returns 'b'
toggle(current, 'a', 'b') // if current === 'b', returns 'a'
toggle(current, 'a', 'b') // if current === 'x', returns 'a'

Deep equality is supported:

cycle({ a: 1 }, { a: 1 }, { a: 2 }) // { a: 2 }

Imports

Depending on the package you are using, you can import or require toggle in the following ways:

// If you installed @sharyn/util.toggle
import toggle from '@sharyn/util.toggle' // smaller size, better for client bundles

// If you installed @sharyn/util
import toggle from '@sharyn/util/toggle' // smaller size, better for client bundles
import { toggle } from '@sharyn/util' // more convenient in Node environments

// If you installed sharyn
import toggle from 'sharyn/util/toggle' // smaller size, better for client bundles
import { toggle } from 'sharyn/util' // more convenient in Node environments

This package is part of Sharyn, a collection of utilities and helpers.