-
Notifications
You must be signed in to change notification settings - Fork 330
/
helpers.ts
111 lines (103 loc) · 2.72 KB
/
helpers.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as assert from 'assert'
import { right, either, fold } from 'fp-ts/lib/Either'
import * as t from '../../src/index'
import { PathReporter } from '../../src/PathReporter'
import { pipe } from 'fp-ts/lib/pipeable'
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function assertStrictEqual<T>(result: t.Validation<T>, expected: any): void {
pipe(
result,
fold(
/* istanbul ignore next */
() => {
throw new Error(`${result} is not a right`)
},
(a) => {
assert.deepStrictEqual(a, expected)
}
)
)
}
export function assertSuccess<T>(result: t.Validation<T>, expected?: T): void {
pipe(
result,
fold(
/* istanbul ignore next */
() => {
throw new Error(`${result} is not a right`)
},
(a) => {
if (expected !== undefined) {
assert.deepStrictEqual(a, expected)
}
}
)
)
}
export function assertStrictSuccess<T>(result: t.Validation<T>, expected: T): void {
pipe(
result,
fold(
/* istanbul ignore next */
() => {
throw new Error(`${result} is not a right`)
},
(a) => {
/* istanbul ignore next */
if (expected !== undefined) {
assert.strictEqual(a, expected)
}
}
)
)
}
export function assertFailure(codec: t.Any, value: unknown, errors: Array<string>): void {
const result = codec.decode(value)
pipe(
result,
fold(
() => {
assert.deepStrictEqual(PathReporter.report(result), errors)
},
/* istanbul ignore next */
() => {
throw new Error(`${result} is not a left`)
}
)
)
}
export const NumberFromString = new t.Type<number, string, unknown>(
'NumberFromString',
t.number.is,
(u, c) =>
either.chain(t.string.validate(u, c), (s) => {
const n = +s
return isNaN(n) ? t.failure(u, c, 'cannot parse to a number') : t.success(n)
}),
String
)
export const HyphenatedString = new t.Type<string, string, unknown>(
'HyphenatedString',
(v): v is string => t.string.is(v) && v.length === 3 && v[1] === '-',
(u, c) =>
either.chain(t.string.validate(u, c), (s) => {
if (s.length === 2) {
return right(s[0] + '-' + s[1])
} else {
return t.failure(s, c)
}
}),
(a) => a[0] + a[2]
)
export const IntegerFromString = t.refinement(NumberFromString, t.Integer.is, 'IntegerFromString')
export function withDefault<T extends t.Mixed>(
type: T,
defaultValue: t.TypeOf<T>
): t.Type<t.TypeOf<T>, t.TypeOf<T>, unknown> {
return new t.Type(
`withDefault(${type.name}, ${JSON.stringify(defaultValue)})`,
type.is,
(v) => type.decode(v != null ? v : defaultValue),
type.encode
)
}