-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
31 lines (30 loc) · 887 Bytes
/
index.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
import Guard from "../../Guard";
import TValidate from "../TValidate";
/**
* Validates if a string matches a regexp.
*
* @returns
* A `Guard` that accepts only strings that matches the given `regexp`.
*
* `guard.name: "string(<regexpName>)"`
*
* @param patternName - Describes the regular expression in a user-readable form.
* @param regexp - The regexp to use for validation of incoming values.
*
* @example
* ```ts
* const TStringUpperCase = TStringMatch("string(upper-case)",/^[A-Z]/);
* TStringUpperCase.isValid("Foo"); // true
* TStringUpperCase.isValid("foo"); // false
* TStringUpperCase.name === "string(upper-case)"; // true
```
*/
export default function TStringMatch(
patternName: string,
regexp: RegExp
): Guard<string> {
return TValidate<string>(
`string(${patternName})`,
(value) => typeof value === "string" && regexp.test(value)
);
}