common.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Returns a fresh copy of the passed in value
  3. *
  4. * @param {unknown} toClone A value to clone
  5. * @returns {unknown} A clone of the input
  6. */
  7. function DeepClone(toClone) {
  8. return JSON.parse(JSON.stringify(toClone));
  9. }
  10. /**
  11. * Check whether the value at a specific spot in a number is non-zero
  12. *
  13. * Similar to the bitwise & operator, checks a "place" in a decimal number
  14. * to see if it has a non-zero value.
  15. *
  16. * @example
  17. * const test = 107;
  18. * console.log(DecimalPlaceIsNonZero(test, 1)) // true
  19. * console.log(DecimalPlaceIsNonZero(test, 2)) // false
  20. * console.log(DecimalPlaceIsNonZero(test, 3)) // true
  21. *
  22. * @param {number} input The number to check
  23. * @param {number} place The power of 10 to check against
  24. * @returns {boolean} Whether the value in that number's place is non-zero
  25. */
  26. function DecimalPlaceIsNonZero(input, place) {
  27. return !!GetDecimalInPlace(input, place);
  28. }
  29. /**
  30. * Get the digit in a given place of a decimal number
  31. *
  32. * @param {number} input The number to check
  33. * @param {number} place The power of 10 to check against
  34. * @returns {number} The number at the specified place in the input
  35. */
  36. function GetDecimalInPlace(input, place) {
  37. return Math.floor((input / (10 ** place)) % 10);
  38. }
  39. module.exports = {
  40. DeepClone: DeepClone,
  41. DecimalPlaceIsNonZero: DecimalPlaceIsNonZero,
  42. GetDecimalInPlace: GetDecimalInPlace,
  43. };