Skip to content

Commit c13e890

Browse files
committed
ref: autoPlural default values
1 parent c66324b commit c13e890

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

.changeset/yellow-fireants-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
`autoPlural()` now defaults `pluralType` to `"auto"` and `num` to 2 if `pluralType` is invalid or `num` resolves to NaN

lib/misc.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ import type { ListWithLength, Prettify, Stringifiable } from "./types.js";
99
export type PluralType = "auto" | "-s" | "-ies";
1010

1111
/**
12-
* Automatically appends an `s` to the passed {@linkcode word}, if {@linkcode num} is not equal to 1
13-
* @param word A word in singular form, to auto-convert to plural
14-
* @param num A number, or list-like value that has either a `length`, `count` or `size` property - does not support iterables
15-
* @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
12+
* Automatically pluralizes the given string an `-s` or `-ies` to the passed {@linkcode term}, if {@linkcode num} is not equal to 1.
13+
* By default, words ending in `-y` will have it replaced with `-ies`, and all other words will simply have `-s` appended.
14+
* 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.
15+
* @param term The term, written in singular form, to auto-convert to plural form
16+
* @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
17+
* @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
1618
*/
1719
export function autoPlural(word: Stringifiable, num: number | ListWithLength, pluralType: PluralType = "auto"): string {
1820
if(typeof num !== "number") {
19-
if(Array.isArray(num) || num instanceof NodeList)
21+
if("length" in num)
2022
num = num.length;
21-
else if("length" in num)
22-
num = num.length;
23-
else if("count" in num)
24-
num = num.count;
2523
else if("size" in num)
2624
num = num.size;
25+
else if("count" in num)
26+
num = num.count;
2727
}
2828

2929
const pType: Exclude<PluralType, "auto"> = pluralType === "auto"
3030
? String(word).endsWith("y") ? "-ies" : "-s"
3131
: pluralType;
3232

33-
// return `${word}${num === 1 ? "" : "s"}`;
33+
if(!["-s", "-ies"].includes(pType) || isNaN(num))
34+
num = 2;
35+
3436
switch(pType) {
3537
case "-s":
3638
return `${word}${num === 1 ? "" : "s"}`;

lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ export type Prettify<T> = {
4646
} & {};
4747

4848
/** Any value that is list-like, i.e. has a numeric length, count or size property */
49-
export type ListWithLength = unknown[] | NodeList | { length: number } | { count: number } | { size: number };
49+
export type ListWithLength = { length: number } | { count: number } | { size: number };

0 commit comments

Comments
 (0)