-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
219 lines (192 loc) · 5.79 KB
/
demo.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html>
<head>
<title>Email Validator Demo</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />
<style>
*,
input {
font-family: monospace;
}
html {
margin: 0;
padding: 0;
}
body {
width: calc(768px - 2em);
min-width: calc(768px - 2em);
max-width: calc(768px - 2em);
margin: 1em auto;
padding: 0;
}
body,
input {
font-size: 1.5rem;
}
p {
margin-bottom: 1em;
}
input[type="text"] {
width: 100%;
box-sizing: border-box;
padding: 0.5em;
border-radius: 4px;
border: 1px solid gray;
}
#message {
padding: 1em;
border-radius: 4px;
background-color: rgb(235, 235, 235);
color: gray;
border: 1px solid gray;
font-weight: bold;
}
#message.is-valid {
background-color: hsl(142, 52%, 96%);
color: hsl(141, 71%, 48%);
border: 1px solid hsl(141, 71%, 48%);
}
#message.is-invalid {
background-color: hsl(347, 90%, 96%);
color: hsl(348, 100%, 61%);
border: 1px solid hsl(348, 100%, 61%);
}
@media (max-width: 768px) {
body {
width: calc(100% - 2em);
min-width: calc(100% - 2em);
max-width: calc(100% - 2em);
margin: 1em;
}
}
</style>
</head>
<body>
<h1>Email Validator Demo</h1>
<p>
<input type="text" id="email-field" autofocus />
</p>
<div id="message">Status: NONE</div>
<script src="node_modules/lodash/lodash.min.js"></script>
<script src="dist/email-validator.js"></script>
<script>
function expect(v1) {
return {
toBe(v2) {
if (v1 !== v2) {
throw new Error(`Expected ${v2} but received ${v1}!`)
}
},
toThrow() {
let failed = false
try {
v1()
} catch (e) {
failed = true
}
if (!failed) {
throw new Error(
`The given function was supposed to throw an error, but it didn't!`
)
}
},
}
}
async function run() {
// run automated tests
const validator = new EmailValidator()
expect(validator.isReady).toBe(false)
validator.isReady = true
expect(validator.isReady).toBe(false)
await validator.load()
const items = [
{ email: "someone@example.com", shouldPass: true },
{ email: "s.o.m.e.o.n.e@e.x.a.m.p.l.e.com", shouldPass: true },
{ email: "someone+test@example.com", shouldPass: true },
{ email: "someone@example+test.com", shouldPass: true },
{ email: "someone@example", shouldPass: false },
{ email: "someone@雨.com", shouldPass: true },
{ email: "雨@example.com", shouldPass: true },
{ email: "雨@雨.雨", shouldPass: false },
{ email: "someone@1.2.3.4", shouldPass: false },
{ email: "@example.com", shouldPass: false },
{ email: "someone@", shouldPass: false },
{ email: "someone@example.thisshouldfail", shouldPass: false },
{ email: "someoneexamplecom", shouldPass: false },
{ email: "someone.example@com", shouldPass: false },
{ email: "someone@123.456.789", shouldPass: false },
{ email: "SOMEONE@EXAMPLE.COM", shouldPass: true },
{ email: "someone@.com", shouldPass: false },
{ email: "someone@example test.com", shouldPass: false },
]
items.forEach(item => {
expect(validator.validate(item.email)).toBe(item.shouldPass)
})
const wrongs = [
0,
1,
2.3,
-2.3,
Infinity,
-Infinity,
NaN,
"foo",
true,
false,
null,
undefined,
Symbol.for("Hello, world!"),
[2, 3, 4],
[
[2, 3, 4],
[5, 6, 7],
],
x => x,
function (x) {
return x
},
{ hello: "world" },
]
wrongs.forEach(value => {
expect(validator.isValid(value)).toBe(false)
const temp = new EmailValidator()
if (value instanceof Array) {
temp.topLevelDomainList = value
expect(temp.isReady).toBe(false)
} else {
expect(() => (temp.topLevelDomainList = value)).toThrow()
}
})
console.log("All automated tests passed!")
// set up demo app
const emailField = document.getElementById("email-field")
const messageContainer = document.getElementById("message")
emailField.addEventListener(
"input",
_.debounce(() => {
if (emailField.value.trim().length === 0) {
messageContainer.classList.remove("is-valid")
messageContainer.classList.remove("is-invalid")
messageContainer.innerText = "Status: NONE"
return
}
const isValid = validator.validate(emailField.value)
if (isValid) {
messageContainer.innerText = "Status: VALID"
messageContainer.classList.remove("is-invalid")
messageContainer.classList.add("is-valid")
} else {
messageContainer.innerText = "Status: INVALID"
messageContainer.classList.remove("is-valid")
messageContainer.classList.add("is-invalid")
}
}, 10)
)
}
run()
</script>
</body>
</html>