Settings: Calculate dependencies

This commit is contained in:
Vendicated 2022-09-01 21:41:00 +02:00
parent 78deb0ebad
commit 9951e0bcc5
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
2 changed files with 86 additions and 31 deletions

View file

@ -69,4 +69,32 @@ export function mergeDefaults<T>(obj: T, defaults: T): T {
}
}
return obj;
}
}
/**
* Join an array of strings in a human readable way (1, 2 and 3)
* @param elements Elements
*/
export function humanFriendlyJoin(elements: string[]): string;
/**
* Join an array of strings in a human readable way (1, 2 and 3)
* @param elements Elements
* @param mapper Function that converts elements to a string
*/
export function humanFriendlyJoin<T>(elements: T[], mapper: (e: T) => string): string;
export function humanFriendlyJoin(elements: any[], mapper: (e: any) => string = s => s): string {
const { length } = elements;
if (length === 0) return "";
if (length === 1) return mapper(elements[0]);
let s = "";
for (let i = 0; i < length; i++) {
s += mapper(elements[i]);
if (length - i > 2) s += ", ";
else if (length - i > 1) s += " and ";
}
return s;
}