Skip to content

Commit

Permalink
Add Repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-mp committed Nov 27, 2024
1 parent 5b5ae54 commit 3769f2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/helpers/isFilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import type { KeyTextField } from "../types/value/keyText"
import type { LinkField } from "../types/value/link"
import type { LinkToMediaField } from "../types/value/linkToMedia"
import type { NumberField } from "../types/value/number"
import type { Repeatable } from "../types/value/repeatable"
import type { RichTextField } from "../types/value/richText"
import type { SelectField } from "../types/value/select"
import type { SharedSlice } from "../types/value/sharedSlice"
import type { Slice } from "../types/value/slice"
import type { SliceZone } from "../types/value/sliceZone"
import type { TimestampField } from "../types/value/timestamp"
import type { TitleField } from "../types/value/title"
import type { AnyRegularField } from "../types/value/types"
import type { AnyRegularField, AnyRepeatableField } from "../types/value/types"

/**
* Determines if a value is not nullish (i.e. not `null` or `undefined`). This
Expand Down Expand Up @@ -266,6 +267,21 @@ export const integrationField = isNonNullish as <
// TODO: Remove when we remove support for deprecated `integrationFields` export.
export const integrationFields = integrationField

/**
* Determines if a Repeatable has at least one item.
*
* @param repeatable - Repeatable to check.
*
* @returns `true` if `repeatable` contains at least one item, `false`
* otherwise.
*/

export const repeatable = (
repeatable: Repeatable | null | undefined,
): repeatable is Repeatable<Array<AnyRepeatableField>, "filled"> => {
return isNonNullish(repeatable) && isNonEmptyArray(repeatable)
}

/**
* Determines if a Group has at least one item.
*
Expand Down
9 changes: 9 additions & 0 deletions src/types/value/repeatable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { AnyRepeatableField, FieldState } from "./types"

/**
* A list of repeatable fields.
*/
export type Repeatable<
Fields extends Array<AnyRepeatableField> = Array<AnyRepeatableField>,
State extends FieldState = FieldState,
> = State extends "empty" ? [] : [Fields, ...Fields[]]
7 changes: 7 additions & 0 deletions src/types/value/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { KeyTextField } from "./keyText"
import type { LinkField } from "./link"
import type { LinkToMediaField } from "./linkToMedia"
import type { NumberField } from "./number"
import { Repeatable } from "./repeatable"

Check failure on line 14 in src/types/value/types.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 18)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 14 in src/types/value/types.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 20)

All imports in the declaration are only used as types. Use `import type`

Check failure on line 14 in src/types/value/types.ts

View workflow job for this annotation

GitHub Actions / Suite (ubuntu-latest, Node 22)

All imports in the declaration are only used as types. Use `import type`
import type { RichTextField } from "./richText"
import type { SelectField } from "./select"
import type { TimestampField } from "./timestamp"
Expand All @@ -26,6 +27,11 @@ export type EmptyObjectField = Record<string, never>
*/
export type FieldState = "empty" | "filled"

/**
* Any field that can be repeated.
*/
export type AnyRepeatableField = LinkField

/**
* Any regular field that can be nested in a group-like field.
*/
Expand All @@ -46,6 +52,7 @@ export type AnyRegularField =
| BooleanField
| GeoPointField
| IntegrationField
| Repeatable

/**
* Any field that can be used in a slice's primary section.
Expand Down

0 comments on commit 3769f2b

Please sign in to comment.