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 Boolean instances #5894

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
18 changes: 15 additions & 3 deletions yaml/_type/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ export const bool: Type<"scalar", boolean> = {
construct: (data: string): boolean => YAML_TRUE_BOOLEANS.includes(data),
resolve: (data: string): boolean => YAML_BOOLEANS.includes(data),
represent: {
lowercase: (object: boolean): string => object ? "true" : "false",
uppercase: (object: boolean): string => object ? "TRUE" : "FALSE",
camelcase: (object: boolean): string => object ? "True" : "False",
// deno-lint-ignore ban-types
lowercase: (object: boolean | Boolean): string => {
const value = object instanceof Boolean ? object.valueOf() : object;
return value ? "true" : "false";
},
// deno-lint-ignore ban-types
uppercase: (object: boolean | Boolean): string => {
const value = object instanceof Boolean ? object.valueOf() : object;
return value ? "TRUE" : "FALSE";
},
// deno-lint-ignore ban-types
camelcase: (object: boolean | Boolean): string => {
const value = object instanceof Boolean ? object.valueOf() : object;
return value ? "True" : "False";
},
},
};
45 changes: 36 additions & 9 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,49 @@ Deno.test({
});

Deno.test({
name: "stringify() serializes boolean values",
name: "stringify() handles boolean values",
fn() {
assertEquals(stringify([true, false]), "- true\n- false\n");
assertEquals(stringify(true), "true\n");
assertEquals(stringify(false), "false\n");

// casing can be controlled with styles options
assertEquals(stringify(new Boolean(true)), "true\n");
assertEquals(stringify(new Boolean(false)), "false\n");
},
});
Deno.test({
name: "stringify() handles boolean with styles option",
fn() {
assertEquals(
stringify([true, false], { styles: { "!!bool": "camelcase" } }),
"- True\n- False\n",
stringify(true, { styles: { "!!bool": "camelcase" } }),
"True\n",
);
assertEquals(
stringify([true, false], { styles: { "!!bool": "uppercase" } }),
"- TRUE\n- FALSE\n",
stringify(false, { styles: { "!!bool": "camelcase" } }),
"False\n",
);
assertEquals(
stringify(new Boolean(true), { styles: { "!!bool": "camelcase" } }),
"True\n",
);
assertEquals(
stringify(true, { styles: { "!!bool": "uppercase" } }),
"TRUE\n",
);
assertEquals(
stringify(false, { styles: { "!!bool": "uppercase" } }),
"FALSE\n",
);
assertEquals(
stringify(new Boolean(true), { styles: { "!!bool": "uppercase" } }),
"TRUE\n",
);
assertThrows(
() => stringify(true, { styles: { "!!bool": "octal" } }),
TypeError,
'!<tag:yaml.org,2002:bool> tag resolver accepts not "octal" style',
);

assertThrows(
() => stringify([true, false], { styles: { "!!bool": "octal" } }),
() => stringify(false, { styles: { "!!bool": "octal" } }),
TypeError,
'!<tag:yaml.org,2002:bool> tag resolver accepts not "octal" style',
);
Expand Down