https://github.com/zapolnoch/choose
https://github.com/zapolnoch/choose
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zapolnoch/choose
- Owner: zapolnoch
- License: mit
- Created: 2023-10-18T17:28:51.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-19T02:44:32.000Z (over 2 years ago)
- Last Synced: 2025-09-21T14:23:21.683Z (9 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/choose-matching
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).