Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

feat: added Duration filters #604

Merged
merged 19 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-badgers-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

added filters for Duration
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,19 @@ S.BigDecimal.pipe(S.negativeBigDecimal());
S.BigDecimal.pipe(S.nonPositiveBigDecimal());
```

### Duration filters

```ts
import * as S from "@effect/schema/Schema";
import * as Duration from "effect/BigDecimal";

S.Duration.pipe(S.greaterThanDuration("5 seconds"));
S.Duration.pipe(S.greaterThanOrEqualToDuration("5 seconds"));
S.Duration.pipe(S.lessThanDuration("5 seconds"));
S.Duration.pipe(S.lessThanOrEqualToDuration("5 seconds"));
S.Duration.pipe(S.betweenDuration("5 seconds", "10 seconds"));
```

### Array filters

```ts
Expand Down Expand Up @@ -2118,6 +2131,75 @@ parse("0"); // BigDecimal(0)
parse("3"); // BigDecimal(-3)
```

### Duration transformations

#### Duration

Converts an hrtime(i.e. `[seconds: number, nanos: number]`) into a `Duration`.

```ts
import * as S from "@effect/schema/Schema";
import * as Duration from "effect/Duration";

// $ExpectType Schema<number, Duration>
const schema = S.Duration;

const parse = S.parseSync(schema);
parse([0, 0]); // 0 seconds
parse([5000, 0]); // 5 seconds
```

#### DurationFromNumber

Converts a `number` into a `Duration` where the number represents the number of milliseconds.

```ts
import * as S from "@effect/schema/Schema";
import * as Duration from "effect/Duration";

// $ExpectType Schema<number, Duration>
const schema = S.DurationFromNumber;

const parse = S.parseSync(schema);
parse(0); // 0 seconds
parse(5000); // 5 seconds
```

#### DurationFromBigint

Converts a `bigint` into a `Duration` where the number represents the number of nanoseconds.

```ts
import * as S from "@effect/schema/Schema";
import * as Duration from "effect/Duration";

// $ExpectType Schema<bigint, Duration>
const schema = S.DurationFromBigint;

const parse = S.parseSync(schema);
parse(0n); // 0 seconds
parse(5000000000n); // 5 seconds
```
fubhy marked this conversation as resolved.
Show resolved Hide resolved

#### clampDuration

Clamps a `Duration` between a minimum and a maximum value.

```ts
import * as S from "@effect/schema/Schema";
import * as Duration from "effect/Duration";

// $ExpectType Schema<Duration.Duration, Duration.Duration>
const schema = S.DurationFromSelf.pipe(
S.clampDuration("5 seconds", "10 seconds")
);

