joinToString
import { joinToString } from "jsr:@ende/collections";function
function joinToString<T>(array: Iterable<T>, selector: (el: T, index: number) => string, options: Readonly<JoinToStringOptions> = [UNSUPPORTED]): stringTransforms the elements in the given array to strings using the given
selector. Joins the produced strings into one using the given separator
and applying the given prefix and suffix to the whole string afterwards.
If the array could be huge, you can specify a non-negative value of limit,
in which case only the first limit elements will be appended, followed by
the truncated string.
array: Iterable<T>The array to join elements from.
selector: (el: T, index: number) => stringThe function to transform elements to strings. The function receives the element and its index.
options: Readonly<JoinToStringOptions> = [UNSUPPORTED]The options to configure the joining.
Returns
The resulting string.
Example: Usage with options
import { joinToString } from "@ende/collections/join-to-string";
import { assertEquals } from "@ende/assert";
const users = [
{ name: "Kim" },
{ name: "Anna" },
{ name: "Tim" },
];
const message = joinToString(users, (user) => user.name, {
suffix: " are winners",
prefix: "result: ",
separator: " and ",
limit: 1,
truncated: "others",
});
assertEquals(message, "result: Kim and others are winners");
Example: Using the index parameter
import { joinToString } from "@ende/collections/join-to-string";
import { assertEquals } from "@ende/assert";
const names = ["Kim", "Anna", "Tim"];
const result = joinToString(names, (name, index) => `${index}:${name}`);
assertEquals(result, "0:Kim,1:Anna,2:Tim");