2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2019-04-07 01:06:07 +00:00
|
|
|
|
2023-12-05 08:52:56 +00:00
|
|
|
/** End-of-line character for POSIX platforms such as macOS and Linux. */
|
2023-11-16 10:40:25 +00:00
|
|
|
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.
|
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { EOL } from "@std/fs/eol";
|
2023-11-16 10:40:25 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* EOL; // "\n" on POSIX platforms and "\r\n" on Windows
|
2023-11-16 10:40:25 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2024-10-24 05:44:27 +00:00
|
|
|
export const EOL: "\n" | "\r\n" =
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(globalThis as any).Deno?.build.os === "windows" ? CRLF : LF;
|
2019-04-07 01:06:07 +00:00
|
|
|
|
|
|
|
const regDetect = /(?:\r?\n)/g;
|
|
|
|
|
|
|
|
/**
|
2024-03-27 06:28:06 +00:00
|
|
|
* 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.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns The detected EOL character(s) or `null` if no EOL character is detected.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { detect } from "@std/fs/eol";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* 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
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2019-04-07 01:06:07 +00:00
|
|
|
*/
|
2023-11-24 08:15:58 +00:00
|
|
|
export function detect(content: string): typeof EOL | null {
|
2019-04-07 01:06:07 +00:00
|
|
|
const d = content.match(regDetect);
|
|
|
|
if (!d || d.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-24 08:15:58 +00:00
|
|
|
const hasCRLF = d.some((x: string): boolean => x === CRLF);
|
2020-11-09 22:43:44 +00:00
|
|
|
|
2023-11-24 08:15:58 +00:00
|
|
|
return hasCRLF ? CRLF : LF;
|
2019-04-07 01:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2024-03-27 06:28:06 +00:00
|
|
|
* 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.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns The input string normalized to the targeted EOL.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { LF, format } from "@std/fs/eol";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* const CRLFinput = "deno\r\nis not\r\nnode";
|
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* format(CRLFinput, LF); // "deno\nis not\nnode"
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2023-11-24 08:15:58 +00:00
|
|
|
export function format(content: string, eol: typeof EOL): string {
|
2019-04-07 01:06:07 +00:00
|
|
|
return content.replace(regDetect, eol);
|
|
|
|
}
|