Skip to content

Commit 8b0b214

Browse files
mixedCasegcanti
authored andcommitted
Add tests and documentation for bigint codec
1 parent e7b23a1 commit 8b0b214

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

docs/modules/index.ts.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Added in v1.0.0
1616
- [~~AnyC~~ (interface)](#anyc-interface)
1717
- [AnyProps (interface)](#anyprops-interface)
1818
- [ArrayC (interface)](#arrayc-interface)
19+
- [BigIntC (interface)](#bigintc-interface)
1920
- [BooleanC (interface)](#booleanc-interface)
2021
- [Brand (interface)](#brand-interface)
2122
- [BrandC (interface)](#brandc-interface)
@@ -88,6 +89,7 @@ Added in v1.0.0
8889
- [AnyDictionaryType (class)](#anydictionarytype-class)
8990
- [~~AnyType~~ (class)](#anytype-class)
9091
- [ArrayType (class)](#arraytype-class)
92+
- [BigIntType (class)](#biginttype-class)
9193
- [BooleanType (class)](#booleantype-class)
9294
- [DictionaryType (class)](#dictionarytype-class)
9395
- [ExactType (class)](#exacttype-class)
@@ -123,6 +125,7 @@ Added in v1.0.0
123125
- [UnknownRecord](#unknownrecord)
124126
- [appendContext](#appendcontext)
125127
- [array](#array)
128+
- [bigint](#bigint)
126129
- [boolean](#boolean)
127130
- [brand](#brand)
128131
- [exact](#exact)
@@ -212,6 +215,16 @@ export interface ArrayC<C extends Mixed> extends ArrayType<C, Array<TypeOf<C>>,
212215

213216
Added in v1.5.3
214217

218+
# BigIntC (interface)
219+
220+
**Signature**
221+
222+
```ts
223+
export interface BigIntC extends BigIntType {}
224+
```
225+
226+
Added in v2.1.0
227+
215228
# BooleanC (interface)
216229

217230
**Signature**
@@ -1093,6 +1106,19 @@ export class ArrayType<C, A, O, I> {
10931106

10941107
Added in v1.0.0
10951108

1109+
# BigIntType (class)
1110+
1111+
**Signature**
1112+
1113+
```ts
1114+
export class BigIntType {
1115+
constructor() { ... }
1116+
...
1117+
}
1118+
```
1119+
1120+
Added in v2.1.0
1121+
10961122
# BooleanType (class)
10971123

10981124
**Signature**
@@ -1642,6 +1668,16 @@ export const array = <C extends Mixed>(codec: C, name: string = `Array<${codec.n
16421668
16431669
Added in v1.0.0
16441670
1671+
# bigint
1672+
1673+
**Signature**
1674+
1675+
```ts
1676+
export const bigint: BigIntC = ...
1677+
```
1678+
1679+
Added in v2.1.0
1680+
16451681
# boolean
16461682
16471683
**Signature**

test/default-types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,29 @@ describe('boolean', () => {
114114
})
115115
})
116116

117+
describe('bigint', () => {
118+
const T = t.bigint
119+
it('should decode bigint values', () => {
120+
assertSuccess(T.decode(BigInt(0)))
121+
assertSuccess(T.decode(BigInt(15)))
122+
const decodedBigNumber = T.decode(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(4))
123+
assertSuccess(decodedBigNumber)
124+
if (decodedBigNumber._tag === 'Right') {
125+
assert.equal(decodedBigNumber.right.toString(), '9007199254740995')
126+
}
127+
})
128+
129+
it('should not decode non-bigint values', () => {
130+
assertFailure(T, true, ['Invalid value true supplied to : bigint'])
131+
assertFailure(T, 'test', ['Invalid value "test" supplied to : bigint'])
132+
assertFailure(T, 123, ['Invalid value 123 supplied to : bigint'])
133+
assertFailure(T, {}, ['Invalid value {} supplied to : bigint'])
134+
assertFailure(T, [], ['Invalid value [] supplied to : bigint'])
135+
assertFailure(T, null, ['Invalid value null supplied to : bigint'])
136+
assertFailure(T, undefined, ['Invalid value undefined supplied to : bigint'])
137+
})
138+
})
139+
117140
describe('Integer', () => {
118141
it('should validate integers', () => {
119142
// tslint:disable-next-line: deprecation

test/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function assertSuccess<T>(result: t.Validation<T>, expected?: T): void {
3131
if (expected !== undefined) {
3232
assert.deepStrictEqual(a, expected)
3333
}
34+
return true
3435
}
3536
)
3637
)

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"moduleResolution": "node",
1414
"forceConsistentCasingInFileNames": true,
1515
"stripInternal": true,
16-
"lib": ["es2015"]
16+
"lib": ["es2015", "ESNext.BigInt"]
1717
},
1818
"include": ["./src/**/*"]
1919
}

0 commit comments

Comments
 (0)