Преглед изворни кода

ref: improve autoPlural and its docs again

Sv443 пре 2 месеци
родитељ
комит
628fa7edf3
2 измењених фајлова са 23 додато и 19 уклоњено
  1. 10 9
      docs.md
  2. 13 10
      lib/misc.ts

+ 10 - 9
docs.md

@@ -1932,7 +1932,7 @@ debouncedFunction.debouncer.on("change", (timeout, type) => {
 Signature:  
 ```ts
 autoPlural(
-  str: string,
+  term: string,
   num: number | Array | NodeList | { length: number } | { count: number } | { size: number },
   pluralType?: "auto" | "-s" | "-ies"
 ): string
@@ -1952,21 +1952,22 @@ If set to anything else, the word will be returned as-is.
 ```ts
 import { autoPlural } from "@sv443-network/userutils";
 
-autoPlural("item", 0); // "items"
-autoPlural("item", 1); // "item"
-autoPlural("item", 2); // "items"
+autoPlural("red apple", 0); // "red apples"
+autoPlural("red apple", 1); // "red apple"
+autoPlural("red apple", 2); // "red apples"
 
+// The default `pluralType` ("auto") switches suffix when the word ends with y:
+autoPlural("category", 1); // "category"
+autoPlural("category", 2); // "categories"
+
+// The passed `num` object just needs to have a numeric length, count or size property:
 autoPlural("element", document.querySelectorAll("html")); // "element"
 autoPlural("element", document.querySelectorAll("*"));    // "elements"
 
 const items = [1, 2, 3, 4, "foo", "bar"];
 console.log(items.length, autoPlural("item", items)); // "6 items"
 
-// pluralType = "auto" switches pluralization suffix when the word ends with -y:
-autoPlural("category", 1); // "category"
-autoPlural("category", 2); // "categories"
-
-// can also be forced to pluralize with one or the other:
+// And you can also force pluralization with one or the other if needed:
 autoPlural("category", 1, "-s"); // "category"
 autoPlural("category", 2, "-s"); // "categorys"
 autoPlural("apple", 1, "-ies");  // "apply"

+ 13 - 10
lib/misc.ts

@@ -11,12 +11,12 @@ export type PluralType = "auto" | "-s" | "-ies";
 /**
  * Automatically pluralizes the given string an `-s` or `-ies` to the passed {@linkcode term}, if {@linkcode num} is not equal to 1.  
  * By default, words ending in `-y` will have it replaced with `-ies`, and all other words will simply have `-s` appended.  
- * If {@linkcode num} resolves to NaN or the {@linkcode pluralType} is wrong, it defaults to the {@linkcode pluralType} `auto` and sets {@linkcode num} to 2.
+ * {@linkcode pluralType} will default to `auto` if invalid and {@linkcode num} is set to 2 if it resolves to `NaN`.
  * @param term The term, written in singular form, to auto-convert to plural form
- * @param num A number, or list-like value that has either a `length`, `count` or `size` property, like an array, Map or discord.js Collection - does not support iterables, they need to be converted to an array first
+ * @param num A number, or list-like value that has either a `length`, `count` or `size` property, like an array, Map or NodeList - does not support iterables, they need to be converted to an array first
  * @param pluralType Which plural form to use when auto-pluralizing. Defaults to `"auto"`, which removes the last char and uses `-ies` for words ending in `y` and simply appends `-s` for all other words
  */
-export function autoPlural(word: Stringifiable, num: number | ListWithLength, pluralType: PluralType = "auto"): string {
+export function autoPlural(term: Stringifiable, num: number | ListWithLength, pluralType: PluralType = "auto"): string {
   if(typeof num !== "number") {
     if("length" in num)
       num = num.length;
@@ -26,20 +26,23 @@ export function autoPlural(word: Stringifiable, num: number | ListWithLength, pl
       num = num.count;
   }
 
-  const pType: Exclude<PluralType, "auto"> = pluralType === "auto"
-    ? String(word).endsWith("y") ? "-ies" : "-s"
-    : pluralType;
+  if(!["-s", "-ies"].includes(pluralType))
+    pluralType = "auto";
 
-  if(!["-s", "-ies"].includes(pType) || isNaN(num))
+  if(isNaN(num))
     num = 2;
 
+  const pType: Exclude<PluralType, "auto"> = pluralType === "auto"
+    ? String(term).endsWith("y") ? "-ies" : "-s"
+    : pluralType;
+
   switch(pType) {
   case "-s":
-    return `${word}${num === 1 ? "" : "s"}`;
+    return `${term}${num === 1 ? "" : "s"}`;
   case "-ies":
-    return `${String(word).slice(0, -1)}${num === 1 ? "y" : "ies"}`;
+    return `${String(term).slice(0, -1)}${num === 1 ? "y" : "ies"}`;
   default:
-    return String(word);
+    return String(term);
   }
 }