Skip to content

Commit eb20132

Browse files
committed
feat: ListWithLength type and support ListWithLength in autoPlural()
1 parent 10a9b80 commit eb20132

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

.changeset/fifty-tigers-live.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+
Added the type `ListWithLength` to represent an array or object with a numeric `length`, `count` or `size` property.
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+
Added `autoPlural()` support for generic objects with a numeric `length`, `count` or `size` property.

README-summary.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ View the documentation of previous major releases:
9595
- [`Prettify`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#prettify) - expands a complex type into a more readable format while keeping functionality the same
9696
- [`ValueGen`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#valuegen) - a "generator" value that allows for super flexible value typing and declaration
9797
- [`StringGen`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#stringgen) - a "generator" string that allows for super flexible string typing and declaration, including enhanced support for unions
98+
- [`ListWithLength`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#listwithlength) - represents an array or object with a numeric `length`, `count` or `size` property
9899

99100
<br><br>
100101

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ View the documentation of previous major releases:
102102
- [`Prettify`](./docs.md#prettify) - expands a complex type into a more readable format while keeping functionality the same
103103
- [`ValueGen`](./docs.md#valuegen) - a "generator" value that allows for super flexible value typing and declaration
104104
- [`StringGen`](./docs.md#stringgen) - a "generator" string that allows for super flexible string typing and declaration, including enhanced support for unions
105+
- [`ListWithLength`](./docs.md#listwithlength) - represents an array or object with a numeric `length`, `count` or `size` property
105106

106107
<br><br>
107108

docs.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ For submitting bug reports or feature requests, please use the [GitHub issue tra
9191
- [`Prettify`](#prettify) - expands a complex type into a more readable format while keeping functionality the same
9292
- [`ValueGen`](#valuegen) - a "generator" value that allows for super flexible value typing and declaration
9393
- [`StringGen`](#stringgen) - a "generator" string that allows for super flexible string typing and declaration, including enhanced support for unions
94+
- [`ListWithLength`](#listwithlength) - represents an array or object with a numeric `length`, `count` or `size` property
9495

9596
<br><br>
9697

@@ -1930,13 +1931,14 @@ debouncedFunction.debouncer.on("change", (timeout, type) => {
19301931
### autoPlural()
19311932
Signature:
19321933
```ts
1933-
autoPlural(str: string, num: number | Array | NodeList): string
1934+
autoPlural(str: string, num: number | Array | NodeList | { length: number } | { count: number } | { size: number }): string
19341935
```
19351936

19361937
Crudely pluralizes a string by appending an `s` if the given number is not 1.
1937-
If an array or NodeList is passed, the amount of contained items will be used.
1938-
1939-
Of course some English words go from `-y` to `-ies`, in which case this function will not work.
1938+
If an array or NodeList or object with either a `length`, `count` or `size` property is passed, the amount of contained items will be used.
1939+
Iterables will not work until converted to an array (with `Array.from()` or `[...iterable]`).
1940+
1941+
Some English words go from `-y` to `-ies`. Using this function in that case will not work.
19401942

19411943
<details><summary><b>Example - click to view</b></summary>
19421944

@@ -3150,6 +3152,40 @@ Remember that [`Stringifiable`](#stringifiable) is a type that describes a value
31503152
Contrary to [`ValueGen`](#valuegen), this type allows for specifying a union of strings that the StringGen should yield, as long as it is loosely typed as just `string`.
31513153
Use it in the [`consumeStringGen()`](#consumestringgen) function to convert the given StringGen value to a plain string. Also refer to that function for an example.
31523154

3155+
<br>
3156+
3157+
### ListWithLength
3158+
Represents a value that is either an array, NodeList, or any other object that has a numeric `length`, `count` or `size` property.
3159+
Iterables are not included because they don't have a length property. They need to be converted to an array first using `Array.from()` or `[...iterable]`.
3160+
3161+
<details><summary><b>Example - click to view</b></summary>
3162+
3163+
```ts
3164+
import type { ListWithLength } from "@sv443-network/userutils";
3165+
3166+
function getSize(list: ListWithLength) {
3167+
let size = -1;
3168+
if("length" in list)
3169+
size = list.length;
3170+
else if("count" in list)
3171+
size = list.count;
3172+
else if("size" in list)
3173+
size = list.size;
3174+
3175+
return size;
3176+
}
3177+
3178+
getSize([1, 2, 3]); // 3
3179+
getSize(document.querySelectorAll("div")); // 5
3180+
getSize(new Map([["a", 1], ["b", 2]])); // 2
3181+
getSize({ count: 42 }); // 42
3182+
3183+
// iterables need to be converted:
3184+
const iter = new Map([["a", 1]]).entries();
3185+
getSize([...iter]); // 1
3186+
```
3187+
</details>
3188+
31533189
<br><br><br><br>
31543190

31553191
<!-- #region Footer -->

lib/misc.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55

66
import type { Prettify, Stringifiable } from "./types.js";
77

8+
/** Any value that is list-like, i.e. has a numeric length, count or size property */
9+
export type ListWithLength = unknown[] | NodeList | { length: number } | { count: number } | { size: number };
10+
811
/**
912
* Automatically appends an `s` to the passed {@linkcode word}, if {@linkcode num} is not equal to 1
1013
* @param word A word in singular form, to auto-convert to plural
11-
* @param num If this is an array or NodeList, the amount of items is used
14+
* @param num A number, or list-like value that has either a `length`, `count` or `size` property - does not support iterables
1215
*/
13-
export function autoPlural(word: Stringifiable, num: number | unknown[] | NodeList): string {
14-
if(Array.isArray(num) || num instanceof NodeList)
15-
num = num.length;
16+
export function autoPlural(word: Stringifiable, num: number | ListWithLength): string {
17+
if(typeof num !== "number") {
18+
if(Array.isArray(num) || num instanceof NodeList)
19+
num = num.length;
20+
else if("length" in num)
21+
num = num.length;
22+
else if("count" in num)
23+
num = num.count;
24+
else if("size" in num)
25+
num = num.size;
26+
}
1627
return `${word}${num === 1 ? "" : "s"}`;
1728
}
1829

0 commit comments

Comments
 (0)