UNPKG

656 BPlain TextView Raw
1import type { Verification } from "./types";
2
3type PostalCodeOptions = {
4 minLength?: number;
5};
6
7const DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;
8
9function verification(
10 isValid: boolean,
11 isPotentiallyValid: boolean,
12): Verification {
13 return { isValid, isPotentiallyValid };
14}
15
16export function postalCode(
17 value: string | unknown,
18 options: PostalCodeOptions = {},
19): Verification {
20 const minLength = options.minLength || DEFAULT_MIN_POSTAL_CODE_LENGTH;
21
22 if (typeof value !== "string") {
23 return verification(false, false);
24 } else if (value.length < minLength) {
25 return verification(false, true);
26 }
27
28 return verification(true, true);
29}