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

fix(yaml): handle String instances #5897

Merged
merged 2 commits into from
Sep 3, 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
12 changes: 6 additions & 6 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,12 @@ export class DumperState {
if (block) {
block = this.flowLevel < 0 || this.flowLevel > level;
}

if (isObject(value)) {
if (typeof value === "string" || value instanceof String) {
value = value instanceof String ? value.valueOf() : value;
if (tag !== "?") {
value = this.stringifyScalar(value as string, { level, isKey });
}
} else if (isObject(value)) {
const duplicateIndex = this.duplicates.indexOf(value);
const duplicate = duplicateIndex !== -1;

Expand Down Expand Up @@ -789,10 +793,6 @@ export class DumperState {
}
}
}
} 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}`);
Expand Down
1 change: 1 addition & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ Oren: Ben-Kiki

Deno.test("stringify() handles string", () => {
assertEquals(stringify("Hello World"), "Hello World\n");
assertEquals(stringify(new String("Hello World")), "Hello World\n");
});

Deno.test("stringify() uses quotes around deprecated boolean notations when `compatMode: true`", () => {
Expand Down