UNPKG

sikits

Version:

A powerful and comprehensive utility library for JavaScript and TypeScript with 100+ functions for strings, numbers, arrays, and objects

358 lines (357 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lastDigit = exports.firstDigit = exports.digitCount = exports.isPalindrome = exports.reverseDigits = exports.productOfDigits = exports.sumOfDigits = exports.nthPrime = exports.fibonacci = exports.lcm = exports.gcd = exports.factorial = exports.isPowerOf10 = exports.isPowerOf2 = exports.isPerfectCube = exports.isPerfectSquare = exports.fromOctal = exports.fromHex = exports.fromBinary = exports.toOctal = exports.toHex = exports.toBinary = exports.toIndonesianWords = exports.toWords = exports.fromRoman = exports.toRoman = void 0; /** * Converts a number to Roman numerals */ const toRoman = (num) => { if (num <= 0 || num > 3999) return ''; const romanNumerals = [ { value: 1000, numeral: 'M' }, { value: 900, numeral: 'CM' }, { value: 500, numeral: 'D' }, { value: 400, numeral: 'CD' }, { value: 100, numeral: 'C' }, { value: 90, numeral: 'XC' }, { value: 50, numeral: 'L' }, { value: 40, numeral: 'XL' }, { value: 10, numeral: 'X' }, { value: 9, numeral: 'IX' }, { value: 5, numeral: 'V' }, { value: 4, numeral: 'IV' }, { value: 1, numeral: 'I' } ]; let result = ''; let remaining = num; for (const { value, numeral } of romanNumerals) { while (remaining >= value) { result += numeral; remaining -= value; } } return result; }; exports.toRoman = toRoman; /** * Converts Roman numerals to a number */ const fromRoman = (roman) => { const romanValues = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }; let result = 0; let prevValue = 0; for (let i = roman.length - 1; i >= 0; i--) { const currentValue = romanValues[roman[i].toUpperCase()]; if (!currentValue) return 0; if (currentValue >= prevValue) { result += currentValue; } else { result -= currentValue; } prevValue = currentValue; } return result; }; exports.fromRoman = fromRoman; /** * Converts a number to words (English) */ const toWords = (num) => { if (num === 0) return 'zero'; if (num < 0) return 'negative ' + (0, exports.toWords)(Math.abs(num)); const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']; const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; const scales = ['', 'thousand', 'million', 'billion', 'trillion']; const convertLessThanOneThousand = (n) => { if (n === 0) return ''; if (n < 10) return ones[n]; if (n < 20) return teens[n - 10]; if (n < 100) return tens[Math.floor(n / 10)] + (n % 10 !== 0 ? '-' + ones[n % 10] : ''); if (n < 1000) return ones[Math.floor(n / 100)] + ' hundred' + (n % 100 !== 0 ? ' and ' + convertLessThanOneThousand(n % 100) : ''); return ''; }; let result = ''; let scaleIndex = 0; while (num > 0) { const chunk = num % 1000; if (chunk !== 0) { const chunkWords = convertLessThanOneThousand(chunk); result = chunkWords + (scales[scaleIndex] ? ' ' + scales[scaleIndex] : '') + (result ? ' ' + result : ''); } num = Math.floor(num / 1000); scaleIndex++; } return result.trim(); }; exports.toWords = toWords; /** * Converts a number to Indonesian words */ const toIndonesianWords = (num) => { if (num === 0) return 'nol'; if (num < 0) return 'negatif ' + (0, exports.toIndonesianWords)(Math.abs(num)); const ones = ['', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'delapan', 'sembilan']; const teens = ['sepuluh', 'sebelas', 'dua belas', 'tiga belas', 'empat belas', 'lima belas', 'enam belas', 'tujuh belas', 'delapan belas', 'sembilan belas']; const tens = ['', '', 'dua puluh', 'tiga puluh', 'empat puluh', 'lima puluh', 'enam puluh', 'tujuh puluh', 'delapan puluh', 'sembilan puluh']; const scales = ['', 'ribu', 'juta', 'miliar', 'triliun']; const convertLessThanOneThousand = (n) => { if (n === 0) return ''; if (n < 10) return ones[n]; if (n < 20) return teens[n - 10]; if (n < 100) return tens[Math.floor(n / 10)] + (n % 10 !== 0 ? ' ' + ones[n % 10] : ''); if (n < 1000) { if (Math.floor(n / 100) === 1) { return 'seratus' + (n % 100 !== 0 ? ' ' + convertLessThanOneThousand(n % 100) : ''); } return ones[Math.floor(n / 100)] + ' ratus' + (n % 100 !== 0 ? ' ' + convertLessThanOneThousand(n % 100) : ''); } return ''; }; let result = ''; let scaleIndex = 0; while (num > 0) { const chunk = num % 1000; if (chunk !== 0) { const chunkWords = convertLessThanOneThousand(chunk); if (scaleIndex === 1 && chunk === 1) { result = 'seribu' + (result ? ' ' + result : ''); } else { result = chunkWords + (scales[scaleIndex] ? ' ' + scales[scaleIndex] : '') + (result ? ' ' + result : ''); } } num = Math.floor(num / 1000); scaleIndex++; } return result.trim(); }; exports.toIndonesianWords = toIndonesianWords; /** * Converts a number to binary string */ const toBinary = (num) => { return num.toString(2); }; exports.toBinary = toBinary; /** * Converts a number to hexadecimal string */ const toHex = (num) => { return num.toString(16); }; exports.toHex = toHex; /** * Converts a number to octal string */ const toOctal = (num) => { return num.toString(8); }; exports.toOctal = toOctal; /** * Converts a binary string to number */ const fromBinary = (binary) => { return parseInt(binary, 2); }; exports.fromBinary = fromBinary; /** * Converts a hexadecimal string to number */ const fromHex = (hex) => { return parseInt(hex, 16); }; exports.fromHex = fromHex; /** * Converts an octal string to number */ const fromOctal = (octal) => { return parseInt(octal, 8); }; exports.fromOctal = fromOctal; /** * Checks if a number is a perfect square */ const isPerfectSquare = (num) => { if (num < 0) return false; const sqrt = Math.sqrt(num); return Math.floor(sqrt) === sqrt; }; exports.isPerfectSquare = isPerfectSquare; /** * Checks if a number is a perfect cube */ const isPerfectCube = (num) => { const cbrt = Math.cbrt(num); return Math.floor(cbrt) === cbrt; }; exports.isPerfectCube = isPerfectCube; /** * Checks if a number is a power of 2 */ const isPowerOf2 = (num) => { return num > 0 && (num & (num - 1)) === 0; }; exports.isPowerOf2 = isPowerOf2; /** * Checks if a number is a power of 10 */ const isPowerOf10 = (num) => { if (num <= 0) return false; while (num % 10 === 0) { num /= 10; } return num === 1; }; exports.isPowerOf10 = isPowerOf10; /** * Gets the factorial of a number */ const factorial = (num) => { if (num < 0) return NaN; if (num === 0 || num === 1) return 1; let result = 1; for (let i = 2; i <= num; i++) { result *= i; } return result; }; exports.factorial = factorial; /** * Gets the greatest common divisor of two numbers */ const gcd = (a, b) => { while (b !== 0) { [a, b] = [b, a % b]; } return Math.abs(a); }; exports.gcd = gcd; /** * Gets the least common multiple of two numbers */ const lcm = (a, b) => { return Math.abs(a * b) / (0, exports.gcd)(a, b); }; exports.lcm = lcm; /** * Gets the Fibonacci number at a given index */ const fibonacci = (n) => { if (n <= 0) return 0; if (n === 1) return 1; let a = 0, b = 1; for (let i = 2; i <= n; i++) { [a, b] = [b, a + b]; } return b; }; exports.fibonacci = fibonacci; /** * Gets the nth prime number */ const nthPrime = (n) => { if (n <= 0) return 0; if (n === 1) return 2; let count = 1; let num = 3; while (count < n) { if (isPrime(num)) { count++; } num += 2; } return num - 2; }; exports.nthPrime = nthPrime; /** * Helper function to check if a number is prime */ const isPrime = (num) => { if (num < 2) return false; if (num === 2) return true; if (num % 2 === 0) return false; for (let i = 3; i <= Math.sqrt(num); i += 2) { if (num % i === 0) return false; } return true; }; /** * Gets the sum of digits of a number */ const sumOfDigits = (num) => { return Math.abs(num).toString().split('').reduce((sum, digit) => sum + parseInt(digit), 0); }; exports.sumOfDigits = sumOfDigits; /** * Gets the product of digits of a number */ const productOfDigits = (num) => { return Math.abs(num).toString().split('').reduce((product, digit) => product * parseInt(digit), 1); }; exports.productOfDigits = productOfDigits; /** * Reverses the digits of a number */ const reverseDigits = (num) => { const reversed = Math.abs(num).toString().split('').reverse().join(''); return parseInt(reversed) * Math.sign(num); }; exports.reverseDigits = reverseDigits; /** * Checks if a number is a palindrome */ const isPalindrome = (num) => { return num === (0, exports.reverseDigits)(num); }; exports.isPalindrome = isPalindrome; /** * Gets the number of digits in a number */ const digitCount = (num) => { return Math.abs(num).toString().length; }; exports.digitCount = digitCount; /** * Gets the first digit of a number */ const firstDigit = (num) => { return parseInt(Math.abs(num).toString()[0]); }; exports.firstDigit = firstDigit; /** * Gets the last digit of a number */ const lastDigit = (num) => { return Math.abs(num) % 10; }; exports.lastDigit = lastDigit;