In three steps
- Take the part after the
@, in lower case and without spaces. - Remove subdomains:
mail.voorbeeld.nlbecomesvoorbeeld.nl. - Skip addresses at email providers. There's no company behind
gmail.com.
Subdomains
Under .nl, the domain sits directly before the extension. So the last two parts are the domain: mail.voorbeeld.nl becomes voorbeeld.nl.
Other extensions don't always work that way. With .co.uk, the last three parts belong to the domain. If you also work with foreign addresses, use the Public Suffix List instead of the last two parts.
Email providers
An address at an email provider or internet provider says nothing about the company. Common ones in the Netherlands:
gmail.com,outlook.com,hotmail.com,live.nl,icloud.com,yahoo.comziggo.nl,kpnmail.nl,planet.nl,home.nl,xs4all.nl,hetnet.nl
A list like this is never finished. Keep it in one place in your code, and add to it when you see sign-ups that don't belong to a company.
The function
Returns the domain, or null if there's no company behind the address:
const providers = new Set([
'gmail.com', 'outlook.com', 'hotmail.com', 'live.nl', 'icloud.com', 'yahoo.com',
'ziggo.nl', 'kpnmail.nl', 'planet.nl', 'home.nl', 'xs4all.nl', 'hetnet.nl',
])
function companyDomain(email) {
const at = email.lastIndexOf('@')
if (at < 0) return null
const host = email.slice(at + 1).trim().toLowerCase()
if (!host.includes('.')) return null
// under .nl the last two parts are the domain
const domain = host.split('.').slice(-2).join('.')
return providers.has(domain) ? null : domain
}
companyDomain('[email protected]') // 'voorbeeld.nl'
companyDomain('[email protected]') // null
A null here is an answer: there's no company domain. So don't store a company either.
Next
With the domain, you look up the company: GET /v1/companies?domain=voorbeeld.nl. How that fits into a sign-up is on Enrich sign-ups.