Skip to content

Commit

Permalink
feat: allow Stringifiable value in digitCount
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Dec 22, 2024
1 parent 294d28e commit 0971e0e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/math.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Stringifiable } from "./types.js";

/** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
export function clamp(value: number, min: number, max: number): number
/** Ensures the passed {@linkcode value} always stays between 0 and {@linkcode max} */
Expand Down Expand Up @@ -89,7 +91,12 @@ export function randRange(...args: (number | boolean | undefined)[]): number {
}

/** 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. */
export function digitCount(num: number | string): number {
export function digitCount(num: number | Stringifiable): number {
num = Number((!["string", "number"].includes(typeof num)) ? String(num) : num);

if(typeof num === "number" && isNaN(num))
return NaN;

return num === 0
? 1
: Math.floor(
Expand Down

0 comments on commit 0971e0e

Please sign in to comment.