* Parse a height value with units into numeric value and unit string. * Supports px, em, rem, vh, vw, %, cm, mm units. * * @param val height value as number or string with units * @returns object with h (height) and unit properties * * @example * Utils.parseHeight('100px'); //
(val: numberOrString)
| 386 | * Utils.parseHeight(50); // {h: 50, unit: 'px'} |
| 387 | */ |
| 388 | static parseHeight(val: numberOrString): HeightData { |
| 389 | let h: number; |
| 390 | let unit = 'px'; |
| 391 | if (typeof val === 'string') { |
| 392 | if (val === 'auto' || val === '') h = 0; |
| 393 | else { |
| 394 | const match = val.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw|%|cm|mm)?$/); |
| 395 | if (!match) { |
| 396 | throw new Error(`Invalid height val = ${val}`); |
| 397 | } |
| 398 | unit = match[2] || 'px'; |
| 399 | h = parseFloat(match[1]); |
| 400 | } |
| 401 | } else { |
| 402 | h = val; |
| 403 | } |
| 404 | return { h, unit }; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Copy unset fields from source objects to target object (shallow merge with defaults). |
no outgoing calls
no test coverage detected