-
Notifications
You must be signed in to change notification settings - Fork 1
/
bigInt.ts
33 lines (29 loc) · 960 Bytes
/
bigInt.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
import { pipe } from 'fp-ts/function';
import * as O from 'fp-ts/Option';
import * as RE from 'fp-ts/ReaderEither';
import { asks, unwrap, validate, VariableDecoder, withOpt } from './VariableDecoder';
type Options = {
/**
* Maximum value that will be accepted (inclusive)
*/
max?: BigInt | undefined;
/**
* Minimum value that will be accepted (inclusive)
*/
min?: BigInt | undefined;
};
/**
* Decodes a BigInt.
*/
const bigInt = (options: Options = {}): VariableDecoder<BigInt> =>
pipe(
asks((x) => O.tryCatch(() => BigInt(x))),
RE.chain(unwrap('must be a valid BigInt')),
withOpt(options.max)((max) =>
RE.chain(validate((n) => n <= max, `must be smaller than or equal to ${String(max)}`)),
),
withOpt(options.min)((min) =>
RE.chain(validate((n) => n >= min, `must be greater than or equal to ${String(min)}`)),
),
);
export { bigInt };