UNPKG

rn-custom-style-sheet

Version:

React Native component to select a specific value from a range of values.

107 lines (99 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.scaleFunc = scaleFunc; var _BreakPoint = require("../../../BreakPoint"); var _Core = require("../../../Core"); var _Metrics = require("../Metrics"); // Groups Size Unit Func Factor threshold SkipAspectRatio Round // Index 1 2 3/4/6/10 5/7 9/12 13 14 const validScaleSheetRegex = new RegExp( // eslint-disable-next-line no-useless-escape /^(\-?\d+(?:\.\d{1,3})?)(em|rem|px|cm|mm|in|pt|pc)?@(hs|vs|wp|hp|vh|vw|vmin|vmax|sdp|ssp|(mvs|mhs)(\d+(?:\.\d{1,2})?)?|(ms)(\d+(?:\.\d{1,2})?)?(t(\d+(?:\.\d{1,2})?))?|(s)(t(\d+(?:\.\d{1,2})?))?)(sar)?(r)?$/i); function scaleFunc(screenResolution, guideLineLayout, guideLineBreakpoint, generalScreenResolution, currentBreakpointIndex) { return function scalingFn(value) { let derivedValue = value; if ((0, _BreakPoint.hasValidBreakpointPropertyFormat)(derivedValue, guideLineBreakpoint)) { derivedValue = (0, _BreakPoint.getBreakpointValueByIndex)(derivedValue, currentBreakpointIndex, guideLineBreakpoint); } try { if (!validScaleSheetRegex.test(derivedValue)) { return derivedValue; } const regexExecResult = validScaleSheetRegex.exec(derivedValue); // get the size of the component const size = parseFloat(regexExecResult?.[1] ?? '0'); // get the unit of the value const unit = regexExecResult?.[2]?.toLowerCase() ?? _Core.UnitEnum.Px; // get the scale factor for font or it's string or undefined values const scaleFactor1 = regexExecResult?.[5]; const scaleFactor2 = regexExecResult?.[7]; const finalScaleFactor = scaleFactor1 ?? scaleFactor2; // get the scale threshold or it's string or undefined values const threshold1 = regexExecResult?.[9]; const threshold2 = regexExecResult?.[12]; const finalThreshold = threshold1 ?? threshold2; // get the scale function const scaleFuncNameIndex = scaleFactor1 ? 4 : scaleFactor2 ? 6 : threshold2 ? 10 : 3; const scaleFuncName = regexExecResult?.[scaleFuncNameIndex]?.toLowerCase() ?? ''; // skip the aspect ratio or not const skipAspectRatio = regexExecResult?.[13]?.toLowerCase() === 'sar'; // should result is round or not const shouldRound = regexExecResult?.[14]?.toLowerCase() === 'r'; const convertSize = (0, _Core.toPx)(size, unit); const threshold = finalThreshold ? parseFloat(finalThreshold) : undefined; const scaleFactor = parseFloat(finalScaleFactor ?? '0.5'); let result; switch (scaleFuncName) { case 's': result = (0, _Metrics.scale)(guideLineLayout, generalScreenResolution)(convertSize, skipAspectRatio, threshold); break; case 'ms': result = (0, _Metrics.moderateScale)(guideLineLayout, generalScreenResolution)(convertSize, skipAspectRatio, threshold, scaleFactor); break; case 'hs': result = (0, _Metrics.horizontalScale)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'vs': result = (0, _Metrics.verticalScale)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'mhs': result = (0, _Metrics.moderateHorizontalScale)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio, scaleFactor); break; case 'mvs': result = (0, _Metrics.moderateVerticalScale)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio, scaleFactor); break; case 'hp': result = (0, _Metrics.heightPercentageToDP)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'wp': result = (0, _Metrics.widthPercentageToDP)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'vmin': result = (0, _Metrics.viewportMin)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'vmax': result = (0, _Metrics.viewportMax)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'vh': result = (0, _Metrics.viewportHeight)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'vw': result = (0, _Metrics.viewportWidth)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'sdp': result = (0, _Metrics.sdp)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; case 'ssp': result = (0, _Metrics.ssp)(screenResolution, guideLineLayout)(convertSize, skipAspectRatio); break; default: } return shouldRound ? Math.round(result ?? 0) : result; } catch (e) { return derivedValue; } }; } //# sourceMappingURL=Scale.js.map