Skip to content

Instantly share code, notes, and snippets.

@moiseshilario
Last active March 5, 2018 14:00
Show Gist options
  • Select an option

  • Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.

Select an option

Save moiseshilario/2852d3d78e92141fa14c880e34dbee70 to your computer and use it in GitHub Desktop.

Revisions

  1. moiseshilario revised this gist Mar 5, 2018. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions test2.js
    Original 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 COMPANY_REGEX = /(?<=@).+(?=\.)/

    const $companyField = $(companyElement)
    const $companyParagraph = $companyField.closest('p')

    $companyParagraph.hide()

    const capitalizeFirstLetter = (string) => {
    return string.charAt(0).toUpperCase() + string.slice(1)
    }
    const toTitleCase = (string) => (
    string.replace(/\w\S*/g, (word) => (
    word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
    ))
    )

    const companyInput = () => {
    const email = $(emailElement).val()
    const company = email.match(COMPANY_REGEX)
    const companyMatch = email.match(COMPANY_REGEX)

    if(company) {
    const companyName = capitalizeFirstLetter(company[0])
    if(companyMatch) {
    const rawCompanyName = companyMatch[0].replace(/\./g,' ')
    const companyName = toTitleCase(rawCompanyName)
    $companyField.val(companyName)
    $companyParagraph.show('slow')
    }
  2. moiseshilario created this gist Mar 1, 2018.
    48 changes: 48 additions & 0 deletions test2.js
    Original 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);