Skip to content

Commit 9f34959

Browse files
committed
fix check float by input
#31
1 parent 9d8a486 commit 9f34959

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Runtime.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//import { sleep } from "https://js.sabae.cc/sleep.js";
22
import { sleep } from "./sleep.js";
3+
import { checkFloat } from "./checkFloat.js";
34

45
const DEFAULT_MAX_LOOP = 1000;
56

@@ -316,9 +317,8 @@ export class Runtime {
316317
vars.input = async (s) => {
317318
const toint = (s) => {
318319
if (s == null) return "";
319-
const f = parseFloat(s);
320-
if (!isNaN(f) && f.toString() == s) return f;
321-
return s;
320+
if (!checkFloat(s)) return s;
321+
return parseFloat(s);
322322
};
323323
if (this.callbackinput) {
324324
return toint(await this.callbackinput(s));

checkFloat.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const checkFloat = (s) => {
2+
const floatRegex = /^[+-]?(?:\d*\.\d+|\d+\.\d*|\d+)$/;
3+
return floatRegex.test(s);
4+
};

checkFloat.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as t from "https://deno.land/std/testing/asserts.ts";
2+
import { checkFloat } from "./checkFloat.js";
3+
4+
const check = (s, b) => {
5+
const b2 = checkFloat(s);
6+
if (b != b2) console.log(s, "must be", b);
7+
t.assertEquals(b, b2);
8+
}
9+
10+
Deno.test("checkFloat", () => {
11+
check(null, false);
12+
check(undefined, false);
13+
check("9", true);
14+
check("0", true);
15+
check("9.", true);
16+
check("9.a", false);
17+
check("a", false);
18+
check("0.1", true);
19+
check(".1", true);
20+
check("-0.1", true);
21+
check("+0.1", true);
22+
check("+-0.1", false);
23+
check(".1000", true);
24+
check("a.1000", false);
25+
check("1e1", false);
26+
});

0 commit comments

Comments
 (0)