mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-02 05:57:54 +00:00
Fix setting aspect ratio in percentage (#2289)
* Fix setting aspect ratio in percentage * Use endsWith * Fix invalid test
This commit is contained in:
parent
3a50d47dd2
commit
1a6226270f
@ -1,24 +1,30 @@
|
|||||||
export default function parseAspectRatio(input) {
|
// Handle 16x9, 16:9, 1.78x1, 1.78:1, 1.78
|
||||||
// Handle 16x9, 16:9, 1.78x1, 1.78:1, 1.78
|
// Ignore everything else
|
||||||
// Ignore everything else
|
const parseOrThrow = (num) => {
|
||||||
function parseOrThrow(num) {
|
const parsed = parseFloat(num);
|
||||||
const parsed = parseFloat(num);
|
if (isNaN(parsed)) {
|
||||||
if (isNaN(parsed)) {
|
throw new Error(`${num} is not a number`);
|
||||||
throw new Error(`${num} is not a number`);
|
}
|
||||||
}
|
return parsed;
|
||||||
return parsed;
|
};
|
||||||
|
|
||||||
|
export default function parseAspectRatio(input: string) {
|
||||||
|
if (!input) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (input) {
|
if (input.endsWith("%")) {
|
||||||
const arr = input.replace(":", "x").split("x");
|
return { w: 100, h: parseOrThrow(input.substr(0, input.length - 1)) };
|
||||||
if (arr.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
} catch (err) {
|
||||||
// Ignore the error
|
// Ignore the error
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,9 @@ describe("parseAspectRatio", () => {
|
|||||||
assert.deepEqual(r, ratio178);
|
assert.deepEqual(r, ratio178);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Skips null states", () => {
|
it("Parses 23%", () => {
|
||||||
const r = parseAspectRatio(null);
|
const r = parseAspectRatio("23%");
|
||||||
assert.equal(r, null);
|
assert.deepEqual(r, { w: 100, h: 23 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Skips empty states", () => {
|
it("Skips empty states", () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user