-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.ts
73 lines (55 loc) · 1.86 KB
/
utils.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
70
71
72
73
const isLetter = (char: string) => {
return /^\p{L}/u.test(char);
};
const isUpperCase = (char: string) => {
return char === char.toUpperCase();
};
export const isAt = (value: string): boolean => {
// Check if the first character is '@'
const first = value.charAt(0);
return first === "@";
};
export const getCapitalPercentage = (str: string): number => {
let totalLetters = 0;
let upperLetters = 0;
for (const currentLetter of str) {
if (!isLetter(currentLetter)) continue;
if (isUpperCase(currentLetter)) {
upperLetters++;
}
totalLetters++;
}
return upperLetters / totalLetters;
};
export const isUri = (value: string): boolean => {
if (!value) return false;
// Check for illegal characters
if (/[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i.test(value)) {
return false;
}
// Check for hex escapes that aren't complete
if (/%[^0-9a-f]/i.test(value) || /%[0-9a-f](:?[^0-9a-f]|$)/i.test(value)) {
return false;
}
// Directly from RFC 3986
const split = value.match(
/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,
);
if (!split) return false;
const [, scheme, authority, path] = split;
// Scheme and path are required, though the path can be empty
if (!(scheme && scheme.length && path.length >= 0)) return false;
// If authority is present, the path must be empty or begin with a /
if (authority && authority.length) {
if (!(path.length === 0 || /^\//.test(path))) return false;
} else if (/^\/\//.test(path)) {
// If authority is not present, the path must not start with //
return false;
}
// Scheme must begin with a letter, then consist of letters, digits, +, ., or -
if (!/^[a-z][a-z0-9\+\-\.]*$/.test(scheme.toLowerCase())) return false;
return true;
};
export const isBreak = (word: string): boolean => {
return word.trim() === "";
};