const parse = S.parseSync(schema);
parse(Duration.decode("2 seconds")); // 5 seconds
parse(Duration.decode("6 seconds")); // 6 seconds
parse(Duration.decode("11 seconds")); // 10 seconds
```

### Symbol transformations

#### symbol
Expand Down
201 changes: 201 additions & 0 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ Added in v1.0.0
- [dateFromString](#datefromstring-1)
- [Duration constructors](#duration-constructors)
- [Duration](#duration)
- [DurationFromMillis](#durationfrommillis)
- [DurationFromNanos](#durationfromnanos)
- [DurationFromSelf](#durationfromself)
- [Duration filters](#duration-filters)
- [betweenDuration](#betweenduration)
- [greaterThanDuration](#greaterthanduration)
- [greaterThanOrEqualToDuration](#greaterthanorequaltoduration)
- [lessThanDuration](#lessthanduration)
- [lessThanOrEqualToDuration](#lessthanorequaltoduration)
- [Duration transformations](#duration-transformations)
- [clampDuration](#clampduration)
- [durationFromHrTime](#durationfromhrtime)
- [durationFromMillis](#durationfrommillis-1)
- [durationFromNanos](#durationfromnanos-1)
- [Either transformations](#either-transformations)
- [EitherFrom (type alias)](#eitherfrom-type-alias)
- [either](#either)
Expand Down Expand Up @@ -270,6 +281,7 @@ Added in v1.0.0
- [BetweenBigDecimalTypeId](#betweenbigdecimaltypeid)
- [BetweenBigintTypeId](#betweenbiginttypeid)
- [BetweenBigintTypeId (type alias)](#betweenbiginttypeid-type-alias)
- [BetweenDurationTypeId](#betweendurationtypeid)
- [BetweenTypeId](#betweentypeid)
- [BetweenTypeId (type alias)](#betweentypeid-type-alias)
- [BrandTypeId](#brandtypeid)
Expand All @@ -278,9 +290,11 @@ Added in v1.0.0
- [GreaterThanBigDecimalTypeId](#greaterthanbigdecimaltypeid)
- [GreaterThanBigintTypeId](#greaterthanbiginttypeid)
- [GreaterThanBigintTypeId (type alias)](#greaterthanbiginttypeid-type-alias)
- [GreaterThanDurationTypeId](#greaterthandurationtypeid)
- [GreaterThanOrEqualToBigDecimalTypeId](#greaterthanorequaltobigdecimaltypeid)
- [GreaterThanOrEqualToBigintTypeId](#greaterthanorequaltobiginttypeid)
- [GreaterThanOrEqualToBigintTypeId (type alias)](#greaterthanorequaltobiginttypeid-type-alias)
- [GreaterThanOrEqualToDurationTypeId](#greaterthanorequaltodurationtypeid)
- [GreaterThanOrEqualToTypeId](#greaterthanorequaltotypeid)
- [GreaterThanOrEqualToTypeId (type alias)](#greaterthanorequaltotypeid-type-alias)
- [GreaterThanTypeId](#greaterthantypeid)
Expand All @@ -297,9 +311,11 @@ Added in v1.0.0
- [LessThanBigDecimalTypeId](#lessthanbigdecimaltypeid)
- [LessThanBigintTypeId](#lessthanbiginttypeid)
- [LessThanBigintTypeId (type alias)](#lessthanbiginttypeid-type-alias)
- [LessThanDurationTypeId](#lessthandurationtypeid)
- [LessThanOrEqualToBigDecimalTypeId](#lessthanorequaltobigdecimaltypeid)
- [LessThanOrEqualToBigintTypeId](#lessthanorequaltobiginttypeid)
- [LessThanOrEqualToBigintTypeId (type alias)](#lessthanorequaltobiginttypeid-type-alias)
- [LessThanOrEqualToDurationTypeId](#lessthanorequaltodurationtypeid)
- [LessThanOrEqualToTypeId](#lessthanorequaltotypeid)
- [LessThanOrEqualToTypeId (type alias)](#lessthanorequaltotypeid-type-alias)
- [LessThanTypeId](#lessthantypeid)
Expand Down Expand Up @@ -710,6 +726,32 @@ export declare const Duration: Schema<readonly [seconds: number, nanos: number],

Added in v1.0.0

## DurationFromMillis

A schema that transforms a `number` tuple into a `Duration`.
Treats the value as the number of milliseconds.

**Signature**

```ts
export declare const DurationFromMillis: Schema<number, Duration.Duration>
```

Added in v1.0.0

## DurationFromNanos

A schema that transforms a `bigint` tuple into a `Duration`.
Treats the value as the number of nanoseconds.

**Signature**

```ts
export declare const DurationFromNanos: Schema<bigint, Duration.Duration>
```

Added in v1.0.0

## DurationFromSelf

**Signature**
Expand All @@ -720,8 +762,91 @@ export declare const DurationFromSelf: Schema<Duration.Duration, Duration.Durati

Added in v1.0.0

# Duration filters

## betweenDuration

**Signature**

```ts
export declare const betweenDuration: <A extends Duration.Duration>(
minimum: Duration.DurationInput,
maximum: Duration.DurationInput,
options?: FilterAnnotations<A> | undefined
) => <I>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

## greaterThanDuration

**Signature**

```ts
export declare const greaterThanDuration: <A extends Duration.Duration>(
min: Duration.DurationInput,
options?: FilterAnnotations<A> | undefined
) => <I>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

## greaterThanOrEqualToDuration

**Signature**

```ts
export declare const greaterThanOrEqualToDuration: <A extends Duration.Duration>(
min: Duration.DurationInput,
options?: FilterAnnotations<A> | undefined
) => <I>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

