mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(console/ext/repl): support using parseFloat() (#25900)
Fixes #21428 Co-authored-by: tannal <tannal2409@gmail.com>
This commit is contained in:
parent
48cbf85add
commit
8dbe77dd29
6 changed files with 46 additions and 12 deletions
|
@ -84,6 +84,7 @@ const {
|
||||||
NumberIsInteger,
|
NumberIsInteger,
|
||||||
NumberIsNaN,
|
NumberIsNaN,
|
||||||
NumberParseInt,
|
NumberParseInt,
|
||||||
|
NumberParseFloat,
|
||||||
NumberPrototypeToFixed,
|
NumberPrototypeToFixed,
|
||||||
NumberPrototypeToString,
|
NumberPrototypeToString,
|
||||||
NumberPrototypeValueOf,
|
NumberPrototypeValueOf,
|
||||||
|
@ -3010,20 +3011,18 @@ function inspectArgs(args, inspectOptions = { __proto__: null }) {
|
||||||
} else if (ArrayPrototypeIncludes(["d", "i"], char)) {
|
} else if (ArrayPrototypeIncludes(["d", "i"], char)) {
|
||||||
// Format as an integer.
|
// Format as an integer.
|
||||||
const value = args[a++];
|
const value = args[a++];
|
||||||
if (typeof value == "bigint") {
|
if (typeof value === "symbol") {
|
||||||
formattedArg = `${value}n`;
|
|
||||||
} else if (typeof value == "number") {
|
|
||||||
formattedArg = `${NumberParseInt(String(value))}`;
|
|
||||||
} else {
|
|
||||||
formattedArg = "NaN";
|
formattedArg = "NaN";
|
||||||
|
} else {
|
||||||
|
formattedArg = `${NumberParseInt(value)}`;
|
||||||
}
|
}
|
||||||
} else if (char == "f") {
|
} else if (char == "f") {
|
||||||
// Format as a floating point value.
|
// Format as a floating point value.
|
||||||
const value = args[a++];
|
const value = args[a++];
|
||||||
if (typeof value == "number") {
|
if (typeof value === "symbol") {
|
||||||
formattedArg = `${value}`;
|
|
||||||
} else {
|
|
||||||
formattedArg = "NaN";
|
formattedArg = "NaN";
|
||||||
|
} else {
|
||||||
|
formattedArg = `${NumberParseFloat(value)}`;
|
||||||
}
|
}
|
||||||
} else if (ArrayPrototypeIncludes(["O", "o"], char)) {
|
} else if (ArrayPrototypeIncludes(["O", "o"], char)) {
|
||||||
// Format as an object.
|
// Format as an object.
|
||||||
|
|
|
@ -255,6 +255,20 @@ fn console_log() {
|
||||||
console.write_line("'world'");
|
console.write_line("'world'");
|
||||||
console.expect("\"world\"");
|
console.expect("\"world\"");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// https://github.com/denoland/deno/issues/21428
|
||||||
|
let (out, err) = util::run_and_collect_output_with_args(
|
||||||
|
true,
|
||||||
|
vec![
|
||||||
|
"repl",
|
||||||
|
"--eval-file=./../specs/repl/console_log/093_console_log_format.js",
|
||||||
|
],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
assert_contains!(out, "0.5");
|
||||||
|
assert!(err.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
16
tests/specs/repl/console_log/093_console_log_format.js
Normal file
16
tests/specs/repl/console_log/093_console_log_format.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
class Frac {
|
||||||
|
constructor(num, den) {
|
||||||
|
this.num = num;
|
||||||
|
this.den = den;
|
||||||
|
}
|
||||||
|
[Symbol.toPrimitive]() {
|
||||||
|
return this.num / this.den;
|
||||||
|
}
|
||||||
|
display() {
|
||||||
|
const result = this.num / this.den;
|
||||||
|
process.stdout.write(`${result}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const f = new Frac(1, 2);
|
||||||
|
f.display();
|
1
tests/specs/repl/console_log/093_console_log_format.out
Normal file
1
tests/specs/repl/console_log/093_console_log_format.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0.5
|
4
tests/specs/repl/console_log/__test__.jsonc
Normal file
4
tests/specs/repl/console_log/__test__.jsonc
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"args": "run -A --quiet 093_console_log_format.js",
|
||||||
|
"output": "093_console_log_format.out"
|
||||||
|
}
|
|
@ -1162,7 +1162,7 @@ Deno.test(function consoleTestWithIntegerFormatSpecifier() {
|
||||||
assertEquals(stringify("%i"), "%i");
|
assertEquals(stringify("%i"), "%i");
|
||||||
assertEquals(stringify("%i", 42.0), "42");
|
assertEquals(stringify("%i", 42.0), "42");
|
||||||
assertEquals(stringify("%i", 42), "42");
|
assertEquals(stringify("%i", 42), "42");
|
||||||
assertEquals(stringify("%i", "42"), "NaN");
|
assertEquals(stringify("%i", "42"), "42");
|
||||||
assertEquals(stringify("%i", 1.5), "1");
|
assertEquals(stringify("%i", 1.5), "1");
|
||||||
assertEquals(stringify("%i", -0.5), "0");
|
assertEquals(stringify("%i", -0.5), "0");
|
||||||
assertEquals(stringify("%i", ""), "NaN");
|
assertEquals(stringify("%i", ""), "NaN");
|
||||||
|
@ -1172,7 +1172,7 @@ Deno.test(function consoleTestWithIntegerFormatSpecifier() {
|
||||||
assertEquals(stringify("%d", 12345678901234567890123), "1");
|
assertEquals(stringify("%d", 12345678901234567890123), "1");
|
||||||
assertEquals(
|
assertEquals(
|
||||||
stringify("%i", 12345678901234567890123n),
|
stringify("%i", 12345678901234567890123n),
|
||||||
"12345678901234567890123n",
|
"1.2345678901234568e+22",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1180,13 +1180,13 @@ Deno.test(function consoleTestWithFloatFormatSpecifier() {
|
||||||
assertEquals(stringify("%f"), "%f");
|
assertEquals(stringify("%f"), "%f");
|
||||||
assertEquals(stringify("%f", 42.0), "42");
|
assertEquals(stringify("%f", 42.0), "42");
|
||||||
assertEquals(stringify("%f", 42), "42");
|
assertEquals(stringify("%f", 42), "42");
|
||||||
assertEquals(stringify("%f", "42"), "NaN");
|
assertEquals(stringify("%f", "42"), "42");
|
||||||
assertEquals(stringify("%f", 1.5), "1.5");
|
assertEquals(stringify("%f", 1.5), "1.5");
|
||||||
assertEquals(stringify("%f", -0.5), "-0.5");
|
assertEquals(stringify("%f", -0.5), "-0.5");
|
||||||
assertEquals(stringify("%f", Math.PI), "3.141592653589793");
|
assertEquals(stringify("%f", Math.PI), "3.141592653589793");
|
||||||
assertEquals(stringify("%f", ""), "NaN");
|
assertEquals(stringify("%f", ""), "NaN");
|
||||||
assertEquals(stringify("%f", Symbol("foo")), "NaN");
|
assertEquals(stringify("%f", Symbol("foo")), "NaN");
|
||||||
assertEquals(stringify("%f", 5n), "NaN");
|
assertEquals(stringify("%f", 5n), "5");
|
||||||
assertEquals(stringify("%f %f", 42, 43), "42 43");
|
assertEquals(stringify("%f %f", 42, 43), "42 43");
|
||||||
assertEquals(stringify("%f %f", 42), "42 %f");
|
assertEquals(stringify("%f %f", 42), "42 %f");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue