From f254c06a387c841be4c263aa25d5611541ed93fc Mon Sep 17 00:00:00 2001 From: Zwyx <29386932+Zwyx@users.noreply.github.com> Date: Mon, 26 Feb 2024 15:00:53 +0800 Subject: [PATCH] Implement sub domain matching in JS Changelog mentions that _the implementers should take care of matching the second level domain names properly_, however this wasn't implemented in code examples. At least not in JS. --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index db76de3..4c69a3f 100644 --- a/README.md +++ b/README.md @@ -128,10 +128,21 @@ async function isDisposable(email) { if (!blocklist) { const content = await readFile('disposable_email_blocklist.conf', { encoding: 'utf-8' }) - blocklist = content.split('\r\n').slice(0, -1) + blocklist = content.split('\r\n').slice(0, -1) // Change to `'\n'` depending on your system } - return blocklist.includes(email.split('@')[1]) + const domains = Array.from((email.split('@')[1] || '').matchAll(/(?=(?:\.|^)(.*))/g)) + .map(match => match[1]) + .slice(0, -1); + + // Or, longer but easier to read for people unfamiliar with regex: + // const domains = (email.split('@')[1] || '') + // .split('.') + // .reverse() + // .reduce((acc, cur) => [...acc, `${cur}${acc.length ? `.${acc[acc.length - 1]}` : ''}`], []) // use `reduce` with TypeScript + // .slice(1); + + return domains.map(domain => blocklist.includes(domain || '')).some(Boolean); } ```