@justthedev123/builderutils
Version:
Basic utilities for developers
106 lines (94 loc) • 3.36 kB
JavaScript
const net = require('net');
const readline = require('readline');
// 1. isWeekend
function isWeekend(date = new Date()) {
const day = date.getDay();
return day === 0 || day === 6;
}
// 2. randomEmoji
function randomEmoji() {
const emojis = ['😀', '🚀', '🔥', '🍕', '🎉', '🐱', '🐸', '💻', '🎮'];
return emojis[Math.floor(Math.random() * emojis.length)];
}
// 3. durationHumanizer
function durationHumanizer(ms) {
const s = Math.floor(ms / 1000);
const m = Math.floor(s / 60);
const h = Math.floor(m / 60);
return `${h}h ${m % 60}m ${s % 60}s`;
}
// 4. formatTimeMs
function formatTimeMs(ms) {
const s = Math.floor(ms / 1000);
const mins = Math.floor(s / 60);
const hrs = Math.floor(mins / 60);
const pad = n => String(n).padStart(2, '0');
return `${pad(hrs)}:${pad(mins % 60)}:${pad(s % 60)}`;
}
// 5. portChecker
function portChecker(port, host = '127.0.0.1') {
return new Promise((resolve) => {
const server = net.createServer()
.once('error', () => resolve(false))
.once('listening', () => {
server.close(() => resolve(true));
})
.listen(port, host);
});
}
// 6. yesNoPrompt
function yesNoPrompt(question = 'Continue? (y/n)') {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(`${question} `, (answer) => {
rl.close();
const normalized = answer.trim().toLowerCase();
resolve(normalized === 'y' || normalized === 'yes');
});
});
}
// 7. simpleProgress
function simpleProgress(current, total, width = 20) {
const percent = current / total;
const filled = Math.round(percent * width);
const bar = '█'.repeat(filled) + '-'.repeat(width - filled);
process.stdout.write(`\r[${bar}] ${Math.round(percent * 100)}%`);
if (current === total) console.log('');
}
// 8. fakeUser (uitgebreid)
function fakeUser() {
const names = ['Sophie', 'Lucas', 'Emma', 'Milan', 'Noah', 'Tess'];
const surnames = ['Jansen', 'de Vries', 'Bakker', 'Visser', 'Smit', 'Meijer'];
const domains = ['example.com', 'mail.com', 'test.nl'];
const streets = ['Dorpsstraat', 'Kerklaan', 'Schoolstraat', 'Hoofdweg', 'Molenstraat'];
const cities = ['Amsterdam', 'Rotterdam', 'Utrecht', 'Eindhoven', 'Groningen'];
const name = names[Math.floor(Math.random() * names.length)];
const surname = surnames[Math.floor(Math.random() * surnames.length)];
const email = `${name.toLowerCase()}.${surname.toLowerCase()}@${domains[Math.floor(Math.random() * domains.length)]}`;
const username = `${name}${Math.floor(Math.random() * 1000)}`;
const password = Math.random().toString(36).slice(-10);
const phone = `06-${Math.floor(10000000 + Math.random() * 90000000)}`;
const address = `${streets[Math.floor(Math.random() * streets.length)]} ${Math.floor(1 + Math.random() * 200)}, ${cities[Math.floor(Math.random() * cities.length)]}`;
return {
name: `${name} ${surname}`,
email,
username,
password,
phone,
address,
};
}
// Export alles
module.exports = {
isWeekend,
randomEmoji,
durationHumanizer,
formatTimeMs,
portChecker,
yesNoPrompt,
simpleProgress,
fakeUser,
};