Bläddra i källkod

feat: utility type Stringifiable

Sven 1 år sedan
förälder
incheckning
84b37f1d96
2 ändrade filer med 9 tillägg och 1 borttagningar
  1. 5 0
      .changeset/forty-geese-mate.md
  2. 4 1
      lib/misc.ts

+ 5 - 0
.changeset/forty-geese-mate.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Add utility type Stringifiable to describe a string or any value that can be converted to one

+ 4 - 1
lib/misc.ts

@@ -1,9 +1,12 @@
+/** Represents any value that is either a string itself or can be converted to one (implicitly or explicitly) because it has a toString() method */
+export type Stringifiable = string | { toString(): string };
+
 /**
  * Automatically appends an `s` to the passed `word`, if `num` is not equal to 1
  * @param word A word in singular form, to auto-convert to plural
  * @param num If this is an array or NodeList, the amount of items is used
  */
-export function autoPlural(word: string, num: number | unknown[] | NodeList) {
+export function autoPlural(word: Stringifiable, num: number | unknown[] | NodeList) {
   if(Array.isArray(num) || num instanceof NodeList)
     num = num.length;
   return `${word}${num === 1 ? "" : "s"}`;