## lessThanDuration

**Signature**

```ts
export declare const lessThanDuration: <A extends Duration.Duration>(
max: Duration.DurationInput,
options?: FilterAnnotations<A> | undefined
) => <I>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

## lessThanOrEqualToDuration

**Signature**

```ts
export declare const lessThanOrEqualToDuration: <A extends Duration.Duration>(
max: Duration.DurationInput,
options?: FilterAnnotations<A> | undefined
) => <I>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

# Duration transformations

## clampDuration

Clamps a `Duration` between a minimum and a maximum value.

**Signature**

```ts
export declare const clampDuration: (
minimum: Duration.DurationInput,
maximum: Duration.DurationInput
) => <I, A extends Duration.Duration>(self: Schema<I, A>) => Schema<I, A>
```

Added in v1.0.0

## durationFromHrTime

A combinator that transforms a `[number, number]` tuple into a `Duration`.
Expand All @@ -736,6 +861,32 @@ export declare const durationFromHrTime: <I, A extends readonly [seconds: number

Added in v1.0.0

## durationFromMillis

A combinator that transforms a `number` into a `Duration`.
Treats the value as the number of milliseconds.

**Signature**

```ts
export declare const durationFromMillis: <I, A extends number>(self: Schema<I, A>) => Schema<I, Duration.Duration>
```

Added in v1.0.0

## durationFromNanos

A combinator that transforms a `bigint` into a `Duration`.
Treats the value as the number of nanoseconds.

**Signature**

```ts
export declare const durationFromNanos: <I, A extends bigint>(self: Schema<I, A>) => Schema<I, Duration.Duration>
```

Added in v1.0.0

# Either transformations

## EitherFrom (type alias)
Expand Down Expand Up @@ -3249,6 +3400,16 @@ export type BetweenBigintTypeId = typeof BetweenBigintTypeId

Added in v1.0.0

## BetweenDurationTypeId

**Signature**

```ts
export declare const BetweenDurationTypeId: typeof BetweenDurationTypeId
```

Added in v1.0.0

## BetweenTypeId

**Signature**
Expand Down Expand Up @@ -3329,6 +3490,16 @@ export type GreaterThanBigintTypeId = typeof GreaterThanBigintTypeId

Added in v1.0.0

## GreaterThanDurationTypeId

**Signature**

```ts
export declare const GreaterThanDurationTypeId: typeof GreaterThanDurationTypeId
```

Added in v1.0.0

## GreaterThanOrEqualToBigDecimalTypeId

**Signature**
Expand Down Expand Up @@ -3359,6 +3530,16 @@ export type GreaterThanOrEqualToBigintTypeId = typeof GreaterThanOrEqualToBigint

Added in v1.0.0

## GreaterThanOrEqualToDurationTypeId

**Signature**

```ts
export declare const GreaterThanOrEqualToDurationTypeId: typeof GreaterThanOrEqualToDurationTypeId
```

Added in v1.0.0

## GreaterThanOrEqualToTypeId

**Signature**
Expand Down Expand Up @@ -3519,6 +3700,16 @@ export type LessThanBigintTypeId = typeof LessThanBigintTypeId

Added in v1.0.0

## LessThanDurationTypeId

**Signature**

```ts
export declare const LessThanDurationTypeId: typeof LessThanDurationTypeId
```

Added in v1.0.0

## LessThanOrEqualToBigDecimalTypeId

**Signature**
Expand Down Expand Up @@ -3549,6 +3740,16 @@ export type LessThanOrEqualToBigintTypeId = typeof LessThanOrEqualToBigintTypeId

Added in v1.0.0

## LessThanOrEqualToDurationTypeId

**Signature**

```ts
export declare const LessThanOrEqualToDurationTypeId: typeof LessThanOrEqualToDurationTypeId
```

Added in v1.0.0

## LessThanOrEqualToTypeId

**Signature**
Expand Down
Loading
Loading