diff --git a/src/utils/parsing.ts b/src/utils/parsing.ts index 00b8d15..a151d3e 100644 --- a/src/utils/parsing.ts +++ b/src/utils/parsing.ts @@ -7,7 +7,7 @@ import { RGBColor } from './rgbcolor' export function parseFloats(str: string): number[] { const floats = [] - const regex = /[+-]?(?:(?:\d+\.?\d*)|(?:\d*\.?\d+))(?:[eE][+-]?\d+)?/g + const regex = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/g let match while ((match = regex.exec(str))) { floats.push(parseFloat(match[0])) diff --git a/test/parsing/parsing.spec.ts b/test/parsing/parsing.spec.ts new file mode 100644 index 0000000..2fe8f2a --- /dev/null +++ b/test/parsing/parsing.spec.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'vitest' +import { parseFloats } from '../../src/utils/parsing.ts' + +describe('parseFloats', () => { + it('returns an empty array when the input contains no numbers', () => { + expect(parseFloats('')).toEqual([]) + }) + + it('parses comma-, whitespace-, and sign-separated numbers', () => { + expect(parseFloats('10, 20 30-40+50')).toEqual([10, 20, 30, -40, 50]) + }) + + it('parses integers, decimal forms, and scientific notation', () => { + expect(parseFloats('1.5 .25 2. 1e3 -2.5E-2 +4e+1')).toEqual([1.5, 0.25, 2, 1000, -0.025, 40]) + }) + + it('has linear runtime complexity', { timeout: 100 }, () => { + expect(parseFloats('1'.repeat(10000) + '!')).to.toHaveLength(1) + }) +})