Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added EOL detect / format #289

Merged
merged 5 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions fs/eol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

/** EndOfLine character enum */
export enum EOL {
LF = "\n",
CRLF = "\r\n"
}

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;
}
const crlf = d.filter(x => x === EOL.CRLF);
if (crlf.length > 0) {
return EOL.CRLF;
} else {
return EOL.LF;
}
}

/** Format the file to the targeted EOL */
export function format(content: string, eol: EOL): string {
return content.replace(regDetect, eol);
}
32 changes: 32 additions & 0 deletions fs/eol_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { format, detect, EOL } from "./eol.ts";

const CRLFinput = "deno\r\nis not\r\nnode";
const Mixedinput = "deno\nis not\r\nnode";
const Mixedinput2 = "deno\r\nis not\nnode";
const LFinput = "deno\nis not\nnode";
const NoNLinput = "deno is not node";

test(function detectCRLF() {
assertEquals(detect(CRLFinput), EOL.CRLF);
});
test(function detectLF() {
assertEquals(detect(LFinput), EOL.LF);
});
test(function detectNoNewLine() {
assertEquals(detect(NoNLinput), null);
});
test(function testFormat() {
assertEquals(format(CRLFinput, EOL.LF), LFinput);
assertEquals(format(LFinput, EOL.LF), LFinput);
assertEquals(format(LFinput, EOL.CRLF), CRLFinput);
assertEquals(format(CRLFinput, EOL.CRLF), CRLFinput);
assertEquals(format(CRLFinput, EOL.CRLF), CRLFinput);
assertEquals(format(NoNLinput, EOL.CRLF), NoNLinput);
assertEquals(format(Mixedinput, EOL.CRLF), CRLFinput);
assertEquals(format(Mixedinput, EOL.LF), LFinput);
assertEquals(format(Mixedinput2, EOL.CRLF), CRLFinput);
assertEquals(format(Mixedinput2, EOL.LF), LFinput);
});
1 change: 1 addition & 0 deletions fs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "./path/test.ts";
import "./walk_test.ts";
import "./globrex_test.ts";
import "./glob_test.ts";
import "./eol_test.ts";
import "./exists_test.ts";
import "./empty_dir_test.ts";
import "./ensure_dir_test.ts";
Expand Down