UNPKG

quarkle

Version:

quarkle is the JavaScript util library providing support of all data types and data structures.

208 lines (207 loc) 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.changeBase = exports.combinations = exports.permutations = exports.factorial = exports.isNumberPalindrome = exports.isPrime = exports.reverseNumber = void 0; /** * Returns the reverse of the given number. * @param number * @returns the reversed number. */ const reverseNumber = (number) => { let reverse = 0; const isNegative = number < 0; if (isNegative) { number *= -1; } while (number !== 0) { const remainder = number % 10; reverse = reverse * 10 + remainder; number = Math.floor(number / 10); } if (isNegative) { reverse *= -1; } return reverse; }; exports.reverseNumber = reverseNumber; /** * Checks if the number is prime or not. * @param number * @returns 'true' if number is prime, otherwise 'false'. */ const isPrime = (number) => { if (number < 2) { return false; } let i = 2; while (i * i < number) { if (number % i === 0) { return false; } i++; } return true; }; exports.isPrime = isPrime; /** * Checks if the given number is palindrome or not. * @param number * @returns 'true' if number is palindrome, otherwise 'false'. */ const isNumberPalindrome = (number) => { if (number < 0) { return false; } return (0, exports.reverseNumber)(number) === number; }; exports.isNumberPalindrome = isNumberPalindrome; /** * Returns the factorial of the given number. * @param number * @returns The factorial of the number. */ const factorial = (number) => { if (number < 0) { throw new Error("Factorial of the negative number cannot be defined."); } if (number === 0) { return 1; } else { return number * (0, exports.factorial)(number - 1); } }; exports.factorial = factorial; /** * Returns the number of ways `k` items can be arranged from a set of `n` items. The order of the arrangement matters here. * @param n The set of items. * @param k The number of items out of `n` which needs to be arranged. * @returns The possible number of arrangements formed by selecting `k` items from a set of `n` items. */ const permutations = (n, k) => { if (k > n) { throw new Error("Value of `k` must be smaller than or equal to `n`."); } return (0, exports.factorial)(n) / (0, exports.factorial)(n - k); }; exports.permutations = permutations; /** * Returns the number of ways `k` items can be selected from a set of `n` items. The order of the selection doesn't matter here. * @param n The set of items. * @param k The number of items out of `n` which needs to be selected. * @returns The possible ways of selecting `k` items from a set of `n` items. */ const combinations = (n, k) => { if (k > n) { throw new Error("Value of `k` must be smaller than or equal to `n`."); } return (0, exports.factorial)(n) / ((0, exports.factorial)(k) * (0, exports.factorial)(n - k)); }; exports.combinations = combinations; const decimalChangeBase = (n, base) => { let baseNum = 10; switch (base) { case "binary": baseNum = 2; break; case "octal": baseNum = 8; break; case "hexadecimal": baseNum = 16; break; } return n.toString(baseNum); }; const binaryChangeBase = (n, base) => { let result = -1; switch (base) { case "decimal": result = parseInt(n, 2); break; case "octal": result = parseInt(n, 2).toString(8); break; case "hexadecimal": result = parseInt(n, 2).toString(16); break; } return result; }; const octalChangeBase = (n, base) => { let result = -1; switch (base) { case "decimal": result = parseInt(n, 8); break; case "binary": result = parseInt(n, 8).toString(2); break; case "hexadecimal": result = parseInt(n, 8).toString(16); break; } return result; }; const hexadecimalChangeBase = (n, base) => { let result = -1; switch (base) { case "decimal": result = parseInt(n, 16); break; case "binary": result = parseInt(n, 16).toString(2); break; case "octal": result = parseInt(n, 16).toString(8); break; } return result; }; /** * Changes the base of the given number or string to the other base. * @param number This can be a decimal number or binary/octal/hexadecimal string. * @param sourceBase The base of the given number. * @param targetBase The base which the given number should get converted to. * @returns The converted number or string. */ const changeBase = (number, sourceBase, targetBase) => { if (sourceBase === targetBase) { return number; } if (sourceBase === "decimal" && targetBase !== "decimal" && typeof number === "number") { return decimalChangeBase(number, targetBase); } else if (sourceBase === "binary" && targetBase !== "binary" && typeof number === "string") { if (!/^[01]+$/.test(number)) { throw new Error("The given string is not a valid binary. It should contain only 0s and 1s."); } else { return binaryChangeBase(number, targetBase); } } else if (sourceBase === "octal" && targetBase !== "octal" && typeof number === "string") { if (!/^[0-7]+$/.test(number)) { throw new Error("The given string is not a valid octal. It should contain numbers in the range [0-7]."); } else { return octalChangeBase(number, targetBase); } } else if (sourceBase === "hexadecimal" && targetBase !== "hexadecimal" && typeof number === "string") { if (!/^[0-9a-fA-F]+$/.test(number)) { throw new Error("The given string is not a valid hexadecimal. It should contain numbers in the range [0-9] and alphabets in the range [a-f] or [A-F]."); } else { return hexadecimalChangeBase(number, targetBase); } } }; exports.changeBase = changeBase;