Skip to content
Snippets Groups Projects
Verified Commit a610a872 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: invalid input throws TypeError

parent 8c6c26a8
No related branches found
No related tags found
No related merge requests found
...@@ -70,11 +70,19 @@ function getDeviceDPI() { ...@@ -70,11 +70,19 @@ function getDeviceDPI() {
* @copyright schukai GmbH * @copyright schukai GmbH
* @throws {Error} Unsupported unit * @throws {Error} Unsupported unit
* @memberOf Monster.DOM * @memberOf Monster.DOM
* @throws {Error} Invalid value format
*/ */
function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) { function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) {
const regex = /^([\d.]+)(.*)$/; const regex = /^([\d.]+)(.*)$/;
const [, num, unit] = value.match(regex); const matchResult = value.match(regex);
if (!matchResult) {
throw new Error(`Invalid value format: ${value}`);
}
const [, num, unit] = matchResult;
const number = parseFloat(num); const number = parseFloat(num);
const dpi = getDeviceDPI(); const dpi = getDeviceDPI();
......
...@@ -61,6 +61,16 @@ describe('dimension', () => { ...@@ -61,6 +61,16 @@ describe('dimension', () => {
expect(result).to.equal(100); expect(result).to.equal(100);
}); });
it("should throw an error when the input value has an invalid format", () => {
const invalidValue = "invalid_value";
const errorFn = () => {
convertToPixels(invalidValue);
};
expect(errorFn).to.throw(Error, `Invalid value format: ${invalidValue}`);
});
it('should correctly convert em values', () => { it('should correctly convert em values', () => {
const testElement = document.createElement('div'); const testElement = document.createElement('div');
testElement.style.fontSize = '16px'; testElement.style.fontSize = '16px';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment