chore(server): Use ChunkedSet in Access repository (#6943)

This change simplifies the Access repository methods, by using the
`ChunkedSet` decorator. As the methods expect sets, the `chunks` util
needed to be fixed, so it returns chunks of the same type it received.

Now `chunks` is overloaded, to have proper typing based on the input
parameter.
This commit is contained in:
Michael Manganiello
2024-02-06 17:03:25 -05:00
committed by GitHub
parent 61768ce89e
commit 4164bcfd0d
2 changed files with 233 additions and 282 deletions

View File

@@ -178,23 +178,25 @@ export function Optional({ nullable, ...validationOptions }: OptionalOptions = {
}
/**
* Chunks an array or set into smaller arrays of the specified size.
* Chunks an array or set into smaller collections of the same type and specified size.
*
* @param collection The collection to chunk.
* @param size The size of each chunk.
*/
export function chunks<T>(collection: Array<T> | Set<T>, size: number): T[][] {
export function chunks<T>(collection: Array<T>, size: number): Array<Array<T>>;
export function chunks<T>(collection: Set<T>, size: number): Array<Set<T>>;
export function chunks<T>(collection: Array<T> | Set<T>, size: number): Array<Array<T>> | Array<Set<T>> {
if (collection instanceof Set) {
const result = [];
let chunk = [];
let chunk = new Set<T>();
for (const element of collection) {
chunk.push(element);
if (chunk.length === size) {
chunk.add(element);
if (chunk.size === size) {
result.push(chunk);
chunk = [];
chunk = new Set<T>();
}
}
if (chunk.length > 0) {
if (chunk.size > 0) {
result.push(chunk);
}
return result;