All files hashEmail.js

85.71% Statements 12/14
50% Branches 4/8
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      2x               14x   14x 14x   14x   14x 14x   14x       6x 6x       4x 4x    
import md5 from 'tiny-hashes/md5'
import sha256 from 'tiny-hashes/sha256'
 
const PLUS_AND_DOT = /\.|\+.*$/g
 
/**
 * Normalize email by lowering case and extracting + and . symbols for gmail
 * @param {string} email
 * @return {string} Normalized email. If not valid, returns and empty string
 */
export function normalizeEmail(email) {
  Iif (typeof email !== 'string') return ''
 
  email = email.toLowerCase()
  const emailParts = email.split(/@/)
 
  Iif (emailParts.length !== 2) return ''
 
  let [username, domain] = emailParts
  username = username.replace(PLUS_AND_DOT, '')
 
  return `${username}@${domain}`
}
 
export function createUniversalId(email) {
  const normalizedEmail = normalizeEmail(email)
  return normalizedEmail ? sha256(normalizedEmail) : ''
}
 
export function hashEmail(email) {
  const normalizedEmail = normalizeEmail(email)
  return normalizedEmail ? md5(normalizedEmail) : ''
}