UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

87 lines (73 loc) 2.44 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import isNumber from 'lodash/isNumber'; import isString from 'lodash/isString'; import cssUnitConverter from 'css-unit-converter'; export function pxToRem(num) { if (num !== undefined && num !== null) { if (num === 0) { return '0'; } if (isNumber(num)) { return "".concat(num / 100, "rem"); } return num; } } var builtInHeight = ['auto', 'fit-content', 'max-content', 'min-content', 'inherit', 'initial', 'unset', 'revert', 'available', '-webkit-fit-content', '-moz-max-content', '-moz-min-content', '-moz-initial']; var calcReg = /^calc\(([^()]*)\)$/; var unitReg = /^(-?[\d.]+)([^\d.-]+)$/; export function isCalcSize(num) { return num.match(calcReg); } export function toPx(num) { if (num !== undefined && num !== null) { if (isNumber(num)) { return num; } if (isString(num) && !builtInHeight.includes(num) && !num.endsWith('%')) { var calcMatches = isCalcSize(num); if (calcMatches) { try { var list = calcMatches[1].split(' '); return list.slice(1).reduce(function (result, calc, index, array) { if (index % 2 === 1) { /* eslint-disable-next-line no-eval */ return eval("".concat(result).concat(array[index - 1]).concat(toPx(calc))); } return result; }, toPx(list[0])); } catch (e) { console.error("Invalid property value in \"".concat(num, "\".")); return undefined; } } var unitMatches = num.match(unitReg); if (unitMatches) { var _unitMatches = _slicedToArray(unitMatches, 3), n = _unitMatches[1], u = _unitMatches[2]; if (n) { var number = Number(n); if (u && u !== 'px') { if (u === 'vh') { return document.documentElement.clientHeight * number / 100; } if (u === 'vw') { return document.documentElement.clientWidth * number / 100; } if (['rem', 'em'].includes(u)) { return number * 100; } try { return cssUnitConverter(number, u, 'px'); } catch (e) { return undefined; } } return number; } } } } } //# sourceMappingURL=UnitConvertor.js.map