Skip to content

Commit

Permalink
refactor(yaml): move duplicate and duplicateIndex (#5836)
Browse files Browse the repository at this point in the history
* initial commit

* update
  • Loading branch information
timreichen authored Aug 29, 2024
1 parent c3b113d commit fab118b
Showing 1 changed file with 46 additions and 54 deletions.
100 changes: 46 additions & 54 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,69 +781,61 @@ export class DumperState {
block = this.flowLevel < 0 || this.flowLevel > level;
}

let duplicateIndex = -1;
let duplicate = false;
if (isObject(value)) {
duplicateIndex = this.duplicates.indexOf(value);
duplicate = duplicateIndex !== -1;
}

if (
(tag !== null && tag !== "?") ||
duplicate ||
(this.indent !== 2 && level > 0)
) {
compact = false;
}
const duplicateIndex = this.duplicates.indexOf(value);
const duplicate = duplicateIndex !== -1;

if (duplicate && this.usedDuplicates.has(value)) {
return `*ref_${duplicateIndex}`;
} else {
if (isObject(value)) {
if (duplicate) {
this.usedDuplicates.add(value);
}
if (!Array.isArray(value)) {
if (block && Object.keys(value).length !== 0) {
value = this.stringifyBlockMapping(value, { tag, level, compact });
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowMapping(value, { level });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
if (duplicate) {
if (this.usedDuplicates.has(value)) return `*ref_${duplicateIndex}`;
this.usedDuplicates.add(value);
}
if (
(tag !== null && tag !== "?") ||
duplicate ||
(this.indent !== 2 && level > 0)
) {
compact = false;
}
if (!Array.isArray(value)) {
if (block && Object.keys(value).length !== 0) {
value = this.stringifyBlockMapping(value, { tag, level, compact });
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
if (block && value.length !== 0) {
value = this.stringifyBlockSequence(value, {
level: arrayLevel,
compact,
});
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowSequence(value, { level: arrayLevel });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
value = this.stringifyFlowMapping(value, { level });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
}
} else if (typeof value === "string") {
if (tag !== "?") {
value = this.stringifyScalar(value, { level, isKey });
}
} else {
if (this.skipInvalid) return null;
throw new TypeError(`Cannot stringify ${typeof value}`);
const arrayLevel = !this.arrayIndent && level > 0 ? level - 1 : level;
if (block && value.length !== 0) {
value = this.stringifyBlockSequence(value, {
level: arrayLevel,
compact,
});
if (duplicate) {
value = `&ref_${duplicateIndex}${value}`;
}
} else {
value = this.stringifyFlowSequence(value, { level: arrayLevel });
if (duplicate) {
value = `&ref_${duplicateIndex} ${value}`;
}
}
}

if (tag !== null && tag !== "?") {
value = `!<${tag}> ${value}`;
} else if (typeof value === "string") {
if (tag !== "?") {
value = this.stringifyScalar(value, { level, isKey });
}
} else {
if (this.skipInvalid) return null;
throw new TypeError(`Cannot stringify ${typeof value}`);
}

if (tag !== null && tag !== "?") {
value = `!<${tag}> ${value}`;
}

return value as string;
Expand Down

0 comments on commit fab118b

Please sign in to comment.