From 056a333c6d2b8df0c5e39d49df9f5536ee59916e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 13 Dec 2018 11:50:45 +0100 Subject: [PATCH 1/3] Fix setting aspect ratio in percentage --- src/common/util/parse-aspect-ratio.ts | 40 +++++++++++-------- .../common/util/parse_aspect_ratio_test.ts | 5 +++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/common/util/parse-aspect-ratio.ts b/src/common/util/parse-aspect-ratio.ts index cd80d4b90d77..2de8f9b5034e 100644 --- a/src/common/util/parse-aspect-ratio.ts +++ b/src/common/util/parse-aspect-ratio.ts @@ -1,24 +1,30 @@ -export default function parseAspectRatio(input) { - // Handle 16x9, 16:9, 1.78x1, 1.78:1, 1.78 - // Ignore everything else - function parseOrThrow(num) { - const parsed = parseFloat(num); - if (isNaN(parsed)) { - throw new Error(`${num} is not a number`); - } - return parsed; +// Handle 16x9, 16:9, 1.78x1, 1.78:1, 1.78 +// Ignore everything else +const parseOrThrow = (num) => { + const parsed = parseFloat(num); + if (isNaN(parsed)) { + throw new Error(`${num} is not a number`); + } + return parsed; +}; + +export default function parseAspectRatio(input: string) { + if (!input) { + return null; } try { - if (input) { - const arr = input.replace(":", "x").split("x"); - if (arr.length === 0) { - return null; - } + if (input.substr(input.length - 1) === "%") { + return { w: 100, h: parseOrThrow(input.substr(0, input.length - 1)) }; + } - return arr.length === 1 - ? { w: parseOrThrow(arr[0]), h: 1 } - : { w: parseOrThrow(arr[0]), h: parseOrThrow(arr[1]) }; + const arr = input.replace(":", "x").split("x"); + if (arr.length === 0) { + return null; } + + return arr.length === 1 + ? { w: parseOrThrow(arr[0]), h: 1 } + : { w: parseOrThrow(arr[0]), h: parseOrThrow(arr[1]) }; } catch (err) { // Ignore the error } diff --git a/test-mocha/common/util/parse_aspect_ratio_test.ts b/test-mocha/common/util/parse_aspect_ratio_test.ts index 15b42a0cc161..04bff7189b6c 100644 --- a/test-mocha/common/util/parse_aspect_ratio_test.ts +++ b/test-mocha/common/util/parse_aspect_ratio_test.ts @@ -31,6 +31,11 @@ describe("parseAspectRatio", () => { assert.deepEqual(r, ratio178); }); + it("Parses 23%", () => { + const r = parseAspectRatio("23%"); + assert.deepEqual(r, { w: 100, h: 23 }); + }); + it("Skips null states", () => { const r = parseAspectRatio(null); assert.equal(r, null); From 2ba0986c112eaf16281236d051286db08f974d2e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 13 Dec 2018 12:14:06 +0100 Subject: [PATCH 2/3] Use endsWith --- src/common/util/parse-aspect-ratio.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/util/parse-aspect-ratio.ts b/src/common/util/parse-aspect-ratio.ts index 2de8f9b5034e..8fb1dee0a29c 100644 --- a/src/common/util/parse-aspect-ratio.ts +++ b/src/common/util/parse-aspect-ratio.ts @@ -13,7 +13,7 @@ export default function parseAspectRatio(input: string) { return null; } try { - if (input.substr(input.length - 1) === "%") { + if (input.endsWith("%")) { return { w: 100, h: parseOrThrow(input.substr(0, input.length - 1)) }; } From 0ca9f05aa89277973ffcf86e1ad1eb429786ed74 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 13 Dec 2018 21:48:14 +0100 Subject: [PATCH 3/3] Fix invalid test --- test-mocha/common/util/parse_aspect_ratio_test.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test-mocha/common/util/parse_aspect_ratio_test.ts b/test-mocha/common/util/parse_aspect_ratio_test.ts index 04bff7189b6c..758010aefa16 100644 --- a/test-mocha/common/util/parse_aspect_ratio_test.ts +++ b/test-mocha/common/util/parse_aspect_ratio_test.ts @@ -36,11 +36,6 @@ describe("parseAspectRatio", () => { assert.deepEqual(r, { w: 100, h: 23 }); }); - it("Skips null states", () => { - const r = parseAspectRatio(null); - assert.equal(r, null); - }); - it("Skips empty states", () => { const r = parseAspectRatio(" "); assert.equal(r, null);