Skip to content

Commit

Permalink
refactor(yaml): remove repeat helper function (#5303)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen committed Jul 4, 2024
1 parent 6a28a21 commit 9d5d887
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 30 deletions.
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 @@ function encodeHex(character: number): string {

// 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);
const length = string.length;
let position = 0;
let next = -1;
Expand All @@ -244,7 +244,7 @@ function indentString(string: string, spaces: number): string {
}

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 @@ function writeFoldedLines(state: LoaderState, count: number) {
if (count === 1) {
state.result += " ";
} else if (count > 1) {
state.result += common.repeat("\n", count - 1);
state.result += "\n".repeat(count - 1);
}
}

Expand Down Expand Up @@ -936,8 +936,7 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
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 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
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 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {

// 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

0 comments on commit 9d5d887

Please sign in to comment.