Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
condition-switch
Advanced tools
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.
To install the library, you can use npm or yarn:
npm install condition-switch
# or
yarn add condition-switch
# or
pnpm add condition-switch
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"
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"
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>
)
}
FAQs
a condition switch library for js/ts
The npm package condition-switch receives a total of 6 weekly downloads. As such, condition-switch popularity was classified as not popular.
We found that condition-switch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.