MCPcopy Index your code
hub / github.com/gridstack/gridstack.js / parseHeight

Method parseHeight

src/utils.ts:388–405  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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).

Callers 5

cellHeightMethod · 0.80
marginMethod · 0.80
_initMarginMethod · 0.80
utils-spec.tsFile · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected