kute.js/src/util/trueDimension.js
thednp 2a5bac2bb3 Changes V2.2.0:
* major JSDoc write up
* removed ESLint `no-bitwise` exception, it only applies to specific functions and not the entire code
* the `SVGCubicMorph` component will remove un-necessary `Z` path commands when is the case for better out of the box animation
* fixed a minor disambiguation with `filterEffects` and `drop-shadow` property and its `dropshadow` interpolation function
* TypeScript strong: all files are modules, easy to implement in any third party app
* updated `CubicBezier` and SVGPathCommander
* code cleanup
2021-12-08 23:43:31 +02:00

28 lines
852 B
JavaScript

/**
* trueDimension
*
* Returns the string value of a specific CSS property converted into a nice
* { v = value, u = unit } object.
*
* @param {string} dimValue the property string value
* @param {boolean | number} isAngle sets the utility to investigate angles
* @returns {{v: number, u: string}} the true {value, unit} tuple
*/
const trueDimension = (dimValue, isAngle) => {
const intValue = parseInt(dimValue, 10) || 0;
const mUnits = ['px', '%', 'deg', 'rad', 'em', 'rem', 'vh', 'vw'];
let theUnit;
for (let mIndex = 0; mIndex < mUnits.length; mIndex += 1) {
if (typeof dimValue === 'string' && dimValue.includes(mUnits[mIndex])) {
theUnit = mUnits[mIndex]; break;
}
}
if (theUnit === undefined) {
theUnit = isAngle ? 'deg' : 'px';
}
return { v: intValue, u: theUnit };
};
export default trueDimension;