From 295b3a4251ba55e6b8857d3e5581f0d90e1c864f Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 12 Jul 2019 10:48:41 +0300 Subject: [PATCH] Correctly parse numbers when passed in CLI Fixes #3295 --- src/command-line/utils.js | 2 ++ test/src/command-line/utilsTest.js | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/command-line/utils.js b/src/command-line/utils.js index fd9eb9e4..c7004f05 100644 --- a/src/command-line/utils.js +++ b/src/command-line/utils.js @@ -71,6 +71,8 @@ class Utils { return undefined; } else if (value === "null") { return null; + } else if (/^-?[0-9]+$/.test(value)) { // Numbers like port + value = parseInt(value, 10); } else if (/^\[.*\]$/.test(value)) { // Arrays // Supporting arrays `[a,b]` and `[a, b]` const array = value.slice(1, -1).split(/,\s*/); diff --git a/test/src/command-line/utilsTest.js b/test/src/command-line/utilsTest.js index fedc530e..56682dd7 100644 --- a/test/src/command-line/utilsTest.js +++ b/test/src/command-line/utilsTest.js @@ -53,6 +53,7 @@ describe("Utils", function() { it("should correctly parse empty strings", function() { expect(Utils.parseConfigOptions("foo=")).to.deep.equal({foo: ""}); + expect(Utils.parseConfigOptions("foo= ")).to.deep.equal({foo: " "}); }); it("should correctly parse null values", function() { @@ -91,6 +92,16 @@ describe("Utils", function() { expect(Utils.parseConfigOptions("foo[0]=value")) .to.deep.equal({foo: ["value"]}); }); + + it("should correctly change type to number", function() { + expect(Utils.parseConfigOptions("foo=1337")).to.deep.equal({foo: 1337}); + expect(Utils.parseConfigOptions("foo=5")).to.deep.equal({foo: 5}); + expect(Utils.parseConfigOptions("foo=0")).to.deep.equal({foo: 0}); + expect(Utils.parseConfigOptions("foo=9876543210")).to.deep.equal({foo: 9876543210}); + expect(Utils.parseConfigOptions("foo=0987654321")).to.deep.equal({foo: 987654321}); + expect(Utils.parseConfigOptions("foo=-1")).to.deep.equal({foo: -1}); + expect(Utils.parseConfigOptions("foo=-0")).to.deep.equal({foo: -0}); + }); }); describe("when some options have already been parsed", function() {