Skip to content

Commit

Permalink
docs: add invariant and nullthrow
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 26, 2023
1 parent fefa70c commit 42ec1e8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
12 changes: 9 additions & 3 deletions docs/src/components/export-meta-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import { useLatestExportsSizes } from '../hooks/use-latest-exports-sizes';
import { useMemo } from 'react';
import { humanReadableSize } from '../libs/sizes';

export default function ExportMetaInfo() {
interface ExportMetaInfoProps {
slug?: string;
}

export default function ExportMetaInfo({ slug: _slug }: ExportMetaInfoProps) {
const { data } = useLatestExportsSizes();
const _slug = useRouter().asPath.split(/[?#]/)[0];
const slug = _slug.startsWith('/') ? _slug.slice(1) : _slug;
const router = useRouter();

const _ = (_slug || router.asPath.split(/[?#]/)[0]);
const slug = _.startsWith('/') ? _.slice(1) : _;

const [humanReadableRawSize, humanReadableGzipSize, humanReadableBrotliSize] = useMemo(() => {
if (!data) return ['loading...', 'loading...'];
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"compose-context-provider": {},
"context-state": {},
"create-fixed-array": {},
"invariant-nullthrow": {},
"noop": {},
"no-ssr": {},
"rem": {},
Expand Down
62 changes: 62 additions & 0 deletions docs/src/pages/invariant-nullthrow.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: invariant & nullthrow
---

import ExportMetaInfo from '../components/export-meta-info';

# invariant

<ExportMetaInfo slug="invariant" />

```ts
import { invariant } from 'foxact/invariant';

invariant(true);
// => undefined
invariant(false);
// => undefined
invariant(0);
// => undefined
invariant('');
// => undefined
invariant(null);
// Error: Value is null or undefined
invariant(undefined);
// Error: Value is null or undefined
invariant();
// Error: Value is null or undefined


let value: string | null;
invariant(value);
console.log(value);
// ^?: string
```

# nullthrow

<ExportMetaInfo slug="nullthrow" />

```ts
import { nullthrow } from 'foxact/nullthrow';

nullthrow(true);
// => undefined
nullthrow(false);
// => undefined
nullthrow(0);
// => undefined
nullthrow('');
// => undefined
nullthrow(null);
// Error: Value is null or undefined
nullthrow(undefined);
// Error: Value is null or undefined
nullthrow();
// Error: Value is null or undefined

let nullable: string | null;
const value = nullthrow(nullable);
console.log(value);
// ^: string
```

0 comments on commit 42ec1e8

Please sign in to comment.