nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
65 lines (64 loc) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMultiple = exports.isOdd = exports.isEven = void 0;
exports.isPerfectSquare = isPerfectSquare;
exports.isFibonacci = isFibonacci;
exports.areInvalidNumbers = areInvalidNumbers;
/**
* * Check if a number is even or not.
*
* @param input The number to check.
* @returns Boolean: `true` if even and `false` if not even.
*/
const isEven = (input) => {
return input % 2 === 0;
};
exports.isEven = isEven;
/**
* * Checks if a number is odd or not.
*
* @param input The number to check.
* @returns Boolean: `true` if odd and `false` if not odd.
*/
const isOdd = (input) => {
return input % 2 !== 0;
};
exports.isOdd = isOdd;
/**
* * Checks if a number is a multiple of another number.
*
* @param input - The number to check.
* @param multipleOf - The number to check against.
* @returns `true` if `input` is a multiple of `multipleOf`, otherwise `false`.
*/
const isMultiple = (input, multipleOf) => {
return input % multipleOf === 0;
};
exports.isMultiple = isMultiple;
/**
* * Checks if a number is a perfect square.
*
* @param num The number to check.
* @returns `true` if the number is a perfect square, otherwise `false`.
*/
function isPerfectSquare(num) {
return Number.isInteger(Math.sqrt(num));
}
/**
* * Checks if a number is part of the Fibonacci sequence.
*
* @param num The number to check.
* @returns `true` if the number is a Fibonacci number, otherwise `false`.
*/
function isFibonacci(num) {
return (isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4));
}
/**
* * Checks whether any input is not a finite number.
*
* @param numbers - The list of numbers to validate.
* @returns `true` if any input is not finite.
*/
function areInvalidNumbers(...numbers) {
return numbers?.some((n) => !Number.isFinite(n));
}