/innet-switch

Innet jsx plugin to switch elements inside

Primary LanguageJavaScriptMIT LicenseMIT

InnetJs logo by Mikhail Lysikov

  @innet/switch

 

NPM downloads changelog license

Abstract

switch is a jsx plugin.
It helps to select the first children that does not return undefined.

stars watchers

Install

npm

npm i @innet/switch

yarn

yarn add @innet/switch

switchSync

Use it if you do not want to handle promises.

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 = (
  <switch>
    {undefined}
    test1
    {'test2'}
  </switch>
)

innet(app, handler)
// 'test1'

switchAsync

Use it when you want to handle promises.
It waits while a promise ends and the continues the checking.

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 = (
  <switch>
    {undefined}
    {new Promise(resolve => resolve('test1'))}
    {'test2'}
  </switch>
)

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.

issues