Skip to content

Commit b97d62a

Browse files
committed
Bug Fix: taggedUnion should handle sub unions / tagged unions correctly, closes #257
Experimental: optimize union with the same algorithm used in taggedUnion Also removed the following useless internal APIs - isTagged - findTagged - getTagValue
1 parent dc5ccc4 commit b97d62a

File tree

8 files changed

+408
-175
lines changed

8 files changed

+408
-175
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
1515
high state of flux, you're at risk of it changing without notice.
1616

17+
# 1.6.1
18+
19+
- **Bug Fix**
20+
- `taggedUnion` should handle sub unions / tagged unions correctly, closes #257 (@gcanti)
21+
- **Experimental**
22+
- optimize `union` with the same algorithm used in `taggedUnion` (@gcanti)
23+
1724
# 1.6.0
1825

1926
**Important**. This version requires `[email protected]+`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io-ts",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "TypeScript compatible runtime type system for IO validation",
55
"files": [
66
"lib"

perf/getIndexRecord.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as Benchmark from 'benchmark'
2+
import * as t from '../src'
3+
4+
const suite = new Benchmark.Suite()
5+
6+
const A = t.intersection([
7+
t.type({
8+
type: t.literal('A')
9+
}),
10+
t.type({
11+
bar: t.number
12+
})
13+
])
14+
const U1 = t.union([A, t.undefined])
15+
const U2 = t.union([t.undefined, A])
16+
17+
suite
18+
.add('getIndexRecord (U1)', function() {
19+
t.getIndexRecord(U1.types)
20+
})
21+
.add('getIndexRecord (U2)', function() {
22+
t.getIndexRecord(U2.types)
23+
})
24+
.on('cycle', function(event: any) {
25+
console.log(String(event.target))
26+
})
27+
.on('complete', function(this: any) {
28+
console.log('Fastest is ' + this.filter('fastest').map('name'))
29+
})
30+
.run({ async: true })

perf/taggedUnion.js renamed to perf/taggedUnion.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var Benchmark = require('benchmark')
2-
var t = require('../lib/index')
1+
import * as Benchmark from 'benchmark'
2+
import * as t from '../src'
33

44
const suite = new Benchmark.Suite()
55

@@ -23,9 +23,9 @@ const TUB = t.intersection(
2323
'TUB'
2424
)
2525

26-
const DateFromNumber = new t.Type(
26+
const DateFromNumber = new t.Type<Date, number, unknown>(
2727
'DateFromNumber',
28-
v => v instanceof Date,
28+
(u): u is Date => u instanceof Date,
2929
(s, c) =>
3030
t.number.validate(s, c).chain(n => {
3131
const d = new Date(n)
@@ -51,10 +51,10 @@ suite
5151
.add('taggedUnion (invalid)', function() {
5252
T.decode({ type: 'D' })
5353
})
54-
.on('cycle', function(event) {
54+
.on('cycle', function(event: any) {
5555
console.log(String(event.target))
5656
})
57-
.on('complete', function() {
57+
.on('complete', function(this: any) {
5858
console.log('Fastest is ' + this.filter('fastest').map('name'))
5959
})
6060
.run({ async: true })

perf/union.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as Benchmark from 'benchmark'
2+
import * as t from '../src'
3+
4+
const suite = new Benchmark.Suite()
5+
6+
const makeType = <L extends string>(L: L) =>
7+
t.type(
8+
{
9+
type: t.literal(L),
10+
a: t.string
11+
},
12+
L
13+
)
14+
15+
const T = t.union(['A', 'B', 'C', 'D', 'E'].map(makeType) as any)
16+
17+
const valid = { type: 'E', a: 'a' }
18+
const invalid = { type: 'Z' }
19+
20+
suite
21+
.add('union (decode) (valid)', function() {
22+
T.decode(valid)
23+
})
24+
.add('union (decode) (invalid)', function() {
25+
T.decode(invalid)
26+
})
27+
.add('union (encode) (valid)', function() {
28+
T.encode(valid)
29+
})
30+
.add('union (create)', function() {
31+
t.union(['A', 'B', 'C', 'D', 'E'].map(makeType) as any)
32+
})
33+
.on('cycle', function(event: any) {
34+
console.log(String(event.target))
35+
})
36+
.on('complete', function(this: any) {
37+
console.log('Fastest is ' + this.filter('fastest').map('name'))
38+
})
39+
.run({ async: true })

0 commit comments

Comments
 (0)