Skip to content

Commit

Permalink
fix(yaml): handle float number instances (#5896)
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
timreichen authored Sep 3, 2024
1 parent 2d6c45e commit 25de700
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
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

0 comments on commit 25de700

Please sign in to comment.