// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Options for {@linkcode 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.
 *
 * @template T The type of the elements in the input array.
 *
 * @param array The array to join elements from.
 * @param selector The function to transform elements to strings.
 * @param options The options to configure the joining.
 *
 * @returns The resulting string.
 *
 * @example Usage with options
 * ```ts
 * import { joinToString } from "@std/collections/join-to-string";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * 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");
 * ```
 */ export function joinToString(array, selector, options = {}) {
  const { separator = ",", prefix = "", suffix = "", limit = -1, truncated = "..." } = options;
  let result = "";
  let index = 0;
  for (const el of array){
    if (index > 0) {
      result += separator;
    }
    if (limit >= 0 && index >= limit) {
      result += truncated;
      break;
    }
    result += selector(el);
    index++;
  }
  return prefix + result + suffix;
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 4500 characters]
// denoCacheMetadata=18309019104293470371,17224497081783112878