btrconsole
Version:
The better version of a console (or is it?)!
193 lines (142 loc) • 4.08 kB
JavaScript
// btrconsole.js
const readlineSync = require('readline-sync')
const debugModule = () => {
const author = 'npmcool'
const version = '1.1.9'
const published = 'testing'
const otherModulesUsed = ['readline-sync']
console.info('Author: ' + author, '\nVersion: ' + version, '\n' + published, '\nUsing modules: ' + otherModulesUsed)
}
const out = (output) => {
/**
* Outputs what you enter
* @returns {output}
*/
console.log(output)
}
const err = (msg) => {
/**
* Throws an error with your message
* @returns {msg}
*/
console.error(msg)
}
const inf = (inf) => {
/**
* Outputs info
* @returns {inf}
*/
console.info(inf)
}
const wrn = (msg) => {
/**
* Warns user based on what you have entered (functions similar to err)
* @returns {msg}
*/
console.warn(msg)
}
const math = (a, operator, b) => {
/**
* Outputs result
* @returns {a operator b}
* Exmaple usage:
* btrconsole.math(23, '+', 5) // Will return '28'
* Note that the operator must be a string
*/
function checkOperator() {
if (operator === '+') {
return a + b
} else if (operator === '-') {
return a - b
} else if (operator === '*') {
return a * b
} else if (operator === '/') {
return a / b
} else if (operator === '%') {
return a % b
} else {
console.error(`Could not find your operator ${operator} as a valid operator.\nVALID_OPERATORS: +, -, *, /, %`)
}
}
const result = checkOperator()
return result
}
const throw404 = (bool) => {
if (bool === true) {
console.error("404ERROR, not found.\nThrown: Force")
} else if (bool === false) {
} else {
console.error(`Could not find your boolean ${bool} as a valid boolean.\nPlease make sure the boolean is true or false\nERR_404 not found`)
}
}
const logWithTimeStamp = (msg) => {
const timestamp = new Date().toLocaleString()
console.log(`[${timestamp}] ${msg}`)
}
const generateRandomNumber = (min, max) => {
if (min >= max) {
throw new Error('Invalid range: min must be less than max');
}
const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
const getUserInput = (question) => {
const userInput = readlineSync.question(question);
return userInput;
}
const capitalizeString = (inputString) => {
if (typeof inputString !== 'string') {
throw new TypeError('Input must be a string');
}
return inputString.charAt(0).toUpperCase() + inputString.slice(1);
};
const calculateAverage = (numbers) => {
if (!Array.isArray(numbers)) {
throw new TypeError('Input must be an array of numbers');
}
if (numbers.length === 0) {
throw new Error('Input array must not be empty');
}
const sum = numbers.reduce((acc, num) => acc + num, 0);
return sum / numbers.length;
};
const greetUser = () => {
var user = readlineSync.question("Enter your username please\n>")
console.log(`Hello, ${user}!`)
}
const isEven = (num) => {
if (typeof num !== Number) {
throw new TypeError(`${num} is not found as a valid number.`)
}
return num % 2 === 0
}
const calculateFactorial = (n) => {
if (typeof n !== 'number' || n < 0 || !Number.isInteger(n)) {
throw new TypeError('Input must be a non-negative integer');
}
if (n === 0 || n === 1) {
return 1;
}
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
module.exports = {
debugModule,
out,
err,
inf,
wrn,
math,
throw404,
logWithTimeStamp,
generateRandomNumber,
getUserInput,
capitalizeString,
calculateAverage,
greetUser,
isEven,
calculateFactorial
};