File tree 3 files changed +33
-3
lines changed
3 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 1
1
//import { sleep } from "https://js.sabae.cc/sleep.js";
2
2
import { sleep } from "./sleep.js" ;
3
+ import { checkFloat } from "./checkFloat.js" ;
3
4
4
5
const DEFAULT_MAX_LOOP = 1000 ;
5
6
@@ -316,9 +317,8 @@ export class Runtime {
316
317
vars . input = async ( s ) => {
317
318
const toint = ( s ) => {
318
319
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 ) ;
322
322
} ;
323
323
if ( this . callbackinput ) {
324
324
return toint ( await this . callbackinput ( s ) ) ;
Original file line number Diff line number Diff line change
1
+ export const checkFloat = ( s ) => {
2
+ const floatRegex = / ^ [ + - ] ? (?: \d * \. \d + | \d + \. \d * | \d + ) $ / ;
3
+ return floatRegex . test ( s ) ;
4
+ } ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments