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

Add withLazyFallback type #105

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
38 changes: 38 additions & 0 deletions docs/modules/withLazyFallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: withLazyFallback.ts
nav_order: 22
parent: Modules
---

---

<h2 class="text-delta">Table of contents</h2>

- [withLazyFallback (function)](#withlazyfallback-function)

---

# withLazyFallback (function)

Returns a clone of the given codec that always succeed using the given value `a` if the original codec fails

**Signature**

```ts
export function withLazyFallback<C extends t.Any>(codec: C, a: () => t.TypeOf<C>, name = `withLazyFallback(${codec.name})`): C { ... }
```

**Example**

```ts
import { withLazyFallback } from 'io-ts-types/lib/withLazyFallback';
import * as t from 'io-ts';
import { right } from 'fp-ts/lib/Either';

const T = withLazyFallback(t.number, () => -1);

assert.deepStrictEqual(T.decode(1), right(1));
assert.deepStrictEqual(T.decode('a'), right(-1));
```

Added in v0.5.0
42 changes: 42 additions & 0 deletions docs/modules/withLazyFallback.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: withLazyFallback.ts
nav_order: 23
parent: Modules
---

---

<h2 class="text-delta">Table of contents</h2>

- [withLazyFallback (function)](#withlazyfallback-function)

---

# withLazyFallback (function)

Returns a clone of the given codec that always succeed using the given value `a` if the original codec fails

**Signature**

```ts
export function withLazyFallback<C extends t.Any>(
codec: C,
a: () => t.TypeOf<C>,
name = `withLazyFallback(${codec.name})`
): C { ... }
```

**Example**

```ts
import { withLazyFallback } from 'io-ts-types/lib/withLazyFallback'
import * as t from 'io-ts'
import { right } from 'fp-ts/lib/Either'

const T = withLazyFallback(t.number, () => -1)

assert.deepStrictEqual(T.decode(1), right(1))
assert.deepStrictEqual(T.decode('a'), right(-1))
```

Added in v0.5.0
2 changes: 1 addition & 1 deletion docs/modules/withMessage.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withMessage.ts
nav_order: 23
nav_order: 24
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/withValidate.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withValidate.ts
nav_order: 24
nav_order: 25
parent: Modules
---

Expand Down
26 changes: 26 additions & 0 deletions src/withLazyFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as t from 'io-ts'
import { withValidate } from './withValidate'
import { orElse } from 'fp-ts/lib/Either'

/**
* Returns a clone of the given codec that always succeed using the given value `a` if the original codec fails
*
* @example
* import { withLazyFallback } from 'io-ts-types/lib/withLazyFallback'
* import * as t from 'io-ts'
* import { right } from 'fp-ts/lib/Either'
*
* const T = withLazyFallback(t.number, () => -1)
*
* assert.deepStrictEqual(T.decode(1), right(1))
* assert.deepStrictEqual(T.decode('a'), right(-1))
*
* @since 0.5.0
*/
export function withLazyFallback<C extends t.Any>(
codec: C,
a: () => t.TypeOf<C>,
name = `withLazyFallback(${codec.name})`
): C {
return withValidate(codec, (u, c) => orElse(() => t.success(a()))(codec.validate(u, c)), name)
}
40 changes: 40 additions & 0 deletions test/withLazyFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as assert from 'assert'
import * as t from 'io-ts'
import { withLazyFallback } from '../src/withLazyFallback'
import { assertSuccess } from './helpers'

describe('withLazyFallback', () => {
const zero: t.Int = 0 as any
const one: t.Int = 1 as any

const zeroFn: () => t.Int = () => 0 as any

describe('name', () => {
it('should assign a default name', () => {
const T = withLazyFallback(t.Int, zeroFn)
assert.strictEqual(T.name, 'withLazyFallback(Int)')
})

it('should accept a name', () => {
const T = withLazyFallback(t.Int, zeroFn, 'T')
assert.strictEqual(T.name, 'T')
})
})

describe('is', () => {
it('should check a isomorphic value', () => {
const T = withLazyFallback(t.Int, zeroFn)
assert.strictEqual(T.is(1), true)
assert.strictEqual(T.is(1.1), false)
assert.strictEqual(T.is('a'), false)
})
})

describe('decode', () => {
it('should decode a isomorphic value', () => {
const T = withLazyFallback(t.Int, zeroFn)
assertSuccess(T.decode(1), one)
assertSuccess(T.decode(1.2), zero)
})
})
})