Function joinToString

  • Transforms 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.

    Type Parameters

    • T

      The type of the elements in the input array.

    Parameters

    • array: Iterable<T, any, any>

      The array to join elements from.

    • selector: ((el: T) => string)

      The function to transform elements to strings.

        • (el): string
        • Parameters

          • el: T

          Returns string

    • options: Readonly<JoinToStringOptions> = {}

      The options to configure the joining.

    Returns string

    The resulting string.

    import { joinToString } from "@std/collections/join-to-string";
    import { assertEquals } from "@std/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");