Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support readonly and/or non-empty arrays #627

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions docs/modules/Codec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ Added in v2.2.3
- [intersect](#intersect)
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nonEmptyArray](#nonemptyarray)
- [nullable](#nullable)
- [partial](#partial)
- [readonly](#readonly)
- [readonlyArray](#readonlyarray)
- [readonlyNonEmptyArray](#readonlynonemptyarray)
- [record](#record)
- [refine](#refine)
- [struct](#struct)
Expand Down Expand Up @@ -216,6 +219,18 @@ export declare const mapLeftWithInput: <I>(

Added in v2.2.3

## nonEmptyArray

**Signature**

```ts
export declare function nonEmptyArray<O, A>(
item: Codec<unknown, O, A>
): Codec<unknown, NonEmptyArray<O>, NonEmptyArray<A>>
```

Added in v2.2.17

## nullable

**Signature**
Expand Down Expand Up @@ -248,6 +263,30 @@ export declare const readonly: <I, O, A>(codec: Codec<I, O, A>) => Codec<I, O, R

Added in v2.2.16

## readonlyArray

**Signature**

```ts
export declare function readonlyArray<O, A>(
item: Codec<unknown, O, A>
): Codec<unknown, ReadonlyArray<O>, ReadonlyArray<A>>
```

Added in v2.2.17

## readonlyNonEmptyArray

**Signature**

```ts
export declare function readonlyNonEmptyArray<O, A>(
item: Codec<unknown, O, A>
): Codec<unknown, ReadonlyNonEmptyArray<O>, ReadonlyNonEmptyArray<A>>
```

Added in v2.2.17

## record

**Signature**
Expand Down
33 changes: 33 additions & 0 deletions docs/modules/Decoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ Added in v2.2.7
- [intersect](#intersect)
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nonEmptyArray](#nonemptyarray)
- [nullable](#nullable)
- [parse](#parse)
- [partial](#partial)
- [readonly](#readonly)
- [readonlyArray](#readonlyarray)
- [readonlyNonEmptyArray](#readonlynonemptyarray)
- [record](#record)
- [refine](#refine)
- [struct](#struct)
Expand Down Expand Up @@ -297,6 +300,16 @@ export declare const mapLeftWithInput: <I>(

Added in v2.2.7

## nonEmptyArray

**Signature**

```ts
export declare const nonEmptyArray: <A>(item: Decoder<unknown, A>) => Decoder<unknown, NonEmptyArray<A>>
```

Added in v2.2.17

## nullable

**Signature**
Expand Down Expand Up @@ -341,6 +354,26 @@ export declare const readonly: <I, A>(decoder: Decoder<I, A>) => Decoder<I, Read

Added in v2.2.15

## readonlyArray

**Signature**

```ts
export declare const readonlyArray: <A>(item: Decoder<unknown, A>) => Decoder<unknown, readonly A[]>
```

Added in v2.2.17

## readonlyNonEmptyArray

**Signature**

```ts
export declare const readonlyNonEmptyArray: <A>(item: Decoder<unknown, A>) => Decoder<unknown, ReadonlyNonEmptyArray<A>>
```

Added in v2.2.17

## record

**Signature**
Expand Down
35 changes: 35 additions & 0 deletions docs/modules/Encoder.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ Added in v2.2.3
- [array](#array)
- [intersect](#intersect)
- [lazy](#lazy)
- [nonEmptyArray](#nonemptyarray)
- [nullable](#nullable)
- [partial](#partial)
- [readonly](#readonly)
- [readonlyArray](#readonlyarray)
- [readonlyNonEmptyArray](#readonlynonemptyarray)
- [record](#record)
- [struct](#struct)
- [sum](#sum)
Expand Down Expand Up @@ -118,6 +121,16 @@ export declare function lazy<O, A>(f: () => Encoder<O, A>): Encoder<O, A>

Added in v2.2.3

## nonEmptyArray

**Signature**

```ts
export declare const nonEmptyArray: <O, A>(item: Encoder<O, A>) => Encoder<NonEmptyArray<O>, NonEmptyArray<A>>
```

Added in v2.2.17

## nullable

**Signature**
Expand Down Expand Up @@ -150,6 +163,28 @@ export declare const readonly: <O, A>(decoder: Encoder<O, A>) => Encoder<O, Read

Added in v2.2.16

## readonlyArray

**Signature**

```ts
export declare const readonlyArray: <O, A>(item: Encoder<O, A>) => Encoder<readonly O[], readonly A[]>
```

Added in v2.2.17

## readonlyNonEmptyArray

**Signature**

```ts
export declare const readonlyNonEmptyArray: <O, A>(
item: Encoder<O, A>
) => Encoder<ReadonlyNonEmptyArray<O>, ReadonlyNonEmptyArray<A>>
```

Added in v2.2.17

## record

**Signature**
Expand Down
21 changes: 21 additions & 0 deletions dtslint/ts3.5/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ _.fromArray(NumberFromString)
// $ExpectType Codec<unknown, string[], string[]>
_.array(_.string)

//
// readonlyArray
//

// $ExpectType Codec<unknown, ReadonlyArray<string>, ReadonlyArray<string>>
_.readonlyArray(_.string)

//
// nonEmptyArray
//

// $ExpectType Codec<unknown, NonEmptyArray<string>, NonEmptyArray<string>>
_.nonEmptyArray(_.string)

//
// readonlyNonEmptyArray
//

// $ExpectType Codec<unknown, ReadonlyNonEmptyArray<string>, ReadonlyNonEmptyArray<string>>
_.readonlyNonEmptyArray(_.string)

//
// fromRecord
//
Expand Down
21 changes: 21 additions & 0 deletions dtslint/ts3.5/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ _.fromArray(NumberFromString)
// $ExpectType Decoder<unknown, string[]>
_.array(_.string)

//
// readonlyArray
//

// $ExpectType Decoder<unknown, ReadonlyArray<string>>
_.readonlyArray(_.string)

//
// nonEmptyArray
//

// $ExpectType Decoder<unknown, NonEmptyArray<string>>
_.nonEmptyArray(_.string)

//
// readonlyNonEmptyArray
//

// $ExpectType Decoder<unknown, ReadonlyNonEmptyArray<string>>
_.readonlyNonEmptyArray(_.string)

//
// fromRecord
//
Expand Down
15 changes: 15 additions & 0 deletions dtslint/ts3.5/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ E.record(NumberToString) // $ExpectType Encoder<Record<string, string>, Record<s
//
E.array(NumberToString) // $ExpectType Encoder<string[], number[]>

//
// readonlyArray
//
E.readonlyArray(NumberToString) // $ExpectType Encoder<ReadonlyArray<string>, ReadonlyArray<number>>

//
// nonEmptyArray
//
E.nonEmptyArray(NumberToString) // $ExpectType Encoder<NonEmptyArray<string>, NonEmptyArray<number>>

//
// readonlyNonEmptyArray
//
E.readonlyNonEmptyArray(NumberToString) // $ExpectType Encoder<ReadonlyNonEmptyArray<string>, ReadonlyNonEmptyArray<number>>

//
// tuple
//
Expand Down
28 changes: 28 additions & 0 deletions src/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
import { identity, Refinement } from 'fp-ts/lib/function'
import { Invariant3 } from 'fp-ts/lib/Invariant'
import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
import { pipe } from 'fp-ts/lib/pipeable'
import * as D from './Decoder'
import * as E from './Encoder'
Expand Down Expand Up @@ -220,6 +222,32 @@ export function array<O, A>(item: Codec<unknown, O, A>): Codec<unknown, Array<O>
return pipe(UnknownArray, compose(fromArray(item))) as any
}

/**
* @category combinators
* @since 2.2.17
*/
export function readonlyArray<O, A>(item: Codec<unknown, O, A>): Codec<unknown, ReadonlyArray<O>, ReadonlyArray<A>> {
return make(D.readonlyArray(item), E.readonlyArray(item))
}

/**
* @category combinators
* @since 2.2.17
*/
export function nonEmptyArray<O, A>(item: Codec<unknown, O, A>): Codec<unknown, NonEmptyArray<O>, NonEmptyArray<A>> {
return make(D.nonEmptyArray(item), E.nonEmptyArray(item))
}

/**
* @category combinators
* @since 2.2.17
*/
export function readonlyNonEmptyArray<O, A>(
item: Codec<unknown, O, A>
): Codec<unknown, ReadonlyNonEmptyArray<O>, ReadonlyNonEmptyArray<A>> {
return make(D.readonlyNonEmptyArray(item), E.readonlyNonEmptyArray(item))
}

/**
* @category combinators
* @since 2.2.3
Expand Down
25 changes: 25 additions & 0 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* @since 2.2.7
*/
import { Alt2, Alt2C } from 'fp-ts/lib/Alt'
import * as A from 'fp-ts/lib/Array'
import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import * as RA from 'fp-ts/lib/ReadonlyArray'
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
import { Bifunctor2 } from 'fp-ts/lib/Bifunctor'
import { Category2 } from 'fp-ts/lib/Category'
import * as E from 'fp-ts/lib/Either'
Expand Down Expand Up @@ -292,6 +296,27 @@ export const fromArray = <I, A>(item: Decoder<I, A>): Decoder<Array<I>, Array<A>
export const array = <A>(item: Decoder<unknown, A>): Decoder<unknown, Array<A>> =>
pipe(UnknownArray, compose(fromArray(item)))

/**
* @category combinators
* @since 2.2.17
*/
export const readonlyArray = <A>(item: Decoder<unknown, A>): Decoder<unknown, ReadonlyArray<A>> =>
pipe(array(item), readonly)

/**
* @category combinators
* @since 2.2.17
*/
export const nonEmptyArray = <A>(item: Decoder<unknown, A>): Decoder<unknown, NonEmptyArray<A>> =>
pipe(array(item), refine(A.isNonEmpty, 'NonEmptyArray<unknown>'))

/**
* @category combinators
* @since 2.2.17
*/
export const readonlyNonEmptyArray = <A>(item: Decoder<unknown, A>): Decoder<unknown, ReadonlyNonEmptyArray<A>> =>
pipe(readonlyArray(item), refine(RA.isNonEmpty, 'ReadonlyNonEmptyArray<unknown>'))

/**
* @category combinators
* @since 2.2.8
Expand Down
22 changes: 22 additions & 0 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
import { Contravariant2 } from 'fp-ts/lib/Contravariant'
import { Category2 } from 'fp-ts/lib/Category'
import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
import { memoize, intersect_ } from './Schemable'
import { identity } from 'fp-ts/lib/function'

Expand Down Expand Up @@ -115,6 +117,26 @@ export function array<O, A>(item: Encoder<O, A>): Encoder<Array<O>, Array<A>> {
}
}

/**
* @category combinators
* @since 2.2.17
*/
export const readonlyArray: <O, A>(item: Encoder<O, A>) => Encoder<ReadonlyArray<O>, ReadonlyArray<A>> = array as any

/**
* @category combinators
* @since 2.2.17
*/
export const nonEmptyArray: <O, A>(item: Encoder<O, A>) => Encoder<NonEmptyArray<O>, NonEmptyArray<A>> = array as any

/**
* @category combinators
* @since 2.2.17
*/
export const readonlyNonEmptyArray: <O, A>(
item: Encoder<O, A>
) => Encoder<ReadonlyNonEmptyArray<O>, ReadonlyNonEmptyArray<A>> = readonlyArray as any

/**
* @category combinators
* @since 2.2.3
Expand Down
Loading