You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

const atSymbol = '@'
const allAtSymbolsRegExp = /@/g
const emptyString = ''
const atDot = '@.'
const dot = '.'
const dotDot = '..'
const emptyArray = []
const allDotsRegExp = /\./g
export default function emailPipe(conformedValue, config) {
const {currentCaretPosition, rawValue, previousConformedValue, placeholderChar} = config
let value = conformedValue
value = removeAllAtSymbolsButFirst(value)
const indexOfAtDot = value.indexOf(atDot)
const emptyEmail = rawValue.match(new RegExp(`[^@\\s.${placeholderChar}]`)) === null
if (emptyEmail) {
return emptyString
}
if (
value.indexOf(dotDot) !== -1 ||
indexOfAtDot !== -1 && currentCaretPosition !== (indexOfAtDot + 1) ||
rawValue.indexOf(atSymbol) === -1 && previousConformedValue !== emptyString && rawValue.indexOf(dot) !== -1
) {
return false
}
const indexOfAtSymbol = value.indexOf(atSymbol)
const domainPart = value.slice(indexOfAtSymbol + 1, value.length)
if (
(domainPart.match(allDotsRegExp) || emptyArray).length > 1 &&
value.substr(-1) === dot &&
currentCaretPosition !== rawValue.length
) {
value = value.slice(0, value.length - 1)
}
return value
}
function removeAllAtSymbolsButFirst(str) {
let atSymbolCount = 0
return str.replace(allAtSymbolsRegExp, () => {
atSymbolCount++
return (atSymbolCount === 1) ? atSymbol : emptyString
})
}