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

Commit

Permalink
feat: added Duration filters (#604)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Lorenz <[email protected]>
  • Loading branch information
jessekelly881 and fubhy authored Dec 11, 2023
1 parent bd6405e commit 88f61cc
Show file tree
Hide file tree
Showing 14 changed files with 795 additions and 14 deletions.
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 @@ -1118,6 +1118,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 @@ -2290,6 +2303,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
```

#### 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 @@ -52,9 +52,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 @@ -283,6 +294,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 @@ -291,9 +303,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 @@ -310,9 +324,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 @@ -787,6 +803,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 @@ -797,8 +839,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 @@ -813,6 +938,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 @@ -3446,6 +3597,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 @@ -3526,6 +3687,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 @@ -3556,6 +3727,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 @@ -3716,6 +3897,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 @@ -3746,6 +3937,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

0 comments on commit 88f61cc

Please sign in to comment.