-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EmailAdjuster.es
109 lines (97 loc) · 3.02 KB
/
EmailAdjuster.es
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {CAUSE} from "./constants";
import AdjusterInterface from "./AdjusterInterface";
import StringAdjuster from "./StringAdjuster";
const MAX_LENGTH = 254;
// https://tools.ietf.org/html/rfc5321
// https://tools.ietf.org/html/rfc5322
const REGEXP_CHARSET_DOT = "[\\w!#$%&'*+\\-\\/=?^`{|}~]";
const REGEXP_CHARSET_QUOTED = "[\\w!#$%&'*+\\-\\/=?^`{|}~. ()<>\\[\\]:;@,]";
const REGEXP_CHARSET_TLD = "[a-zA-Z]";
const REGEXP_CHARSET_SLD = "[a-zA-Z\\d\\-]";
const REGEXP_CHARSET_IPV4 = "\\d";
const REGEXP_CHARSET_IPV6 = "[\\da-fA-F]";
const REGEXP_COMPONENT_DOT = `${REGEXP_CHARSET_DOT}+`;
const REGEXP_COMPONENT_QUOTED = `(${REGEXP_CHARSET_QUOTED}|\\\\|\\")+`;
const REGEXP_COMPONENT_TLD = `${REGEXP_CHARSET_TLD}+`;
const REGEXP_COMPONENT_SLD = `${REGEXP_CHARSET_SLD}+`;
const REGEXP_COMPONENT_IPV4 = `${REGEXP_CHARSET_IPV4}{1,3}`;
const REGEXP_COMPONENT_IPV6 = `${REGEXP_CHARSET_IPV6}{0,4}`;
const REGEXP_LOCAL_DOT = `${REGEXP_COMPONENT_DOT}(\\.${REGEXP_COMPONENT_DOT})*`;
const REGEXP_LOCAL_QUOTED = `"${REGEXP_COMPONENT_QUOTED}"`;
const REGEXP_LOCAL = `(${REGEXP_LOCAL_DOT}|${REGEXP_LOCAL_QUOTED})`;
const REGEXP_DOMAIN_GENERAL = `(${REGEXP_COMPONENT_SLD}\\.)+${REGEXP_COMPONENT_TLD}`;
const REGEXP_DOMAIN_IPV4 = `${REGEXP_COMPONENT_IPV4}(\\.${REGEXP_COMPONENT_IPV4}){3}`;
const REGEXP_DOMAIN_IPV6_COMMON = `${REGEXP_COMPONENT_IPV6}(:${REGEXP_COMPONENT_IPV6}){1,5}`;
const REGEXP_DOMAIN_IPV6_REST = `(:${REGEXP_DOMAIN_IPV4}|(:${REGEXP_COMPONENT_IPV6}){0,2})`;
const REGEXP_DOMAIN_IPV6 = `IPv6:${REGEXP_DOMAIN_IPV6_COMMON}${REGEXP_DOMAIN_IPV6_REST}`;
const REGEXP_DOMAIN_IP = `\\[(${REGEXP_DOMAIN_IPV4}|${REGEXP_DOMAIN_IPV6})\\]`;
const REGEXP_DOMAIN = `(${REGEXP_DOMAIN_GENERAL}|${REGEXP_DOMAIN_IP})`;
const REGEXP_EMAIL = `^${REGEXP_LOCAL}@${REGEXP_DOMAIN}$`;
const PATTERN = new RegExp(REGEXP_EMAIL);
/**
* adjuster for e-mail
*/
export default class EmailAdjuster extends AdjusterInterface
{
/**
* constructor
*/
constructor()
{
super();
this._objAdjuster = new StringAdjuster()
.maxLength(MAX_LENGTH)
.pattern(PATTERN);
}
/**
* set default value; enable to omit
* @param {string} value default value
* @return {EmailAdjuster}
*/
default(value)
{
this._objAdjuster.default(value);
return this;
}
/**
* allow empty string (NOT undefined)
* @param {?string} [value=null] value on empty
* @return {EmailAdjuster}
*/
allowEmpty(value = null)
{
this._objAdjuster.allowEmpty(value);
return this;
}
/**
* specify custom pattern by regular expression
* @param {RegExp} pattern acceptable pattern
* @return {EmailAdjuster}
*/
pattern(pattern)
{
this._objAdjuster.pattern(pattern);
return this;
}
/**
* do adjust
* @param {*} value value to be checked
* @param {?_OnError} onError callback function on error
* @return {string} adjusted value
*/
adjust(value, onError = null)
{
try
{
return this._objAdjuster.adjust(value);
}
catch(err)
{
if(err.cause === CAUSE.PATTERN)
{
err.cause = CAUSE.EMAIL;
}
return AdjusterInterface._handleError(onError, err.cause, value);
}
}
}