Skip to content

Commit 9d5d887

Browse files
authored
refactor(yaml): remove repeat helper function (#5303)
1 parent 6a28a21 commit 9d5d887

File tree

4 files changed

+10
-30
lines changed

4 files changed

+10
-30
lines changed

yaml/_dumper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function encodeHex(character: number): string {
218218

219219
// Indents every line in a string. Empty lines (\n only) are not indented.
220220
function indentString(string: string, spaces: number): string {
221-
const ind = common.repeat(" ", spaces);
221+
const ind = " ".repeat(spaces);
222222
const length = string.length;
223223
let position = 0;
224224
let next = -1;
@@ -244,7 +244,7 @@ function indentString(string: string, spaces: number): string {
244244
}
245245

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

250250
function testImplicitResolving(state: DumperState, str: string): boolean {

yaml/_loader.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ function writeFoldedLines(state: LoaderState, count: number) {
486486
if (count === 1) {
487487
state.result += " ";
488488
} else if (count > 1) {
489-
state.result += common.repeat("\n", count - 1);
489+
state.result += "\n".repeat(count - 1);
490490
}
491491
}
492492

@@ -936,8 +936,7 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
936936
if (state.lineIndent < textIndent) {
937937
// Perform the chomping.
938938
if (chomping === CHOMPING_KEEP) {
939-
state.result += common.repeat(
940-
"\n",
939+
state.result += "\n".repeat(
941940
didReadContent ? 1 + emptyLines : emptyLines,
942941
);
943942
} else if (chomping === CHOMPING_CLIP) {
@@ -957,15 +956,14 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
957956
if (isWhiteSpace(ch)) {
958957
atMoreIndented = true;
959958
// except for the first content line (cf. Example 8.1)
960-
state.result += common.repeat(
961-
"\n",
959+
state.result += "\n".repeat(
962960
didReadContent ? 1 + emptyLines : emptyLines,
963961
);
964962

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

970968
// Just one line break - perceive as the same line.
971969
} else if (emptyLines === 0) {
@@ -976,16 +974,13 @@ function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
976974

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

982980
// Literal style: just add exact number of line breaks between content lines.
983981
} else {
984982
// Keep all line breaks except the header line break.
985-
state.result += common.repeat(
986-
"\n",
987-
didReadContent ? 1 + emptyLines : emptyLines,
988-
);
983+
state.result += "\n".repeat(didReadContent ? 1 + emptyLines : emptyLines);
989984
}
990985

991986
didReadContent = true;

yaml/_mark.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
44
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
55

6-
import { repeat } from "./_utils.ts";
7-
86
export class Mark {
97
buffer: string;
108
position: number;
@@ -56,11 +54,8 @@ export class Mark {
5654
}
5755

5856
const snippet = this.buffer.slice(start, end);
59-
return `${repeat(" ", indent)}${head}${snippet}${tail}\n${
60-
repeat(
61-
" ",
62-
indent + this.position - start + head.length,
63-
)
57+
return `${" ".repeat(indent)}${head}${snippet}${tail}\n${
58+
" ".repeat(indent + this.position - start + head.length)
6459
}^`;
6560
}
6661

yaml/_utils.ts

-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ export function isObject(value: unknown): value is Record<string, unknown> {
1414
return value !== null && typeof value === "object";
1515
}
1616

17-
export function repeat(str: string, count: number): string {
18-
let result = "";
19-
20-
for (let cycle = 0; cycle < count; cycle++) {
21-
result += str;
22-
}
23-
24-
return result;
25-
}
26-
2717
export function isNegativeZero(i: number): boolean {
2818
return i === 0 && Number.NEGATIVE_INFINITY === 1 / i;
2919
}

0 commit comments

Comments
 (0)