mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
fix(yaml): handle Boolean instances correctly (#5894)
This commit is contained in:
parent
5bed4c81b5
commit
890958ca48
@ -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";
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -135,22 +135,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, false], { styles: { "!!bool": "octal" } }),
|
||||
() => stringify(true, { styles: { "!!bool": "octal" } }),
|
||||
TypeError,
|
||||
'!<tag:yaml.org,2002:bool> tag resolver accepts not "octal" style',
|
||||
);
|
||||
assertThrows(
|
||||
() => stringify(false, { styles: { "!!bool": "octal" } }),
|
||||
TypeError,
|
||||
'!<tag:yaml.org,2002:bool> tag resolver accepts not "octal" style',
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user