summaryrefslogtreecommitdiff
path: root/src/utils/iterator.ts
diff options
context:
space:
mode:
authorJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-08-05 18:50:37 +0100
committerJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-08-05 18:50:37 +0100
commit0af094770c4ebabc56ff761a8bd215bc397c0f7e (patch)
treea9ad669c8b84b4d13897732ed93ccfcbbeb2cb25 /src/utils/iterator.ts
parent84eef3f848c4efa18985a776021a58720744523a (diff)
refactor: reading page review
Signed-off-by: João Augusto Costa Branco Marado Torres <torres.dev@disroot.org>
Diffstat (limited to 'src/utils/iterator.ts')
-rw-r--r--src/utils/iterator.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utils/iterator.ts b/src/utils/iterator.ts
index fa58fc9..43437b6 100644
--- a/src/utils/iterator.ts
+++ b/src/utils/iterator.ts
@@ -50,3 +50,30 @@ export async function findMapAsync<T, R>(
return await tryNext(0);
}
+
+export function createRanges(nums: Iterable<number>): string[] {
+ const ns = new Set(nums).values().toArray().sort((
+ a,
+ b,
+ ) => a - b);
+
+ const result = [];
+ let start = ns[0];
+ let end = ns[0];
+
+ for (let i = 1; i <= ns.length; i++) {
+ if (ns[i] === end + 1) {
+ end = ns[i];
+ } else {
+ if (start === end) {
+ result.push(`${start}`);
+ } else {
+ result.push(`${start}-${end}`);
+ }
+ start = ns[i];
+ end = ns[i];
+ }
+ }
+
+ return result;
+}