@schukai/monster 3.35.0 → 3.35.3
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
package/source/dom/dimension.mjs
CHANGED
@@ -70,11 +70,18 @@ function getDeviceDPI() {
|
|
70
70
|
* @copyright schukai GmbH
|
71
71
|
* @throws {Error} Unsupported unit
|
72
72
|
* @memberOf Monster.DOM
|
73
|
+
* @throws {Error} Invalid value format
|
73
74
|
*/
|
74
75
|
|
75
76
|
function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) {
|
76
|
-
const regex = /^([\d.]+)(.*)$/;
|
77
|
-
const
|
77
|
+
const regex = /^(-?[\d.]+)(.*)$/;
|
78
|
+
const matchResult = value.match(regex);
|
79
|
+
|
80
|
+
if (!matchResult) {
|
81
|
+
throw new Error(`Invalid value format: ${value}`);
|
82
|
+
}
|
83
|
+
|
84
|
+
const [, num, unit] = matchResult;
|
78
85
|
const number = parseFloat(num);
|
79
86
|
const dpi = getDeviceDPI();
|
80
87
|
|
package/source/types/version.mjs
CHANGED
@@ -61,6 +61,25 @@ describe('dimension', () => {
|
|
61
61
|
expect(result).to.equal(100);
|
62
62
|
});
|
63
63
|
|
64
|
+
it("should throw an error when the input value has an invalid format", () => {
|
65
|
+
const invalidValue = "invalid_value";
|
66
|
+
|
67
|
+
const errorFn = () => {
|
68
|
+
convertToPixels(invalidValue);
|
69
|
+
};
|
70
|
+
|
71
|
+
expect(errorFn).to.throw(Error, `Invalid value format: ${invalidValue}`);
|
72
|
+
});
|
73
|
+
|
74
|
+
it("should handle negative values correctly", () => {
|
75
|
+
const negativeValue = "-10px";
|
76
|
+
const expectedResult = -10;
|
77
|
+
|
78
|
+
const result = convertToPixels(negativeValue);
|
79
|
+
|
80
|
+
expect(result).to.equal(expectedResult);
|
81
|
+
});
|
82
|
+
|
64
83
|
it('should correctly convert em values', () => {
|
65
84
|
const testElement = document.createElement('div');
|
66
85
|
testElement.style.fontSize = '16px';
|
package/test/cases/monster.mjs
CHANGED