Skip to content

Commit

Permalink
docs: defaultable tuple elements, fix some sidebar header links
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Jan 4, 2025
1 parent 8909026 commit 283689e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
12 changes: 6 additions & 6 deletions ark/docs/content/docs/objects/arrays/meta.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"title": "arrays and tuples",
"pages": [
"lengths",
"[prefix](/docs/objects#arrays-prefix)",
"[optional](/docs/objects#arrays-optional)",
"[defaultable](/docs/objects#arrays-defaultable)",
"[variadic](/docs/objects#arrays-variadic)",
"[postfix](/docs/objects#arrays-postfix)"
"[lengths](/docs/objects#arrays-lengths)",
"[prefix](/docs/objects#tuples-prefix)",
"[defaultable](/docs/objects#tuples-defaultable)",
"[optional](/docs/objects#tuples-optional)",
"[variadic](/docs/objects#tuples-variadic)",
"[postfix](/docs/objects#tuples-postfix)"
]
}
55 changes: 43 additions & 12 deletions ark/docs/content/docs/objects/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -623,19 +623,17 @@ const myTuple = type([

### defaultable [#tuples-defaultable]

🚧 Coming soon ™️🚧
Defaultable elements are optional elements that will be assigned their specified default if not present in the tuple's input.

### optional [#tuples-optional]
A tuple may include zero or more defaultable elements following its prefix elements and preceding its non-defaultable optional elements.

Tuples can include any number of optional elements following its prefix elements.

Like in TypeScript, optional elements are mutually exclusive with postfix elements.
Like optional elements, defaultable elements are mutually exclusive with postfix elements.

<SyntaxTabs>
<SyntaxTab string>

```ts
const myTuple = type(["string", "boolean?", "number?"])
const myTuple = type(["string", "boolean = false", "number = 0"])
```

</SyntaxTab>
Expand All @@ -645,8 +643,8 @@ const myTuple = type(["string", "boolean?", "number?"])
```ts
const myTuple = type([
type.string,
type.boolean.optional(),
type.number.optional()
type.boolean.default(false),
type.number.default(0)
])
```

Expand All @@ -661,24 +659,57 @@ const myTuple = type([
{
name: "string"
},
"?"
"=",
() => ({ name: "Anon Eemuss" })
]
])
```

</SyntaxTab>

<SyntaxTab args>
</SyntaxTabs>

### optional [#tuples-optional]

Optional elements are tuple elements that may or may not be present in the input that do not have a default value.

A tuple may include zero or more optional elements following its prefix and defaultable elements and preceding either a variadic element or the end of the tuple.

Like in TypeScript, optional elements are mutually exclusive with postfix elements.

<SyntaxTabs>
<SyntaxTab string>

```ts
const myTuple = type(["string", "bigint = 999n", "boolean?", "number?"])
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
const myTuple = type([
type.string,
type.bigint.default(999n),
type.boolean.optional(),
type.number.optional()
])
```

</SyntaxTab>

<SyntaxTab tuple>

```ts
const myTuple = type([
"string",
type(
[
{
name: "string"
},
"?"
)
]
])
```

Expand Down
2 changes: 1 addition & 1 deletion ark/docs/content/docs/objects/properties/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"[defaultable](/docs/objects#properties-defaultable)",
"[index](/docs/objects#properties-index)",
"[undeclared](/docs/objects#properties-undeclared)",
"[merge](/docs/objects#properties-merge)",
"[spread](/docs/objects#properties-spread)",
"[keyof](/docs/objects#properties-keyof)",
"[get](/docs/objects#properties-get)"
]
Expand Down
2 changes: 2 additions & 0 deletions ark/type/parser/tupleExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator

export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator

export type ArgTwoOperator = Exclude<IndexOneOperator, "?" | "=">

export type TuplePostfixOperator = "[]" | "?"

export type TupleInfixOperator = "&" | "|" | "=>" | "=" | ":" | "@"
Expand Down
6 changes: 4 additions & 2 deletions ark/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type { BaseType } from "./methods/base.ts"
import type { instantiateType } from "./methods/instantiate.ts"
import type { validateDeclared } from "./parser/definition.ts"
import type {
IndexOneOperator,
ArgTwoOperator,
IndexZeroOperator,
TupleInfixOperator
} from "./parser/tupleExpressions.ts"
Expand Down Expand Up @@ -70,7 +70,7 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
_1: zero extends "keyof" ? type.validate<one, $>
: zero extends "instanceof" ? conform<one, Constructor>
: zero extends "===" ? conform<one, unknown>
: conform<one, IndexOneOperator>,
: conform<one, ArgTwoOperator>,
..._2: zero extends "===" ? rest
: zero extends "instanceof" ? conform<rest, readonly Constructor[]>
: one extends TupleInfixOperator ?
Expand Down Expand Up @@ -187,9 +187,11 @@ export type DeclarationParser<$> = <preinferred>() => {
}

export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>

export type InstanceOfTypeParser<$> = <const t extends object>(
ctor: Constructor<t>
) => Type<t, $>

export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(
...values: values
) => Type<values[number], $>
Expand Down

0 comments on commit 283689e

Please sign in to comment.