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): remove repeat helper function #5303

Merged
merged 1 commit into from
Jul 4, 2024
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
4 changes: 2 additions & 2 deletions yaml/_dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@

// Indents every line in a string. Empty lines (\n only) are not indented.
function indentString(string: string, spaces: number): string {
const ind = common.repeat(" ", spaces);
const ind = " ".repeat(spaces);

Check warning on line 221 in yaml/_dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper.ts#L221

Added line #L221 was not covered by tests
const length = string.length;
let position = 0;
let next = -1;
Expand All @@ -244,7 +244,7 @@
}

function generateNextLine(state: DumperState, level: number): string {
return `\n${common.repeat(" ", state.indent * level)}`;
return `\n${" ".repeat(state.indent * level)}`;
}

function testImplicitResolving(state: DumperState, str: string): boolean {
Expand Down
17 changes: 6 additions & 11 deletions yaml/_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
if (count === 1) {
state.result += " ";
} else if (count > 1) {
state.result += common.repeat("\n", count - 1);
state.result += "\n".repeat(count - 1);

Check warning on line 489 in yaml/_loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader.ts#L489

Added line #L489 was not covered by tests
}
}

Expand Down Expand Up @@ -936,8 +936,7 @@
if (state.lineIndent < textIndent) {
// Perform the chomping.
if (chomping === CHOMPING_KEEP) {
state.result += common.repeat(
"\n",
state.result += "\n".repeat(
didReadContent ? 1 + emptyLines : emptyLines,
);
} else if (chomping === CHOMPING_CLIP) {
Expand All @@ -957,15 +956,14 @@
if (isWhiteSpace(ch)) {
atMoreIndented = true;
// except for the first content line (cf. Example 8.1)
state.result += common.repeat(
"\n",
state.result += "\n".repeat(
didReadContent ? 1 + emptyLines : emptyLines,
);

// End of more-indented block.
} else if (atMoreIndented) {
atMoreIndented = false;
state.result += common.repeat("\n", emptyLines + 1);
state.result += "\n".repeat(emptyLines + 1);

// Just one line break - perceive as the same line.
} else if (emptyLines === 0) {
Expand All @@ -976,16 +974,13 @@

// Several line breaks - perceive as different lines.
} else {
state.result += common.repeat("\n", emptyLines);
state.result += "\n".repeat(emptyLines);
}

// Literal style: just add exact number of line breaks between content lines.
} else {
// Keep all line breaks except the header line break.
state.result += common.repeat(
"\n",
didReadContent ? 1 + emptyLines : emptyLines,
);
state.result += "\n".repeat(didReadContent ? 1 + emptyLines : emptyLines);
}

didReadContent = true;
Expand Down
9 changes: 2 additions & 7 deletions yaml/_mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { repeat } from "./_utils.ts";

export class Mark {
buffer: string;
position: number;
Expand Down Expand Up @@ -56,11 +54,8 @@ export class Mark {
}

const snippet = this.buffer.slice(start, end);
return `${repeat(" ", indent)}${head}${snippet}${tail}\n${
repeat(
" ",
indent + this.position - start + head.length,
)
return `${" ".repeat(indent)}${head}${snippet}${tail}\n${
" ".repeat(indent + this.position - start + head.length)
}^`;
}

Expand Down
10 changes: 0 additions & 10 deletions yaml/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export function isObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object";
}

export function repeat(str: string, count: number): string {
let result = "";

for (let cycle = 0; cycle < count; cycle++) {
result += str;
}

return result;
}

export function isNegativeZero(i: number): boolean {
return i === 0 && Number.NEGATIVE_INFINITY === 1 / i;
}
Expand Down