Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

condition-switch

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

condition-switch

a condition switch library for js/ts

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Condition Switch Library

This TypeScript library provides a utility function condSwitch for performing switch-like operations based on conditions with associated values.

The condSwitch function is convenient to use in arrow functions, JSX, and other situations where if or switch statements are cumbersome or not allowed.

Installation

To install the library, you can use npm or yarn:

npm install condition-switch
# or
yarn add condition-switch
# or
pnpm add condition-switch

Usage

Basic Usage

Using ConditionWithValueObject

A ConditionWithValueObject is an object with two properties: condition and value. This format is particularly useful when you want to make your code more readable and self-documenting.

import conditionSwitch, { ConditionWithValueObject } from 'condition-switch'

const conditions: ConditionWithValueObject<string>[] = [
  { condition: false, value: 'Condition 1' },
  { condition: true, value: 'Condition 2' }
]

const result = conditionSwitch(conditions, 'Default Value')
console.log(result) // Output: "Condition 2"
Using ConditionWithValueArray

A ConditionWithValueArray is a tuple where the first element is the condition and the second element is the value. This format can be more concise and is useful when you want to keep your code compact.

import conditionSwitch, { ConditionWithValueArray } from 'condition-switch'

const conditions: ConditionWithValueArray<string>[] = [
  [false, 'Condition 1'],
  [true, 'Condition 2']
]

const result = conditionSwitch(conditions, 'Default Value')
console.log(result) // Output: "Condition 2"

Using Functions

You can also use functions for conditions and values to delay their evaluation until they are needed. Please use function when there is performance concern or the return type is function.

const result = condSwitch(
  [
    { condition: false, value: () => 1 },
    { condition: () => true, value: () => 2 },
    [false, () => 3],
    [() => true, () => 4]
  ],
  () => 0
)
console.log(result) // Output: 2

const func = condSwitch<() => number>(
  [
    [() => true, () => () => 42],
    [() => false, () => () => 0]
  ],
  () => () => 100
)

const result = func()
console.log(result) // Output: 2
const MyComponent = () => {
  //....

  return (
    <div>
      {condSwitch(
        [
          {
            condition: isLoading,
            value: () => <Load />
          },
          {
            condition: isError,
            value: () => <ErrorMessage />
          }
        ],
        () => (
          <Content />
        )
      )}
    </div>
  )
}

Keywords

FAQs

Package last updated on 17 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc