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

refactor(yaml): simplify and rename dropEndingNewline() #5336

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 4 additions & 7 deletions yaml/_dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,8 @@
return result.slice(1); // drop extra \n joiner
}

// (See the note for writeScalar.)
function dropEndingNewline(string: string): string {
return string[string.length - 1] === "\n" ? string.slice(0, -1) : string;
export function trimTrailingNewline(string: string) {
return string.at(-1) === "\n" ? string.slice(0, -1) : string;
}

// Note: a long line without a suitable break point will exceed the width limit.
Expand Down Expand Up @@ -578,13 +577,11 @@
return `'${string.replace(/'/g, "''")}'`;
case STYLE_LITERAL:
return `|${blockHeader(string, state.indent)}${
dropEndingNewline(
indentString(string, indent),
)
trimTrailingNewline(indentString(string, indent))

Check warning on line 580 in yaml/_dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper.ts#L580

Added line #L580 was not covered by tests
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}`;
case STYLE_FOLDED:
return `>${blockHeader(string, state.indent)}${
dropEndingNewline(
trimTrailingNewline(

Check warning on line 584 in yaml/_dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper.ts#L584

Added line #L584 was not covered by tests
indentString(foldString(string, lineWidth), indent),
)
}`;
Expand Down
13 changes: 13 additions & 0 deletions yaml/_dumper_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "@std/assert";
import { trimTrailingNewline } from "./_dumper.ts";

Deno.test("trimTrailingNewline()", async (t) => {
await t.step("handles string without trailing newline", () => {
assertEquals(trimTrailingNewline("hello\nworld"), "hello\nworld");
});
await t.step("handles string with trailing newline", () => {
assertEquals(trimTrailingNewline("hello\nworld\n"), "hello\nworld");
});
});