// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/** End-of-line character for POSIX platforms such as macOS and Linux. */
export const LF = "\n" as const;

/** End-of-line character for Windows platforms. */
export const CRLF = "\r\n" as const;

/**
 * End-of-line character evaluated for the current platform.
 *
 * @example
 * ```ts
 * import { EOL } from "@std/fs/eol";
 *
 * EOL; // "\n" on POSIX platforms and "\r\n" on Windows
 * ```
 */
export const EOL: "\n" | "\r\n" = Deno?.build.os === "windows" ? CRLF : LF;

const regDetect = /(?:\r?\n)/g;

/**
 * Returns the detected EOL character(s) detected in the input string. If no EOL
 * character is detected, `null` is returned.
 *
 * @param content The input string to detect EOL characters.
 * @returns The detected EOL character(s) or `null` if no EOL character is detected.
 *
 * @example
 * ```ts
 * import { detect } from "@std/fs/eol";
 *
 * detect("deno\r\nis not\r\nnode"); // "\r\n"
 * detect("deno\nis not\r\nnode"); // "\r\n"
 * detect("deno\nis not\nnode"); // "\n"
 * detect("deno is not node"); // null
 * ```
 */
export function detect(content: string): typeof EOL | null {
  const d = content.match(regDetect);
  if (!d || d.length === 0) {
    return null;
  }
  const hasCRLF = d.some((x: string): boolean => x === CRLF);

  return hasCRLF ? CRLF : LF;
}

/**
 * Normalize the input string to the targeted EOL.
 *
 * @param content The input string to normalize.
 * @param eol The EOL character(s) to normalize the input string to.
 * @returns The input string normalized to the targeted EOL.
 *
 * @example
 * ```ts
 * import { LF, format } from "@std/fs/eol";
 *
 * const CRLFinput = "deno\r\nis not\r\nnode";
 *
 * format(CRLFinput, LF); // "deno\nis not\nnode"
 * ```
 */
export function format(content: string, eol: typeof EOL): string {
  return content.replace(regDetect, eol);
}

// denoCacheMetadata={"headers":{"x-content-type-options":"nosniff","cross-origin-resource-policy":"cross-origin","cache-control":"public, max-age=31536000, immutable","vary":"Origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-length":"1890","access-control-allow-origin":"*","cf-ray":"a223e656bfeb7db6-LAX","server":"cloudflare","accept-ranges":"bytes","age":"804698","date":"Tue, 28 Jul 2026 12:31:15 GMT","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';","cf-cache-status":"HIT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=8mc117fB%2FtAg8Hi1OznB9o%2BFXLwdgfqhb1rSS0HJfasRgdnTNqaAISDw5GBKjHT9YZ5sAmxg1WPWMajFCiMoC1p9pD1C%2FFiof7Ax%2B%2FkhKBBgUA7N%2FagJzxw%3D\"}]}","access-control-expose-headers":"*","content-type":"text/typescript","etag":"\"e986e44a35d4cdae56931b1b4a25a1bb\"","x-jsr-backend":"modules","set-cookie":"__cf_bm=8ajCuOHuwcsJosatALQsVHuyu94gNXKk6SDoCGr8hPc-1785241875.0006568-1.0.1.1-JzmZJSMwhh30D9V5FS4lOBQiyBroJn30aBRa8CcfhY9EAsiktt3M2JbwqyibmNbKHV62IMFdNbq4fHQpM1BFgdu0nyb_wuf4AyhBIuZi0dpWiWgFcsUrt2lVtK9fxHXs; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","x-robots-tag":"noindex"},"url":"https://jsr.io/@std/fs/0.224.0/eol.ts","time":1785241875}