An open API service indexing awesome lists of open source software.

https://github.com/zapolnoch/choose


https://github.com/zapolnoch/choose

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

First element matching a boolean condition:

```sh
npm i choose-matching
```

```ts
const Component = () => (



{choose(
[test === "blue", ],
[test === "green", ],
[true, ],
)}


)
```

## Why?

Because `if` statement is a statement, but here we expect an expression.

## Alternatives

### Self-invoking function

```jsx


{(() => {
switch (test) {
case "blue":
return
case "green":
return
default:
return
}
})()}

```

### Pattern matching

Based on [TC39 proposal](https://github.com/tc39/proposal-pattern-matching) or [ts-pattern](https://github.com/gvergnaud/ts-pattern):

```js
match (test) {
when ("blue"):
when ("green"):
default:
}
```

### Modern way

Use languages in which `if` and `switch` are expressions, e.g. [Civet](https://github.com/DanielXMoore/Civet).