discriminatedUnion.js
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var benchmark_1 = __importDefault(require("benchmark"));
var index_1 = require("../index");
var doubleSuite = new benchmark_1.default.Suite("z.discriminatedUnion: double");
var manySuite = new benchmark_1.default.Suite("z.discriminatedUnion: many");
var aSchema = index_1.z.object({
type: index_1.z.literal("a"),
});
var objA = {
type: "a",
};
var bSchema = index_1.z.object({
type: index_1.z.literal("b"),
});
var objB = {
type: "b",
};
var cSchema = index_1.z.object({
type: index_1.z.literal("c"),
});
var objC = {
type: "c",
};
var dSchema = index_1.z.object({
type: index_1.z.literal("d"),
});
var double = index_1.z.discriminatedUnion("type", [aSchema, bSchema]);
var many = index_1.z.discriminatedUnion("type", [aSchema, bSchema, cSchema, dSchema]);
doubleSuite
.add("valid: a", function () {
double.parse(objA);
})
.add("valid: b", function () {
double.parse(objB);
})
.add("invalid: null", function () {
try {
double.parse(null);
}
catch (err) { }
})
.add("invalid: wrong shape", function () {
try {
double.parse(objC);
}
catch (err) { }
})
.on("cycle", function (e) {
console.log("".concat(doubleSuite.name, ": ").concat(e.target));
});
manySuite
.add("valid: a", function () {
many.parse(objA);
})
.add("valid: c", function () {
many.parse(objC);
})
.add("invalid: null", function () {
try {
many.parse(null);
}
catch (err) { }
})
.add("invalid: wrong shape", function () {
try {
many.parse({ type: "unknown" });
}
catch (err) { }
})
.on("cycle", function (e) {
console.log("".concat(manySuite.name, ": ").concat(e.target));
});
exports.default = {
suites: [doubleSuite, manySuite],
};