Skip to content

Commit

Permalink
Bug Fix: taggedUnion should handle sub unions / tagged unions correct…
Browse files Browse the repository at this point in the history
…ly, closes #257

Experimental: optimize union with the same algorithm used in taggedUnion

Also removed the following useless internal APIs

- isTagged
- findTagged
- getTagValue
  • Loading branch information
gcanti committed Jan 15, 2019
1 parent dc5ccc4 commit b97d62a
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 175 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 1.6.1

- **Bug Fix**
- `taggedUnion` should handle sub unions / tagged unions correctly, closes #257 (@gcanti)
- **Experimental**
- optimize `union` with the same algorithm used in `taggedUnion` (@gcanti)

# 1.6.0

**Important**. This version requires `[email protected]+`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "1.6.0",
"version": "1.6.1",
"description": "TypeScript compatible runtime type system for IO validation",
"files": [
"lib"
Expand Down
30 changes: 30 additions & 0 deletions perf/getIndexRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as Benchmark from 'benchmark'
import * as t from '../src'

const suite = new Benchmark.Suite()

const A = t.intersection([
t.type({
type: t.literal('A')
}),
t.type({
bar: t.number
})
])
const U1 = t.union([A, t.undefined])
const U2 = t.union([t.undefined, A])

suite
.add('getIndexRecord (U1)', function() {
t.getIndexRecord(U1.types)
})
.add('getIndexRecord (U2)', function() {
t.getIndexRecord(U2.types)
})
.on('cycle', function(event: any) {
console.log(String(event.target))
})
.on('complete', function(this: any) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
12 changes: 6 additions & 6 deletions perf/taggedUnion.js → perf/taggedUnion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Benchmark = require('benchmark')
var t = require('../lib/index')
import * as Benchmark from 'benchmark'
import * as t from '../src'

const suite = new Benchmark.Suite()

Expand All @@ -23,9 +23,9 @@ const TUB = t.intersection(
'TUB'
)

const DateFromNumber = new t.Type(
const DateFromNumber = new t.Type<Date, number, unknown>(
'DateFromNumber',
v => v instanceof Date,
(u): u is Date => u instanceof Date,
(s, c) =>
t.number.validate(s, c).chain(n => {
const d = new Date(n)
Expand All @@ -51,10 +51,10 @@ suite
.add('taggedUnion (invalid)', function() {
T.decode({ type: 'D' })
})
.on('cycle', function(event) {
.on('cycle', function(event: any) {
console.log(String(event.target))
})
.on('complete', function() {
.on('complete', function(this: any) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
39 changes: 39 additions & 0 deletions perf/union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as Benchmark from 'benchmark'
import * as t from '../src'

const suite = new Benchmark.Suite()

const makeType = <L extends string>(L: L) =>
t.type(
{
type: t.literal(L),
a: t.string
},
L
)

const T = t.union(['A', 'B', 'C', 'D', 'E'].map(makeType) as any)

const valid = { type: 'E', a: 'a' }
const invalid = { type: 'Z' }

suite
.add('union (decode) (valid)', function() {
T.decode(valid)
})
.add('union (decode) (invalid)', function() {
T.decode(invalid)
})
.add('union (encode) (valid)', function() {
T.encode(valid)
})
.add('union (create)', function() {
t.union(['A', 'B', 'C', 'D', 'E'].map(makeType) as any)
})
.on('cycle', function(event: any) {
console.log(String(event.target))
})
.on('complete', function(this: any) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
Loading

0 comments on commit b97d62a

Please sign in to comment.