Last active
March 5, 2018 14:00
-
-
Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.
Revisions
-
moiseshilario revised this gist
Mar 5, 2018 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,25 @@ ;(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') } -
moiseshilario created this gist
Mar 1, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ ;(function companyNameFromEmail(emailElement, companyElement, $) { const DEBOUNCE_TIME = 250 const COMPANY_REGEX = /(?<=@)[^.]+(?=\.)/ const $companyField = $(companyElement) const $companyParagraph = $companyField.closest('p') $companyParagraph.hide() const capitalizeFirstLetter = (string) => { return string.charAt(0).toUpperCase() + string.slice(1) } const companyInput = () => { const email = $(emailElement).val() const company = email.match(COMPANY_REGEX) if(company) { const companyName = capitalizeFirstLetter(company[0]) $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);