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 float number instances #5896

Merged
merged 1 commit 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: 12 additions & 6 deletions yaml/_type/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ function constructYamlFloat(data: string): number {

const SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/;

function representYamlFloat(object: number, style?: StyleVariant): string {
if (isNaN(object)) {
function representYamlFloat(
// deno-lint-ignore ban-types
object: number | Number,
style?: StyleVariant,
): string {
const value = object instanceof Number ? object.valueOf() : object;
if (isNaN(value)) {
switch (style) {
case "lowercase":
return ".nan";
Expand All @@ -60,7 +65,7 @@ function representYamlFloat(object: number, style?: StyleVariant): string {
case "camelcase":
return ".NaN";
}
} else if (Number.POSITIVE_INFINITY === object) {
} else if (Number.POSITIVE_INFINITY === value) {
switch (style) {
case "lowercase":
return ".inf";
Expand All @@ -69,7 +74,7 @@ function representYamlFloat(object: number, style?: StyleVariant): string {
case "camelcase":
return ".Inf";
}
} else if (Number.NEGATIVE_INFINITY === object) {
} else if (Number.NEGATIVE_INFINITY === value) {
switch (style) {
case "lowercase":
return "-.inf";
Expand All @@ -78,11 +83,11 @@ function representYamlFloat(object: number, style?: StyleVariant): string {
case "camelcase":
return "-.Inf";
}
} else if (isNegativeZero(object)) {
} else if (isNegativeZero(value)) {
return "-0.0";
}

const res = object.toString(10);
const res = value.toString(10);

// JS stringifier can build scientific format without dots: 5e-100,
// while YAML requires dot: 5.e-100. Fix it with simple hack
Expand All @@ -91,6 +96,7 @@ function representYamlFloat(object: number, style?: StyleVariant): string {
}

function isFloat(object: unknown): object is number {
if (object instanceof Number) object = object.valueOf();
return typeof object === "number" &&
(object % 1 !== 0 || isNegativeZero(object));
}
Expand Down
80 changes: 44 additions & 36 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,49 +226,57 @@ Deno.test({
});

Deno.test({
name: "stringify() handles float types",
name: "stringify() handles float values",
fn() {
const floats = [
4.1,
-1.473,
6.82e-5,
6.82e-12,
5e-12,
0,
-0,
];
assertEquals(
stringify(floats),
`- 4.1
- -1.473
- 0.0000682
- 6.82e-12
- 5.e-12
- 0
- -0.0
`,
assertEquals(stringify(4.1), `4.1\n`);
assertEquals(stringify(-1.473), `-1.473\n`);
assertEquals(stringify(6.82e-5), `0.0000682\n`);
assertEquals(stringify(6.82e-12), `6.82e-12\n`);
assertEquals(stringify(5e-12), `5.e-12\n`);
assertEquals(stringify(0.0), `0\n`);
assertEquals(stringify(-0), `-0.0\n`);
assertEquals(stringify(new Number(1.234)), `1.234\n`);
assertEquals(stringify(new Number(-1.234)), `-1.234\n`);

assertEquals(stringify(Infinity), `.inf\n`);
assertEquals(stringify(-Infinity), `-.inf\n`);
assertEquals(stringify(NaN), `.nan\n`);
},
});
Deno.test({
name: "stringify() handles float values with styles option",
fn() {
assertEquals(
stringify(Infinity, {
styles: { "tag:yaml.org,2002:float": "uppercase" },
}),
`.INF\n`,
);
const infNaN = [Infinity, -Infinity, NaN];
assertEquals(
stringify(infNaN),
`- .inf
- -.inf
- .nan
`,
stringify(-Infinity, {
styles: { "tag:yaml.org,2002:float": "uppercase" },
}),
`-.INF\n`,
);
assertEquals(
stringify(infNaN, { styles: { "tag:yaml.org,2002:float": "uppercase" } }),
`- .INF
- -.INF
- .NAN
`,
stringify(NaN, { styles: { "tag:yaml.org,2002:float": "uppercase" } }),
`.NAN\n`,
);
assertEquals(
stringify(infNaN, { styles: { "tag:yaml.org,2002:float": "camelcase" } }),
`- .Inf
- -.Inf
- .NaN
`,
stringify(Infinity, {
styles: { "tag:yaml.org,2002:float": "camelcase" },
}),
`.Inf\n`,
);
assertEquals(
stringify(-Infinity, {
styles: { "tag:yaml.org,2002:float": "camelcase" },
}),
`-.Inf\n`,
);
assertEquals(
stringify(NaN, { styles: { "tag:yaml.org,2002:float": "camelcase" } }),
`.NaN\n`,
);
},
});
Expand Down