npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typesafe-react

v5.1.0

Published

## Functions

Downloads

252

Readme

Framework agnostic functions, hooks and components.

Functions

when

Exhaustive switch statement with return value.

// handle all cases
const result1 = when(AorB, { A: () => 'a', B: () => 'b' });

// provide a fallback
const result2 = when(ABC, { A: () => 'a', B: () => 'b' }, () => 'abc');

// run some code
when(AorB, { A: () => console.log('a'), B: () => console.log('b') });

// even simpler
console.log(when(AorB, { A: () => 'a', B: () => 'b' }));

| Parameter | Type | Description | | ------------ | --------------------- | ----------------------------------------------------------------------------------------------------------- | | expression | E | Expression to evaluate. | | cases | Record<CE, () => R> | Record that maps some or all possible values of expression to a branch. | | fallback | (() => F) \| never | Default branch. Mandatory (and only allowed) if cases does not cover all possible values of expression. |

| Generic Type | Extends | Description | | ------------ | ------------------ | --------------------------------------------------------- | | E | string \| number | expression's type. Union of strings or numbers. | | CE | E | Union of covered cases. Subunion of E or same as E. | | const R | unknown | Union of return values. | | const F | unknown | Fallback value. |

switchTrue

Switch true statement with return value.

const result = switchTrue(
  [
    [() => isA(), () => 'a'],
    [() => isB(), () => 'b'],
  ],
  () => 'c'
);

| Parameter | Type | Description | | ---------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | | cases | ReadonlyArray<C> | Array of tuples. Each tuple has a callback that returns a condition and a callback that will run if the condition is truthy. | | fallback | (() => F) \| undefined | Default branch. |

| Generic Type | Extends | Description | | ------------ | -------------------------------- | ---------------------------------------- | | const C | [() => unknown, () => unknown] | Tuple of condition and result callbacks. | | const F | unknown | Fallback value. |

Components

When

Exhaustive switch component.

// handle all cases
const result1 = <When expression={AorB} cases={{ A: () => <A />, B: () => <B /> }} />;

// provide a fallback
const result2 = (
  <When expression={ABC} cases={{ A: () => <A />, B: () => <B /> }} fallback={() => <Abc />} />
);

| Prop | Type | Description | | ------------ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | | expression | E | Expression to evaluate. | | cases | Record<CE, () => ReactNode> | Record that maps some or all possible values of expression to a branch that returns content. | | fallback | (() => ReactNode) \| never | Default branch that returns content. Mandatory (and only allowed) if cases does not cover all possible values of expression. |

| Generic Type | Extends | Description | | ------------ | ------------------ | --------------------------------------------------------- | | E | string \| number | expression's type. Union of strings or numbers. | | CE | E | Union of covered cases. Subunion of E or same as E. |

SwitchTrue

Switch true component.

const result = (
  <SwitchTrue
    cases={[
      [() => isA(), () => <A />],
      [() => isB(), () => <B />],
    ]}
    fallback={() => <Fallback />}
  />
);

| Prop | Type | Description | | ---------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | | cases | ReadonlyArray<[() => unknown, () => ReactNode]> | Array of tuples. Each tuple has a callback that returns a condition and a callback that will run if the condition is truthy and return content. | | fallback | (() => ReactNode) \| undefined | Default branch that returns content. |

Show

Component to conditionally render content.

// simple check
const result1 = (
  <Show when={show} fallback={`I am visible if 'show' is falsy.`}>
    I am visible if 'show' is truthy.
  </Show>
);

// multiple conditions
const result1 = <Show when={[show, !hidden]}>I am visible if all conditions are truthy.</Show>;

// use when's value
const result2 = <Show when={nullableArray}>{(data) => data.length}</Show>;

// use whenAll's values
const result2 = (
  <Show when={[nullableArray, nullableObject]} fallback={'Something went wrong!'}>
    {([array, object]) => array.length + object.numberProperty}
  </Show>
);

| Prop | Type | Description | | ---------- | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | | when | W | Single condition to evaluate. | | whenAll | T | Multiple conditions to evaluate. | | fallback | ReactNode | Content to render if when is falsy or whenAll contains some falsy elements. | | children | ReactNode \| ((data: NoInfer<IsEqual<W, never> extends true ? NonNullableElements<T> : NonNullable<W>>) => ReactNode) | Children callback that receives when or whenAll and returns content to render if when is truthy or whenAll only contains truthy elements. |

| Generic Type | Extends | Description | | ------------ | ------------------------ | -------------------------------------- | | W | unknown | when's type. Single condition. | | const T | ReadonlyArray<unknown> | whenAll's type. Tuple of conditions. |

For

Component for rendering lists.

const result = (
  <For each={array} keyFn={(item) => item.id} fallback={`The array is empty.`}>
    {(item, index) => `#${index} ${item.value}`}
  </For>
);

| Prop | Type | Description | | ---------- | ------------------------------------------------ | ---------------------------------------------------------------------------- | | each | ReadonlyArray<I> | Array to iterate over. | | keyFn | (item: NoInfer<I>, index: number) => Key | Function that returns the key for each item. | | children | (item: NoInfer<I>, index: number) => ReactNode | Function that returns the content for each item. No need to define key here. | | fallback | ReactNode | Content to render if each is empty. |

| Generic Type | Extends | Description | | ------------ | ---------------------- | ------------------------ | | I | NonNullable<unknown> | each's elements' type. |