UNPKG

email-validator-helper

Version:

This package provides your application with several types of email validation: syntax, domain, MX server, and much more.

118 lines 4.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const { dns } = require("dns"); const { promisify } = require("util"); const { IPToASN } = require("ip-to-asn"); module.exports = class Validator { constructor(email) { this.emailRegEx = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; this.checkSingle = (email) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { const emailValidateStatus = this.getEmailValidation(email); if (email && emailValidateStatus) { resolve(true); } else { resolve(false); } })); }); this.checkList = (array) => __awaiter(this, void 0, void 0, function* () { if (array && array.length > 0) { return yield this.getEmailValidationBatch(array); } else { return "You need send array of email validation."; } }); } findMxRecord(domain) { return new Promise((resolve, reject) => { dns.resolveMx(domain, (err, address) => { if (err) { return reject({ err, message: 'Domain not found' }); } const min = address.reduce((a, b) => { if (a.priority > b.priority) { return a; } return b; }, {}); return resolve(min); }); }); } getEmailValidation(email) { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { try { const emailValidateStatus = this.checkSyntax(email); let address = null; let domain = null; if (emailValidateStatus) { domain = this.getDomain(email); address = this.findMxRecord(domain); if (domain && (yield address) && emailValidateStatus) { resolve(true); } else { resolve(false); } } else { resolve(false); } } catch (error) { resolve(false); } })); } getEmailValidationBatch(array) { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { if (array.length > 0) { let valid = []; let invalid = []; for (const email of array) { let isValid = yield this.getEmailValidation(email); if (isValid) { valid.push(email); } else { invalid.push(email); } } yield resolve({ valid: valid, invalid: invalid }); } else { yield resolve([]); } })); } checkSyntax(email) { return this.emailRegEx.test(email); } getDomain(email) { const domain = email.split('@'); return domain[1]; } analyzer(email) { if (email) { return this.getEmailValidation(email); } else { return "You need send array of email validation."; } } }; //# sourceMappingURL=index.js.map