# `Range`

The values of one ordered type from a start up to, but not including, an end.

Use it for spans of your own type, such as a price band, and name the type inside it: `Range<Grams>`. For days, use [`DateRange`](/docs/types/type-date-range.md). For moments, use [`InstantRange`](/docs/types/type-instant-range.md).

## Shape

| Field | Type | Required? |
|---|---|---|
| `start` | `T` | No |
| `end` | `T` | No |

Write every field when you build one, and give a field that isn't required as `none`.

Its parts keep these rules, and a value that breaks one fails with the rule's name:

- `start_not_after_end`: start <= end

## Example

```jadpo
var example: Range<CalendarDate> = Range { start: "2026-10-01" end: none }
assert range.contains(example, CalendarDate("2026-10-25"))
```

`text.parse(raw, as: Range<CalendarDate>)` reads one from text that comes from outside. Text that isn't one at all fails `malformed`, such as `2026-10-01`, `2026-11-01/2026-10-01` or `2026-10-01/tomorrow`.

This operation gives one: `range.intersection`.

## Good to know

- Give `none` for an end with no limit, so the example runs from 1 October onwards.
- `range.contains`, `range.overlaps` and `range.intersection` work on it.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it, so sort by one of its parts instead.

## In JSON

An object of its parts, in a request, a response or `json.write`:

```json
{"start":"2026-10-01","end":"2026-11-01"}
{"start":"2026-10-01","end":null}
{"start":null,"end":"2026-11-01"}
```

When you read one, a field that isn't required may be left out, or sent as `null`.

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`Range` is in the group Maths and data, and has been in Jadpo since 0.1.0.

### Text form

`text.from` writes one standard text form, the same form `text.parse` reads back: `2026-10-01/2026-11-01`, `2026-10-01/..` and `../2026-11-01`.

### How it is stored

It is stored in one column named for the field, of type `daterange`. `../2026-11-01` is stored as `daterange(NULL::date, '2026-11-01'::date, '[)')`.

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb` and `format: TimeFormat.date_medium`:

| Value | Shown |
|---|---|
| `2026-10-01/2026-11-01` | `1 Oct 2026 – 1 Nov 2026` |
| `2026-10-01/..` | `1 Oct 2026 – …` |
| `../2026-11-01` | `… – 1 Nov 2026` |
| `../..` | `… – …` |

### Operations

- `range.contains(range: Range<T>, value: T) -> Bool`: Whether `value` is inside `range`.
- `range.intersection(a: Range<T>, b: Range<T>) -> Range<T>?`: The part `a` and `b` share; `none` when they don't overlap.
- `range.overlaps(a: Range<T>, b: Range<T>) -> Bool`: Whether `a` and `b` share at least one value.

It also takes `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
