-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.ts
36 lines (32 loc) · 1.19 KB
/
numbers.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
import { fail, succeed, ValidationResult, Validator } from "./validator.ts";
import { chain } from "./mapping.ts";
/** `num` is a `Validator` that tries to convert the given string into a number. */
export const num: Validator<string, number> = (s: string) => {
const failure: ValidationResult<number> = fail("is not a number");
if (s.length < 1) {
return failure;
}
const i = Number(s);
if (isNaN(i)) {
return failure;
}
return succeed(i);
};
/** `wholeNumber` is a `Validator` that fails if the given number is not an integer. */
export const wholeNumber: Validator<number, number> = (n: number) => {
if (Number.isInteger(n)) {
return succeed(n);
}
return fail("is not a whole number");
};
/** `int` is a `Validator` that tries to convert the given string into a whole number. */
export const int: Validator<string, number> = chain(num, wholeNumber);
/** `lessThan` takes a limit and returns a `Validator` that fails if the given number is greater than or equal to the limit. */
export function lessThan(limit: number): Validator<number, number> {
return ((n: number) => {
if (!(n < limit)) {
return fail(`must be less than ${limit}`);
}
return succeed(n);
});
}