Last active
March 5, 2018 14:00
-
-
Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.
Test2
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 characters
| ;(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment