Skip to content

Commit

Permalink
refactor(yaml): add writeNode options (#5409)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen authored Jul 11, 2024
1 parent e29f946 commit 9a32225
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions yaml/_dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ export class DumperState {

for (let index = 0; index < object.length; index += 1) {
// Write only valid elements.
if (this.writeNode(level, object[index], false, false)) {
if (
this.writeNode(level, object[index], {
block: false,
compact: false,
isKey: false,
})
) {
if (index !== 0) _result += `,${!this.condenseFlow ? " " : ""}`;
_result += this.dump;
}
Expand All @@ -292,14 +298,20 @@ export class DumperState {
level: number,
// deno-lint-ignore no-explicit-any
object: any,
compact = false,
compact: boolean,
) {
let _result = "";
const _tag = this.tag;

for (let index = 0; index < object.length; index += 1) {
// Write only valid elements.
if (this.writeNode(level + 1, object[index], true, true)) {
if (
this.writeNode(level + 1, object[index], {
block: true,
compact: true,
isKey: false,
})
) {
if (!compact || index !== 0) {
_result += generateNextLine(this.indent, level);
}
Expand Down Expand Up @@ -334,7 +346,13 @@ export class DumperState {

const objectValue = object[objectKey];

if (!this.writeNode(level, objectKey, false, false)) {
if (
!this.writeNode(level, objectKey, {
block: false,
compact: false,
isKey: false,
})
) {
continue; // Skip this pair because of invalid key;
}

Expand All @@ -344,7 +362,13 @@ export class DumperState {
this.condenseFlow ? "" : " "
}`;

if (!this.writeNode(level, objectValue, false, false)) {
if (
!this.writeNode(level, objectValue, {
block: false,
compact: false,
isKey: false,
})
) {
continue; // Skip this pair because of invalid value.
}

Expand All @@ -362,7 +386,7 @@ export class DumperState {
level: number,
// deno-lint-ignore no-explicit-any
object: any,
compact = false,
compact: boolean,
) {
const _tag = this.tag;
const objectKeyList = Object.keys(object);
Expand All @@ -389,7 +413,13 @@ export class DumperState {

const objectValue = object[objectKey];

if (!this.writeNode(level + 1, objectKey, true, true, true)) {
if (
!this.writeNode(level + 1, objectKey, {
block: true,
compact: true,
isKey: true,
})
) {
continue; // Skip this pair because of invalid key.
}

Expand All @@ -410,7 +440,13 @@ export class DumperState {
pairBuffer += generateNextLine(this.indent, level);
}

if (!this.writeNode(level + 1, objectValue, true, explicitPair)) {
if (
!this.writeNode(level + 1, objectValue, {
block: true,
compact: explicitPair,
isKey: false,
})
) {
continue; // Skip this pair because of invalid value.
}

Expand All @@ -433,7 +469,7 @@ export class DumperState {
detectType(
// deno-lint-ignore no-explicit-any
object: any,
explicit = false,
explicit: boolean,
): boolean {
const typeList = explicit ? this.explicitTypes : this.implicitTypes;

Expand Down Expand Up @@ -474,9 +510,11 @@ export class DumperState {
level: number,
// deno-lint-ignore no-explicit-any
object: any,
block: boolean,
compact: boolean,
isKey = false,
{ block, compact, isKey }: {
block: boolean;
compact: boolean;
isKey: boolean;
},
): boolean {
this.tag = null;
this.dump = object;
Expand Down Expand Up @@ -892,7 +930,13 @@ export function dump(input: any, options: DumperStateOptions = {}): string {

if (state.useAnchors) state.getDuplicateReferences(input);

if (state.writeNode(0, input, true, true)) return `${state.dump}\n`;
if (
state.writeNode(0, input, {
block: true,
compact: true,
isKey: false,
})
) return `${state.dump}\n`;

return "";
}

0 comments on commit 9a32225

Please sign in to comment.