@timshel_npm/maildev
Version:
SMTP Server with async API and Web Interface for viewing and testing emails during development
66 lines (65 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeId = makeId;
exports.formatBytes = formatBytes;
exports.filterEmails = filterEmails;
exports.delay = delay;
// Create an unique id, length 8 characters
function makeId() {
let text = "";
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 8; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
// Format bytes
// Source: https://stackoverflow.com/a/18650828/3143704
function formatBytes(bytes, decimals = 2) {
if (bytes === 0)
return "0 bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
function lookup(obj, path) {
const parts = path.split(".");
const base = obj[parts[0]];
if (!base)
return;
if (parts.length === 1) {
return base;
}
const next = parts.slice(1).join(".");
if (Array.isArray(base)) {
return base.map((el) => {
return lookup(el, next);
});
}
else {
return lookup(base, next);
}
}
function filterEmails(emails, query) {
return emails.filter((email) => {
const hits = [];
for (const key in query) {
if (Object.hasOwnProperty.call(query, key)) {
const element = query[key];
const value = lookup(email, key);
if (Array.isArray(value)) {
hits.push(value.includes(element));
}
else {
hits.push(value === element);
}
}
}
return !hits.includes(false);
});
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}