toggle
: Toggles between two values, and defaults to the first one.
npm i @sharyn/util.toggle
# or
yarn add @sharyn/util.toggle
You can alternatively install the @sharyn/util package, or the entire sharyn library.
currentValue (any): The current value.
firstValue (any): The first value.
secondValue (any): The second value.
any: The other option.
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 }
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.