Skip to content

Commit

Permalink
feat add optional type
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeiscontent authored and Aaron Reisman committed Dec 17, 2022
1 parent 9103e88 commit d4424ab
Show file tree
Hide file tree
Showing 32 changed files with 349 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Combinators](#combinators)
- [The `literal` constructor](#the-literal-constructor)
- [The `nullable` combinator](#the-nullable-combinator)
- [The `optional` combinator](#the-optional-combinator)
- [The `struct` combinator](#the-struct-combinator)
- [The `partial` combinator](#the-partial-combinator)
- [The `record` combinator](#the-record-combinator)
Expand Down Expand Up @@ -100,6 +101,14 @@ The `nullable` combinator describes a nullable value
export const NullableString: D.Decoder<unknown, null | string> = D.nullable(D.string)
```

## The `nullable` combinator

The `optional` combinator describes a optional value

```ts
export const OptionalString: D.Decoder<unknown, undefined | string> = D.optional(D.string)
```

## The `struct` combinator

The `struct` combinator describes an object with required fields.
Expand Down
1 change: 1 addition & 0 deletions Eq.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const string: Eq<string> = {

- `literal`
- `nullable`
- `optional`
- `type`
- `partial`
- `record`
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Codec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Added in v2.2.3
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
- [optional](#optional)
- [partial](#partial)
- [readonly](#readonly)
- [record](#record)
Expand Down Expand Up @@ -226,6 +227,16 @@ export declare function nullable<I, O, A>(or: Codec<I, O, A>): Codec<null | I, n

Added in v2.2.3

## optional

**Signature**

```ts
export declare function optional<I, O, A>(or: Codec<I, O, A>): Codec<undefined | I, undefined | O, undefined | A>
```

Added in v2.3.0

## partial

**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Decoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Added in v2.2.7
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
- [optional](#optional)
- [parse](#parse)
- [partial](#partial)
- [readonly](#readonly)
Expand Down Expand Up @@ -307,6 +308,16 @@ export declare const nullable: <I, A>(or: Decoder<I, A>) => Decoder<I, A>
Added in v2.2.7
## optional
**Signature**
```ts
export declare const optional: <I, A>(or: Decoder<I, A>) => Decoder<I, A>
```
Added in v2.3.0
## parse
**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Encoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Added in v2.2.3
- [intersect](#intersect)
- [lazy](#lazy)
- [nullable](#nullable)
- [optional](#optional)
- [partial](#partial)
- [readonly](#readonly)
- [record](#record)
Expand Down Expand Up @@ -128,6 +129,16 @@ export declare function nullable<O, A>(or: Encoder<O, A>): Encoder<null | O, nul

Added in v2.2.3

## optional

**Signature**

```ts
export declare function optional<O, A>(or: Encoder<O, A>): Encoder<undefined | O, undefined | A>
```

Added in v2.3.0

## partial

**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Eq.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Added in v2.2.2
- [intersect](#intersect)
- [lazy](#lazy)
- [nullable](#nullable)
- [optional](#optional)
- [partial](#partial)
- [readonly](#readonly)
- [record](#record)
Expand Down Expand Up @@ -89,6 +90,16 @@ export declare function nullable<A>(or: Eq<A>): Eq<null | A>

Added in v2.2.2

## optional

**Signature**

```ts
export declare function optional<A>(or: Eq<A>): Eq<undefined | A>
```

Added in v2.3.0

## partial

**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Guard.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Added in v2.2.0
- [intersect](#intersect)
- [lazy](#lazy)
- [nullable](#nullable)
- [optional](#optional)
- [partial](#partial)
- [readonly](#readonly)
- [record](#record)
Expand Down Expand Up @@ -132,6 +133,16 @@ export declare const nullable: <I, A extends I>(or: Guard<I, A>) => Guard<I, A>
Added in v2.2.0
## optional
**Signature**
```ts
export declare const optional: <I, A extends I>(or: Guard<I, A>) => Guard<I, A>
```
Added in v2.3.0
## partial
**Signature**
Expand Down
13 changes: 13 additions & 0 deletions docs/modules/Kleisli.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Added in v2.2.7
- [map](#map)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
- [optional](#optional)
- [parse](#parse)
- [refine](#refine)
- [union](#union)
Expand Down Expand Up @@ -237,6 +238,18 @@ export declare function nullable<M extends URIS2, E>(

Added in v2.2.7

## optional

**Signature**

```ts
export declare function optional<M extends URIS2, E>(
M: Applicative2C<M, E> & Bifunctor2<M>
): <I>(onError: (i: I, e: E) => E) => <A>(or: Kleisli<M, I, E, A>) => Kleisli<M, undefined | I, E, undefined | A>
```

Added in v2.3.0

## parse

**Signature**
Expand Down
3 changes: 3 additions & 0 deletions docs/modules/Schemable.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface Schemable<S> {
readonly number: HKT<S, number>
readonly boolean: HKT<S, boolean>
readonly nullable: <A>(or: HKT<S, A>) => HKT<S, null | A>
readonly optional: <A>(or: HKT<S, A>) => HKT<S, undefined | A>
/** @deprecated */
readonly type: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, { [K in keyof A]: A[K] }>
readonly struct: <A>(properties: { [K in keyof A]: HKT<S, A[K]> }) => HKT<S, { [K in keyof A]: A[K] }>
Expand Down Expand Up @@ -95,6 +96,7 @@ export interface Schemable1<S extends URIS> {
readonly number: Kind<S, number>
readonly boolean: Kind<S, boolean>
readonly nullable: <A>(or: Kind<S, A>) => Kind<S, null | A>
readonly optional: <A>(or: Kind<S, A>) => Kind<S, undefined | A>
/** @deprecated */
readonly type: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, { [K in keyof A]: A[K] }>
readonly struct: <A>(properties: { [K in keyof A]: Kind<S, A[K]> }) => Kind<S, { [K in keyof A]: A[K] }>
Expand Down Expand Up @@ -127,6 +129,7 @@ export interface Schemable2C<S extends URIS2, E> {
readonly number: Kind2<S, E, number>
readonly boolean: Kind2<S, E, boolean>
readonly nullable: <A>(or: Kind2<S, E, A>) => Kind2<S, E, null | A>
readonly optional: <A>(or: Kind2<S, E, A>) => Kind2<S, E, undefined | A>
/** @deprecated */
readonly type: <A>(properties: { [K in keyof A]: Kind2<S, E, A[K]> }) => Kind2<S, E, { [K in keyof A]: A[K] }>
readonly struct: <A>(properties: { [K in keyof A]: Kind2<S, E, A[K]> }) => Kind2<S, E, { [K in keyof A]: A[K] }>
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/TaskDecoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Added in v2.2.7
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
- [optional](#optional)
- [parse](#parse)
- [partial](#partial)
- [readonly](#readonly)
Expand Down Expand Up @@ -310,6 +311,16 @@ export declare const nullable: <I, A>(or: TaskDecoder<I, A>) => TaskDecoder<I, A
Added in v2.2.7
## optional
**Signature**
```ts
export declare const optional: <I, A>(or: TaskDecoder<I, A>) => TaskDecoder<I, A>
```
Added in v2.3.0
## parse
**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Type.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Added in v2.2.3
- [intersect](#intersect)
- [lazy](#lazy)
- [nullable](#nullable)
- [optional](#optional)
- [partial](#partial)
- [readonly](#readonly)
- [record](#record)
Expand Down Expand Up @@ -95,6 +96,16 @@ export declare const nullable: <A>(or: Type<A>) => Type<A>
Added in v2.2.3
## optional
**Signature**
```ts
export declare const optional: <A>(or: Type<A>) => Type<A>
```
Added in v2.3.0
## partial
**Signature**
Expand Down
5 changes: 5 additions & 0 deletions dtslint/ts3.5/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export type OfTestOutput = E.OutputOf<typeof OfTest> // $ExpectType { a: string;
//
E.nullable(NumberToString) // $ExpectType Encoder<string | null, number | null>

//
// optional
//
E.optional(NumberToString) // $ExpectType Encoder<string | undefined, number | undefined>

//
// struct
//
Expand Down
6 changes: 6 additions & 0 deletions dtslint/ts3.5/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ make((S) => S.boolean) // $ExpectType Schema<boolean>

make((S) => S.nullable(S.string)) // $ExpectType Schema<string | null>

//
// optional
//

make((S) => S.optional(S.string)) // $ExpectType Schema<string | undefined>

//
// struct
//
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export function nullable<I, O, A>(or: Codec<I, O, A>): Codec<null | I, null | O,
return make(D.nullable(or), E.nullable(or))
}

/**
* @category combinators
* @since 2.3.0
*/
export function optional<I, O, A>(or: Codec<I, O, A>): Codec<undefined | I, undefined | O, undefined | A> {
return make(D.optional(or), E.optional(or))
}

/**
* @category combinators
* @since 2.2.15
Expand Down
9 changes: 9 additions & 0 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ export const nullable: <I, A>(or: Decoder<I, A>) => Decoder<null | I, null | A>
/*#__PURE__*/
K.nullable(M)((u, e) => FS.concat(FS.of(DE.member(0, error(u, 'null'))), FS.of(DE.member(1, e))))

/**
* @category combinators
* @since 2.3.0
*/
export const optional: <I, A>(or: Decoder<I, A>) => Decoder<undefined | I, undefined | A> =
/*#__PURE__*/
K.optional(M)((u, e) => FS.concat(FS.of(DE.member(0, error(u, 'undefined'))), FS.of(DE.member(1, e))))

/**
* @category combinators
* @since 2.2.15
Expand Down Expand Up @@ -488,6 +496,7 @@ export const Schemable: S.Schemable2C<URI, unknown> = {
number,
boolean,
nullable,
optional,
type,
struct,
partial,
Expand Down
10 changes: 10 additions & 0 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export function nullable<O, A>(or: Encoder<O, A>): Encoder<null | O, null | A> {
}
}

/**
* @category combinators
* @since 2.3.0
*/
export function optional<O, A>(or: Encoder<O, A>): Encoder<undefined | O, undefined | A> {
return {
encode: (a) => (a === undefined ? undefined : or.encode(a))
}
}

/**
* @category combinators
* @since 2.2.15
Expand Down
11 changes: 11 additions & 0 deletions src/Eq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ export function nullable<A>(or: Eq<A>): Eq<null | A> {
}
}

/**
* @category combinators
* @since 2.3.0
*/
export function optional<A>(or: Eq<A>): Eq<undefined | A> {
return {
equals: (x, y) => (x === undefined || y === undefined ? x === y : or.equals(x, y))
}
}

/**
* @category combinators
* @since 2.2.15
Expand Down Expand Up @@ -204,6 +214,7 @@ export const Schemable: Schemable1<E.URI> = {
number,
boolean,
nullable,
optional,
type,
struct,
partial,
Expand Down
Loading

0 comments on commit d4424ab

Please sign in to comment.