Skip to content

Instantly share code, notes, and snippets.

@moiseshilario
Last active March 5, 2018 14:00
Show Gist options
  • Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.
Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.
Test2
;(function companyNameFromEmail(emailElement, companyElement, $) {
const DEBOUNCE_TIME = 250
const COMPANY_REGEX = /(?<=@).+(?=\.)/
const $companyField = $(companyElement)
const $companyParagraph = $companyField.closest('p')
$companyParagraph.hide()
const toTitleCase = (string) => (
string.replace(/\w\S*/g, (word) => (
word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
))
)
const companyInput = () => {
const email = $(emailElement).val()
const companyMatch = email.match(COMPANY_REGEX)
if(companyMatch) {
const rawCompanyName = companyMatch[0].replace(/\./g,' ')
const companyName = toTitleCase(rawCompanyName)
$companyField.val(companyName)
$companyParagraph.show('slow')
}
}
const debounce = (func, wait, immediate) => {
let timeout
return function() {
const context = this
const args = arguments
const later = () => {
timeout = null
if(!immediate) func.apply(context, args)
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if(callNow) func.apply(context, args)
}
}
$(emailElement).keyup(
debounce(companyInput, DEBOUNCE_TIME)
)
})('[name=your-work-email]', '[name=your-company]', jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment