diff --git a/implementations/javascript/packages/core/src/reader.ts b/implementations/javascript/packages/core/src/reader.ts index 75b5e14..2133a94 100644 --- a/implementations/javascript/packages/core/src/reader.ts +++ b/implementations/javascript/packages/core/src/reader.ts @@ -148,6 +148,7 @@ export class ReaderState { readDigit0(kind: IntOrFloat, acc: string, k: IntContinuation): Numeric { while (true) { + if (this.atEnd()) break; const ch = this.peek(); if (!(ch >= '0' && ch <= '9')) break; this.advance(); @@ -157,7 +158,7 @@ export class ReaderState { } readFracexp(kind: IntOrFloat, acc: string): Numeric { - if (this.peek() === '.') { + if (!this.atEnd() && this.peek() === '.') { this.advance(); return this.readDigit1('float', acc + '.', (kind, acc) => this.readExp(kind, acc)); } @@ -165,7 +166,7 @@ export class ReaderState { } readExp(kind: IntOrFloat, acc: string): Numeric { - const ch = this.peek(); + const ch = this.atEnd() ? '' : this.peek(); if (ch === 'e' || ch === 'E') { this.advance(); return this.readSignAndExp(acc + ch); @@ -185,7 +186,7 @@ export class ReaderState { finishNumber(kind: IntOrFloat, acc: string): Numeric { const i = parseFloat(acc); if (kind === 'int') return i; - const ch = this.peek(); + const ch = this.atEnd() ? '' : this.peek(); if (ch === 'f' || ch === 'F') { this.advance(); return Single(i); diff --git a/implementations/javascript/packages/core/test/reader.test.ts b/implementations/javascript/packages/core/test/reader.test.ts index 3484196..3a6e1a3 100644 --- a/implementations/javascript/packages/core/test/reader.test.ts +++ b/implementations/javascript/packages/core/test/reader.test.ts @@ -1,4 +1,4 @@ -import { Bytes, Decoder, genericEmbeddedType, encode, Reader } from '../src/index'; +import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double } from '../src/index'; import './test-utils'; import * as fs from 'fs'; @@ -28,4 +28,10 @@ describe('reading common test suite', () => { })); expect(bs).toEqual(new Uint8Array(samples_bin)); }); + + it('should be ok with no whitespace at the end of a numeric input', () => { + expect(new Reader('123').next()).toEqual(123); + expect(new Reader('123.0').next()).toEqual(Double(123.0)); + expect(new Reader('123.00').next()).toEqual(Double(123.0)); + }); });