2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
2021-10-17 11:09:49 +00:00
|
|
|
|
2023-12-05 08:53:55 +00:00
|
|
|
/** Options for {@linkcode joinToString}. */
|
2021-10-17 11:09:49 +00:00
|
|
|
export type JoinToStringOptions = {
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a separator between the elements.
|
|
|
|
*
|
|
|
|
* @default {","}
|
|
|
|
*/
|
2021-10-17 11:09:49 +00:00
|
|
|
separator?: string;
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a prefix for the resulting string.
|
|
|
|
*
|
|
|
|
* @default {""}
|
|
|
|
*/
|
2021-10-17 11:09:49 +00:00
|
|
|
prefix?: string;
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a suffix for the resulting string.
|
|
|
|
*
|
|
|
|
* @default {""}
|
|
|
|
*/
|
2021-10-17 11:09:49 +00:00
|
|
|
suffix?: string;
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The maximum number of elements to append. If the value is negative, all
|
|
|
|
* elements will be appended, which is the default.
|
|
|
|
*
|
|
|
|
* @default {-1}
|
|
|
|
*/
|
2021-10-17 11:09:49 +00:00
|
|
|
limit?: number;
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a placeholder for the truncated elements.
|
|
|
|
*
|
|
|
|
* @default {"..."}
|
|
|
|
*/
|
2021-10-17 11:09:49 +00:00
|
|
|
truncated?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-11-25 11:40:23 +00:00
|
|
|
* 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.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* 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
|
2024-05-06 07:51:20 +00:00
|
|
|
* the `truncated` string.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the input array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2021-10-17 11:09:49 +00:00
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* @example Usage with options
|
2021-10-17 11:09:49 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { joinToString } from "@std/collections/join-to-string";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-10-17 11:09:49 +00:00
|
|
|
*
|
|
|
|
* const users = [
|
|
|
|
* { name: "Kim" },
|
|
|
|
* { name: "Anna" },
|
|
|
|
* { name: "Tim" },
|
|
|
|
* ];
|
|
|
|
*
|
2024-05-07 07:11:55 +00:00
|
|
|
* const message = joinToString(users, (user) => user.name, {
|
2021-10-17 11:09:49 +00:00
|
|
|
* suffix: " are winners",
|
|
|
|
* prefix: "result: ",
|
|
|
|
* separator: " and ",
|
|
|
|
* limit: 1,
|
|
|
|
* truncated: "others",
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* assertEquals(message, "result: Kim and others are winners");
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function joinToString<T>(
|
2023-05-30 23:48:21 +00:00
|
|
|
array: Iterable<T>,
|
2021-10-17 11:09:49 +00:00
|
|
|
selector: (el: T) => string,
|
2024-05-06 07:51:20 +00:00
|
|
|
options: Readonly<JoinToStringOptions> = {},
|
|
|
|
): string {
|
|
|
|
const {
|
2021-10-17 11:09:49 +00:00
|
|
|
separator = ",",
|
|
|
|
prefix = "",
|
|
|
|
suffix = "",
|
|
|
|
limit = -1,
|
|
|
|
truncated = "...",
|
2024-05-06 07:51:20 +00:00
|
|
|
} = options;
|
|
|
|
|
2021-10-17 11:09:49 +00:00
|
|
|
let result = "";
|
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
let index = 0;
|
2023-05-30 23:48:21 +00:00
|
|
|
for (const el of array) {
|
2021-10-17 11:09:49 +00:00
|
|
|
if (index > 0) {
|
|
|
|
result += separator;
|
|
|
|
}
|
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
if (limit >= 0 && index >= limit) {
|
2021-10-17 11:09:49 +00:00
|
|
|
result += truncated;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += selector(el);
|
2024-05-07 07:11:55 +00:00
|
|
|
index++;
|
2021-10-17 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
return prefix + result + suffix;
|
2021-10-17 11:09:49 +00:00
|
|
|
}
|