math.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type { Stringifiable } from "./types.js";
  2. /** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
  3. export function clamp(value: number, min: number, max: number): number
  4. /** Ensures the passed {@linkcode value} always stays between 0 and {@linkcode max} */
  5. export function clamp(value: number, max: number): number
  6. /** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} - if `max` isn't given, it defaults to `min` and `min` defaults to 0 */
  7. export function clamp(value: number, min: number, max?: number): number {
  8. if(typeof max !== "number") {
  9. max = min;
  10. min = 0;
  11. }
  12. return Math.max(Math.min(value, max), min);
  13. }
  14. /**
  15. * Transforms the value parameter from the numerical range `range1min` to `range1max` to the numerical range `range2min` to `range2max`
  16. * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
  17. */
  18. export function mapRange(value: number, range1min: number, range1max: number, range2min: number, range2max: number): number;
  19. /**
  20. * Transforms the value parameter from the numerical range `0` to `range1max` to the numerical range `0` to `range2max`
  21. * For example, you can map the value 2 in the range of 0-5 to the range of 0-10 and you'd get a 4 as a result.
  22. */
  23. export function mapRange(value: number, range1max: number, range2max: number): number;
  24. export function mapRange(value: number, range1min: number, range1max: number, range2min?: number, range2max?: number): number {
  25. if(typeof range2min === "undefined" || typeof range2max === "undefined") {
  26. range2max = range1max;
  27. range1max = range1min;
  28. range2min = range1min = 0;
  29. }
  30. if(Number(range1min) === 0.0 && Number(range2min) === 0.0)
  31. return value * (range2max / range1max);
  32. return (value - range1min) * ((range2max - range2min) / (range1max - range1min)) + range2min;
  33. }
  34. /**
  35. * Returns a random number between {@linkcode min} and {@linkcode max} (inclusive)
  36. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  37. */
  38. export function randRange(min: number, max: number, enhancedEntropy?: boolean): number
  39. /**
  40. * Returns a random number between 0 and {@linkcode max} (inclusive)
  41. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  42. */
  43. export function randRange(max: number, enhancedEntropy?: boolean): number
  44. /**
  45. * Returns a random number between {@linkcode min} and {@linkcode max} (inclusive)
  46. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  47. */
  48. export function randRange(...args: (number | boolean | undefined)[]): number {
  49. let min: number, max: number, enhancedEntropy = false;
  50. // using randRange(min, max)
  51. if(typeof args[0] === "number" && typeof args[1] === "number")
  52. [ min, max ] = args;
  53. // using randRange(max)
  54. else if(typeof args[0] === "number" && typeof args[1] !== "number") {
  55. min = 0;
  56. [ max ] = args;
  57. }
  58. else
  59. throw new TypeError(`Wrong parameter(s) provided - expected (number, boolean|undefined) or (number, number, boolean|undefined) but got (${args.map(a => typeof a).join(", ")}) instead`);
  60. if(typeof args[2] === "boolean")
  61. enhancedEntropy = args[2];
  62. else if(typeof args[1] === "boolean")
  63. enhancedEntropy = args[1];
  64. min = Number(min);
  65. max = Number(max);
  66. if(isNaN(min) || isNaN(max))
  67. return NaN;
  68. if(min > max)
  69. throw new TypeError("Parameter \"min\" can't be bigger than \"max\"");
  70. if(enhancedEntropy) {
  71. // TODO:FIXME: doesn't work
  72. const uintArr = new Uint8Array(1);
  73. crypto.getRandomValues(uintArr);
  74. return Number(Array.from(
  75. uintArr,
  76. (v) => Math.round(mapRange(v, 0, 255, min, max)).toString(10).substring(0, 1),
  77. ).join(""));
  78. }
  79. else
  80. return Math.floor(Math.random() * (max - min + 1)) + min;
  81. }
  82. /** Calculates the amount of digits in the given number - the given number or string will be passed to the `Number()` constructor. Returns NaN if the number is invalid. */
  83. export function digitCount(num: number | Stringifiable): number {
  84. num = Number((!["string", "number"].includes(typeof num)) ? String(num) : num);
  85. if(typeof num === "number" && isNaN(num))
  86. return NaN;
  87. return num === 0
  88. ? 1
  89. : Math.floor(
  90. Math.log10(Math.abs(Number(num))) + 1
  91. );
  92. }