Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
David Blass committed Dec 22, 2024
1 parent bb2da6b commit 2ac3a64
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 62 deletions.
4 changes: 2 additions & 2 deletions ark/attest/__tests__/snapPopulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ contextualize(() => {
fromHere("benchExpectedOutput.ts")
).replaceAll("\r\n", "\n")
equal(actual, expectedOutput)
}).timeout(20000)
}).timeout(60000)

it("snap populates file", () => {
const actual = runThenGetContents(fromHere("snapTemplate.ts"))
const expectedOutput = readFile(
fromHere("snapExpectedOutput.ts")
).replaceAll("\r\n", "\n")
equal(actual, expectedOutput)
}).timeout(20000)
}).timeout(60000)
})
98 changes: 50 additions & 48 deletions ark/docs/components/AutoDocs.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
import { flatMorph } from "@ark/util"
import { entriesOf, hasKey } from "@ark/util"
import { ark } from "arktype"
import { Fragment } from "react"

const flattened = flatMorph(ark.internal.resolutions, (k, v) =>
"description" in v ? [k, v.description] : []
)
type DescriptionEntry = [alias: string, description: string]

const groupedEntries = Object.entries(flattened).reduce(
(acc, [k, v]) => {
if (!k.includes(".")) acc.root.push([k, v])
else {
const [group, ...rest] = k.split(".")
acc.other[group] ??= { vals: [] }
const groupType = rest.join(".")
if (groupType === "root") acc.other[group].description = v
else acc.other[group].vals.push([groupType, v])
}
return acc
},
{ root: [], other: {} } as {
root: [string, string][]
other: Record<string, { description?: string; vals: [string, string][] }>
}
)
const root: DescriptionEntry[] = []
const other: Record<
string,
{ description?: string; vals: DescriptionEntry[] }
> = {}

entriesOf(ark.internal.resolutions).forEach(([k, v]) => {
if (!hasKey(v, "description")) return

if (!k.includes(".")) return root.push([k, v.description])

const [group, ...rest] = k.split(".")
other[group] ??= { vals: [] }
const groupType = rest.join(".")
if (groupType === "root") other[group].description = v.description
else other[group].vals.push([groupType, v.description])
})

const rootElements = root.map(([k, v]) => (
<tr key={k}>
<td>{k}</td>
<td>{v}</td>
</tr>
))

const otherElements = Object.entries(other).map(([k, v]) => (
<Fragment key={k}>
<tr className="font-bold" id={k}>
<td colSpan={3}>
<a
href={`#${k}`}
className="w-full h-full no-underline hover:underline"
>
{k}
{v.description ? ` - ${v.description}` : ""}
</a>
</td>
</tr>
{v.vals.map(([k, v]) => (
<tr key={k}>
<td>{k}</td>
<td>{v}</td>
</tr>
))}
</Fragment>
))

export const AutoDocs = () => (
<table>
Expand All @@ -33,33 +60,8 @@ export const AutoDocs = () => (
</tr>
</thead>
<tbody>
{groupedEntries.root.map(([k, v]) => (
<tr key={k}>
<td>{k}</td>
<td>{v}</td>
</tr>
))}
{Object.entries(groupedEntries.other).map(([key, v]) => (
<Fragment key={key}>
<tr className="font-bold" id={key}>
<td colSpan={3}>
<a
href={`#${key}`}
className="w-full h-full no-underline hover:underline"
>
{key}
{v.description ? ` - ${v.description}` : ""}
</a>
</td>
</tr>
{v.vals.map(([k, v]) => (
<tr key={k}>
<td>{k}</td>
<td>{v}</td>
</tr>
))}
</Fragment>
))}
{...rootElements}
{...otherElements}
</tbody>
</table>
)
12 changes: 0 additions & 12 deletions ark/docs/components/snippets/contentsById.ts
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
export default {
betterErrors:
'import { type, type ArkErrors } from "arktype"\n\nconst user = type({\n\tname: "string",\n\tplatform: "\'android\' | \'ios\'",\n\t"versions?": "(number | string)[]"\n})\n\ninterface RuntimeErrors extends ArkErrors {\n\t/**platform must be "android" or "ios" (was "enigma")\nversions[2] must be a number or a string (was bigint)*/\n\tsummary: string\n}\n\nconst narrowMessage = (e: ArkErrors): e is RuntimeErrors => true\n\n// ---cut---\nconst out = user({\n\tname: "Alan Turing",\n\tplatform: "enigma",\n\tversions: [0, "1", 0n]\n})\n\nif (out instanceof type.errors) {\n\t// ---cut-start---\n\tif (!narrowMessage(out)) throw new Error()\n\t// ---cut-end---\n\t// hover summary to see validation errors\n\tconsole.error(out.summary)\n}\n',
clarityAndConcision:
'// @errors: 2322\nimport { type } from "arktype"\n// this file is written in JS so that it can include a syntax error\n// without creating a type error while still displaying the error in twoslash\n// ---cut---\n// hover me\nconst user = type({\n\tname: "string",\n\tplatform: "\'android\' | \'ios\'",\n\t"versions?": "number | string)[]"\n})\n',
deepIntrospectability:
'import { type } from "arktype"\n\nconst user = type({\n\tname: "string",\n\tdevice: {\n\t\tplatform: "\'android\' | \'ios\'",\n\t\t"version?": "number | string"\n\t}\n})\n\n// ---cut---\nuser.extends("object") // true\nuser.extends("string") // false\n// true (string is narrower than unknown)\nuser.extends({\n\tname: "unknown"\n})\n// false (string is wider than "Alan")\nuser.extends({\n\tname: "\'Alan\'"\n})\n',
intrinsicOptimization:
'import { type } from "arktype"\n// prettier-ignore\n// ---cut---\n// all unions are optimally discriminated\n// even if multiple/nested paths are needed\nconst account = type({\n\tkind: "\'admin\'",\n\t"powers?": "string[]"\n}).or({\n\tkind: "\'superadmin\'",\n\t"superpowers?": "string[]"\n}).or({\n\tkind: "\'pleb\'"\n})\n',
unparalleledDx:
'// @noErrors\nimport { type } from "arktype"\n// prettier-ignore\n// ---cut---\nconst user = type({\n\tname: "string",\n\tplatform: "\'android\' | \'ios\'",\n\t"version?": "number | s"\n\t// ^|\n})\n'
}

0 comments on commit 2ac3a64

Please sign in to comment.