UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

72 lines (71 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._find2NumbersLCM = exports._find2NumbersHCF = exports._applyMultiples = void 0; exports._convertLessThanThousand = _convertLessThanThousand; const constants_1 = require("./constants"); /** * Apply multiples of a number if there is any. * @param array Array of numbers to apply the condition on. * @param multiples The multiples of which number. * @returns Array of multiples of the desired number */ const _applyMultiples = (array, multiples) => { if (!multiples) return array; return array?.filter((n) => n % multiples === 0); }; exports._applyMultiples = _applyMultiples; /** * - Converts a number less than 1000 to words. * @param num - The number to convert (less than 1000). * @param isLast - Whether this is the last group (thousands, millions, etc.). * @returns Numbers less than 1000 in words. */ function _convertLessThanThousand(num, isLast) { if (num < 10) return constants_1.ONES[num]; if (num < 20) return constants_1.TEENS[num - 10]; let result = constants_1.TENS[Math.floor(num / 10)]; const remainder = num % 10; if (remainder > 0) result += `-${constants_1.ONES[remainder]}`; if (num >= 100) { const hundredsPart = `${constants_1.ONES[Math.floor(num / 100)]} hundred`; return num % 100 === 0 ? hundredsPart : `${hundredsPart} ${isLast ? 'and' : ''} ${_convertLessThanThousand(num % 100, false)}`; } return result; } /** * * Calculate the HCF (Highest Common Factor) of two numbers using the Euclidean algorithm. * * @param a - First number. * @param b - Second number. * @returns The HCF of the two numbers. */ const _find2NumbersHCF = (a, b) => { let x = Math.abs(a); let y = Math.abs(b); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; exports._find2NumbersHCF = _find2NumbersHCF; /** * * Calculate the LCM (Least Common Multiple) of two numbers using the Euclidean algorithm. * * @param a - First number. * @param b - Second number. * @returns The LCM of the two numbers. */ const _find2NumbersLCM = (a, b) => { const x = Math.abs(a); const y = Math.abs(b); return (x * y) / (0, exports._find2NumbersHCF)(x, y); }; exports._find2NumbersLCM = _find2NumbersLCM;