-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-checkers.ts
69 lines (57 loc) · 1.9 KB
/
string-checkers.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* TODO: This is deprecated. Move this to validators
*/
import * as MiscValidators from "../validators/misc-validators";
const timeFormats: { [key: string]: string } = {
HH : "(2[0-3]|[01]\\d)",
H : "(2[0-3]|[01]?\\d)",
mm : "([0-5]\\d)",
m : "([0-5]?\\d)",
MM : "(0\\d|1[0-2]|\\d)",
M : "([1-9]|1[0-2])",
ss : "([0-5]\\d)", // mm
s : "([0-5]?\\d)", // ss
YYYY: "([1-9]\\d{3,3})",
YY : "(\\d{2,2})",
DD : "([0-3]\\d)",
};
export function isCamelCase(text: string): boolean {
return new RegExp("^[A-Z]?[a-z]+([A-Z][a-z]*)*$", "g").test(text);
}
export function isUpperCamelCase(text: string): boolean {
return new RegExp("^([A-Z][a-z]*)*$", "g").test(text);
}
export function isLowerCamelCase(text: string): boolean {
return new RegExp("^[a-z]+([A-Z][a-z]*)*$", "g").test(text);
}
export function isLowerSnakeCase(text: string): boolean {
return new RegExp("^[a-z]*(_[a-z]*)*$", "g").test(text);
}
export function isUpperSnakeCase(text: string): boolean {
return new RegExp("^[A-Z]*(_[A-Z]*)*$", "g").test(text);
}
export function isSnakeCase(text: string): boolean {
return new RegExp("^([a-z]*|[A-Z]*)(_[a-zA-Z]*)*$", "g").test(text);
}
export function isTimeFormat(text: string, format: string): boolean {
for (const key in timeFormats) {
if (timeFormats.hasOwnProperty(key)) {
format = format.replace(key, timeFormats[key]);
}
}
return new RegExp(`^${format}$`).test(text);
}
/**
* @deprecated use {@link MiscValidators.isValidPhoneNumber} instead
* @param num - num to validate
*/
export function isValidPhoneNumber(num: string): boolean {
return MiscValidators.isValidPhoneNumber(num);
}
/**
* @deprecated use {@link MiscValidators.isValidEmail} instead
* @param email - email to validate
*/
export function isValidEmail(email: string): boolean {
return MiscValidators.isValidEmail(email);
}