2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-06 21:06:07 -04:00
|
|
|
|
|
|
|
/** EndOfLine character enum */
|
|
|
|
export enum EOL {
|
|
|
|
LF = "\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
CRLF = "\r\n",
|
2019-04-06 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const regDetect = /(?:\r?\n)/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect the EOL character for string input.
|
|
|
|
* returns null if no newline
|
|
|
|
*/
|
|
|
|
export function detect(content: string): EOL | null {
|
|
|
|
const d = content.match(regDetect);
|
|
|
|
if (!d || d.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-11-09 17:43:44 -05:00
|
|
|
const hasCRLF = d.some((x: string): boolean => x === EOL.CRLF);
|
|
|
|
|
|
|
|
return hasCRLF ? EOL.CRLF : EOL.LF;
|
2019-04-06 21:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Format the file to the targeted EOL */
|
|
|
|
export function format(content: string, eol: EOL): string {
|
|
|
|
return content.replace(regDetect, eol);
|
|
|
|
}
|