diff --git a/js/__snapshots__/regionParser.test.js.snap b/js/__snapshots__/regionParser.test.js.snap index d11db1c9..19edcc68 100644 --- a/js/__snapshots__/regionParser.test.js.snap +++ b/js/__snapshots__/regionParser.test.js.snap @@ -409,6 +409,23 @@ Object { } `; +exports[`regionParser parses a region.txt where the points have leading commas 1`] = ` +Object { + "equalizer": Array [ + "5,0 3,1 2,2 1,3 0,5 0,116 275,116 275,6 274,3 273,2 272,1 270,0", + ], + "equalizerws": Array [ + "5,0 3,1 2,2 1,3 0,5 0,14 275,14 275,6 274,3 273,2 272,1 270,0", + ], + "normal": Array [ + "5,0 3,1 2,2 1,3 0,5 0,116 275,116 275,6 274,3 273,2 272,1 270,0", + ], + "windowshade": Array [ + "5,0 3,1 2,2 1,3 0,5 0,14 275,14 275,6 274,3 273,2 272,1 270,0", + ], +} +`; + exports[`regionParser parses the EVAunit region.txt 1`] = ` Object { "equalizer": Array [ diff --git a/js/__tests__/fixtures/region_leading_comma.txt b/js/__tests__/fixtures/region_leading_comma.txt new file mode 100755 index 00000000..54dfe60a --- /dev/null +++ b/js/__tests__/fixtures/region_leading_comma.txt @@ -0,0 +1,15 @@ +[Normal] +NumPoints=12 +PointList=,5,0, 3,1, 2,2 1,3, 0,5, 0,116, 275,116, 275,6, 274,3, 273,2 272,1, 270,0 + +[EQUALIZER] +NumPoints=12 +PointList=,5,0, 3,1, 2,2 1,3, 0,5, 0,116, 275,116, 275,6, 274,3, 273,2 272,1, 270,0 + +[WindowShade] +NumPoints=12 +PointList=,5,0, 3,1, 2,2 1,3, 0,5, 0,14, 275,14, 275,6, 274,3, 273,2 272,1, 270,0 + +[EqualizerWS] +NumPoints=12 +PointList=,5,0, 3,1, 2,2 1,3, 0,5, 0,14, 275,14, 275,6, 274,3, 273,2 272,1, 270,0 \ No newline at end of file diff --git a/js/regionParser.js b/js/regionParser.js index 48e86fb9..53290ba4 100644 --- a/js/regionParser.js +++ b/js/regionParser.js @@ -16,9 +16,11 @@ export default function regionParser(regionStr) { if (!numpoints || !pointlist) { return; } - const pointCounts = numpoints.split(/\s*,\s*/); - // points can be separated by spaces, or by commas - const points = pointPairs(pointlist.split(/\s*[, ]\s*/)); + const pointCounts = numpoints.split(/\s*,\s*/).filter(val => val !== ""); + const points = pointPairs( + // points can be separated by spaces, or by commas + pointlist.split(/\s*[, ]\s*/).filter(val => val !== "") + ); let pointIndex = 0; const polygons = pointCounts.map(numStr => { const num = Number(numStr); diff --git a/js/regionParser.test.js b/js/regionParser.test.js index b62b8e8f..7cd2eafa 100644 --- a/js/regionParser.test.js +++ b/js/regionParser.test.js @@ -45,4 +45,11 @@ describe("regionParser", () => { ); expect(regionParser(regionTxt)).toMatchSnapshot(); }); + it("parses a region.txt where the points have leading commas", () => { + const regionTxt = fs.readFileSync( + "./js/__tests__/fixtures/region_leading_comma.txt", + "utf8" + ); + expect(regionParser(regionTxt)).toMatchSnapshot(); + }); });