// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { format } from "./format.ts";
import type { Comparator, Range } from "./types.ts";

function formatComparator(comparator: Comparator): string {
  const { operator } = comparator;
  return `${operator === undefined ? "" : operator}${format(comparator)}`;
}

/**
 * Formats the range into a string
 * @example Usage
 * ```ts
 * import { formatRange, parseRange } from "@std/semver";
 * import { assertEquals } from "@std/assert";
 *
 * const range = parseRange(">=1.2.3 <1.2.4");
 * assertEquals(formatRange(range), ">=1.2.3 <1.2.4");
 * ```
 *
 * @param range The range to format
 * @returns A string representation of the range
 */
export function formatRange(range: Range): string {
  return range.map((c) => c.map((c) => formatComparator(c)).join(" "))
    .join("||");
}

// denoCacheMetadata={"headers":{"content-length":"895","accept-ranges":"bytes","x-jsr-backend":"modules","alt-svc":"h3=\":443\"; ma=86400","cf-cache-status":"HIT","content-type":"text/typescript","vary":"Origin","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","set-cookie":"__cf_bm=RjSydEp8vKQLWRxUFpAQqAf.aP7qyWcjCAvcwgZM_F4-1785241875.172154-1.0.1.1-fN6zlL8vCU4agQe7L0TUulPoOn8HM4H2Sxpqsr00bjRkZIR73x_b0Y3uGVcCSPzrZZzm_Ffx60tiTitLqqadFn0S2Gqht0n7PoC4tGtM1vuohvX9oPvHW5vWns5Z6hjL; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","age":"36703","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cf-ray":"a223e657ca947db6-LAX","server":"cloudflare","date":"Tue, 28 Jul 2026 12:31:15 GMT","x-robots-tag":"noindex","access-control-expose-headers":"*","cache-control":"public, max-age=31536000, immutable","cross-origin-resource-policy":"cross-origin","etag":"\"67307e8e09d6c5397cc988885166b8f4\"","access-control-allow-origin":"*","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=fnZyIWSPArAq3ypfIZyU%2FyY5IPh3CijJGlk008cOZhtsmNeOMMwZXuDc0mTSX5EZkBMqJlV2pIPKQsqgLcnWCpn0yHiyb0fSpRqv8ZYwMjE51REGN2iHv%2FE%3D\"}]}"},"url":"https://jsr.io/@std/semver/0.224.3/format_range.ts","time":1785241875}