-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathhashEmail.js
More file actions
29 lines (24 loc) · 877 Bytes
/
Copy pathhashEmail.js
File metadata and controls
29 lines (24 loc) · 877 Bytes
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
import $ from 'jquery';
import md5 from 'md5';
const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
export default function (options) {
// Hash the email, if it is an email.
const email = normalizeEmail($(options.email_selector).val());
if (email !== '' && EMAIL_REGEX.test(email)) {
const hashed_email = hashEmail(email);
$(options.hashed_email_selector).val(hashed_email);
// Unless we want to deliberately skip the step of clearing the email.
if (!options.skip_clear_email) {
// If age < 13, don't send the plaintext email.
if (!options.age_selector || $(options.age_selector).val() < 13) {
$(options.email_selector).val('');
}
}
}
}
export function hashEmail(cleartextEmail) {
return md5(normalizeEmail(cleartextEmail));
}
function normalizeEmail(rawEmail) {
return rawEmail.toLowerCase().trim();
}