-
Notifications
You must be signed in to change notification settings - Fork 1
/
string-example.ts
44 lines (38 loc) · 1.33 KB
/
string-example.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
/**
* Here's the thing I want to be sure about when I use it. I want to know that
* something is a well-formed email address as per the RFC (roughly).
*/
interface EmailAddress {
readonly EmailAddress: unique symbol;
}
/**
* Regular expression to validate email addresses
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#basic_validation
*/
const validityExpression =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
/** Is the given `candidate` string a valid {@link EmailAddress}? */
function isEmailAddress(candidate: string): candidate is EmailAddress & string {
return validityExpression.test(candidate);
}
//
// Somewhere in a project...
//
// A convenience for nicely displaying an email addres...
function showEmailAddress(addr: EmailAddress): string {
return `This is an email address: ${addr}`;
}
// A convenience for nicely displaying a name...
function showName(name: string): string {
return `This is a name or something: ${name}`;
}
// This gets string data that for unfortunately _unioned_ and chooses an
// appropriate utility for displaying the data.
function handleContact(info: string): string {
if (isEmailAddress(info)) {
return showEmailAddress(info);
} else {
return showName(info);
}
}