瀏覽代碼

feat: `digitCount()` function

Sv443 4 月之前
父節點
當前提交
e10d62956a
共有 4 個文件被更改,包括 50 次插入0 次删除
  1. 5 0
      .changeset/kind-badgers-study.md
  2. 1 0
      README-summary.md
  3. 35 0
      README.md
  4. 9 0
      lib/math.ts

+ 5 - 0
.changeset/kind-badgers-study.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Added function `digitCount()` to calculate the amount of digits in the passed number

+ 1 - 0
README-summary.md

@@ -49,6 +49,7 @@ or view the documentation of previous major releases:
     - [`clamp()`](https://github.com/Sv443-Network/UserUtils#clamp) - constrain a number between a min and max value
     - [`mapRange()`](https://github.com/Sv443-Network/UserUtils#maprange) - map a number from one range to the same spot in another range
     - [`randRange()`](https://github.com/Sv443-Network/UserUtils#randrange) - generate a random number between a min and max boundary
+    - [`digitCount()`](https://github.com/Sv443-Network/UserUtils#digitcount) - calculate the amount of digits in a number
 - **Misc:**
     - [`DataStore`](https://github.com/Sv443-Network/UserUtils#datastore) - class that manages a hybrid sync & async persistent JSON database, including data migration
     - [`DataStoreSerializer`](https://github.com/Sv443-Network/UserUtils#datastoreserializer) - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations

+ 35 - 0
README.md

@@ -52,6 +52,7 @@ View the documentation of previous major releases:
     - [`clamp()`](#clamp) - constrain a number between a min and max value
     - [`mapRange()`](#maprange) - map a number from one range to the same spot in another range
     - [`randRange()`](#randrange) - generate a random number between a min and max boundary
+    - [`digitCount()`](#digitcount) - calculate the amount of digits in a number
   - [**Misc:**](#misc)
     - [`DataStore`](#datastore) - class that manages a hybrid sync & async persistent JSON database, including data migration
     - [`DataStoreSerializer`](#datastoreserializer) - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations
@@ -1046,6 +1047,40 @@ benchmark(true);  // Generated 100k in 461ms
 ```
 </details>
 
+<br>
+
+### digitCount()
+Usage:  
+```ts
+digitCount(num: number | string): number
+```
+  
+Calculates and returns the amount of digits in the given number.  
+The number or string will be passed to the `Number()` constructor before the calculation.  
+The function returns `NaN` if the number is invalid.  
+  
+<details><summary><b>Example - click to view</b></summary>
+
+```ts
+import { digitCount } from "@sv443-network/userutils";
+
+const num1 = 123;
+const num2 = 123456789;
+const num3 = "  123456789    ";
+const num4 = Number.MAX_SAFE_INTEGER;
+const num5 = "a123b456c789d";
+const num6 = parseInt("0x123456789abcdef", 16);
+
+digitCount(num1); // 3
+digitCount(num2); // 9
+digitCount(num3); // 9
+digitCount(num4); // 16
+digitCount(num5); // NaN (because hex conversion has to be done through parseInt(str, 16)), like below:
+digitCount(num6); // 17
+```
+
+</details>
+
 <br><br>
 
 <!-- #region Misc -->

+ 9 - 0
lib/math.ts

@@ -81,3 +81,12 @@ export function randRange(...args: (number | boolean | undefined)[]): number {
   else
     return Math.floor(Math.random() * (max - min + 1)) + min;
 }
+
+/** 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 {
+  return num === 0
+    ? 1
+    : Math.floor(
+      Math.log10(Math.abs(Number(num))) + 1
+    );
+}