https://github.com/d8corp/innet-switch
Innet jsx plugin to switch elements inside
https://github.com/d8corp/innet-switch
Last synced: 11 months ago
JSON representation
Innet jsx plugin to switch elements inside
- Host: GitHub
- URL: https://github.com/d8corp/innet-switch
- Owner: d8corp
- License: mit
- Created: 2022-02-20T15:04:56.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-22T09:48:58.000Z (over 4 years ago)
- Last Synced: 2025-03-14T12:36:36.734Z (over 1 year ago)
- Language: JavaScript
- Size: 91.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @innet/switch
[](https://www.npmjs.com/package/@innet/switch)
[](https://www.npmtrends.com/@innet/switch)
[](https://changelogs.xyz/@innet/switch)
[](https://github.com/d8corp/innet-switch/blob/main/LICENSE)
## Abstract
`switch` is a [jsx plugin](https://www.npmjs.com/package/@innet/jsx).
It helps to select the first children that does not return `undefined`.
[](https://github.com/d8corp/innet-switch/stargazers)
[](https://github.com/d8corp/innet-switch/watchers)
## Install
npm
```bash
npm i @innet/switch
```
yarn
```bash
yarn add @innet/switch
```
## switchSync
Use it if you do not want to handle promises.
```typescript jsx
import innet, { createHandler } from 'innet'
import { switchSync } from '@innet/switch'
import { jsxPlugins } from '@innet/jsx'
import { object } from '@innet/utils'
const handler = createHandler([
object([
jsxPlugins({
switch: switchSync,
}),
]),
])
const app = (
{undefined}
test1
{'test2'}
)
innet(app, handler)
// 'test1'
```
## switchAsync
Use it when you want to handle promises.
It waits while a promise ends and the continues the checking.
```typescript jsx
import innet, { createHandler } from 'innet'
import { switchAsync } from '@innet/switch'
import { jsxPlugins } from '@innet/jsx'
import { object } from '@innet/utils'
const handler = createHandler([
object([
jsxPlugins({
switch: switchAsync,
}),
]),
])
const app = (
{undefined}
{new Promise(resolve => resolve('test1'))}
{'test2'}
)
await innet(app, handler)
// 'test1'
```
Change `resolve('test1')` to `resolve(undefined)` and you get `test2`
## Issues
If you find a bug or have a suggestion, please file an issue on [GitHub](https://github.com/d8corp/innet-switch/issues).
[](https://github.com/d8corp/innet-switch/issues)