math.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
  2. export function clamp(value: number, min: number, max: number): number
  3. /** Ensures the passed {@linkcode value} always stays between 0 and {@linkcode max} */
  4. export function clamp(value: number, max: number): number
  5. /** 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 */
  6. export function clamp(value: number, min: number, max?: number): number {
  7. if(typeof max !== "number") {
  8. max = min;
  9. min = 0;
  10. }
  11. return Math.max(Math.min(value, max), min);
  12. }
  13. /**
  14. * Transforms the value parameter from the numerical range `range1min` to `range1max` to the numerical range `range2min` to `range2max`
  15. * 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.
  16. */
  17. export function mapRange(value: number, range1min: number, range1max: number, range2min: number, range2max: number): number;
  18. /**
  19. * Transforms the value parameter from the numerical range `0` to `range1max` to the numerical range `0` to `range2max`
  20. * 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.
  21. */
  22. export function mapRange(value: number, range1max: number, range2max: number): number;
  23. export function mapRange(value: number, range1min: number, range1max: number, range2min?: number, range2max?: number): number {
  24. if(typeof range2min === "undefined" || typeof range2max === "undefined") {
  25. range2max = range1max;
  26. range1max = range1min;
  27. range2min = range1min = 0;
  28. }
  29. if(Number(range1min) === 0.0 && Number(range2min) === 0.0)
  30. return value * (range2max / range1max);
  31. return (value - range1min) * ((range2max - range2min) / (range1max - range1min)) + range2min;
  32. }
  33. /**
  34. * Returns a random number between {@linkcode min} and {@linkcode max} (inclusive)
  35. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  36. */
  37. export function randRange(min: number, max: number, enhancedEntropy?: boolean): number
  38. /**
  39. * Returns a random number between 0 and {@linkcode max} (inclusive)
  40. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  41. */
  42. export function randRange(max: number, enhancedEntropy?: boolean): number
  43. /**
  44. * Returns a random number between {@linkcode min} and {@linkcode max} (inclusive)
  45. * Set {@linkcode enhancedEntropy} to true to use `crypto.getRandomValues()` for better cryptographic randomness (this also makes it take longer to generate)
  46. */
  47. export function randRange(...args: (number | boolean | undefined)[]): number {
  48. let min: number, max: number, enhancedEntropy = false;
  49. // using randRange(min, max)
  50. if(typeof args[0] === "number" && typeof args[1] === "number")
  51. [ min, max ] = args;
  52. // using randRange(max)
  53. else if(typeof args[0] === "number" && typeof args[1] !== "number") {
  54. min = 0;
  55. [ max ] = args;
  56. }
  57. else
  58. 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`);
  59. if(typeof args[2] === "boolean")
  60. enhancedEntropy = args[2];
  61. else if(typeof args[1] === "boolean")
  62. enhancedEntropy = args[1];
  63. min = Number(min);
  64. max = Number(max);
  65. if(isNaN(min) || isNaN(max))
  66. return NaN;
  67. if(min > max)
  68. throw new TypeError("Parameter \"min\" can't be bigger than \"max\"");
  69. if(enhancedEntropy) {
  70. const uintArr = new Uint8Array(1);
  71. crypto.getRandomValues(uintArr);
  72. return Number(Array.from(
  73. uintArr,
  74. (v) => Math.round(mapRange(v, 0, 255, min, max)).toString(10).substring(0, 1),
  75. ).join(""));
  76. }
  77. else
  78. return Math.floor(Math.random() * (max - min + 1)) + min;
  79. }
  80. /** 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. */
  81. export function digitCount(num: number | string): number {
  82. return num === 0
  83. ? 1
  84. : Math.floor(
  85. Math.log10(Math.abs(Number(num))) + 1
  86. );
  87. }