UNPKG

@smart-react-components/ui

Version:
30 lines (29 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toCSSValue = exports.extractNumberUnit = void 0; /** * Takes a CSS number value as JavaScript string or number. * Extracts the unit and returns the number part of the value. */ const extractNumberUnit = (input) => typeof input === 'number' ? input : Number(input.replace(/[A-z|%]+/, '')); exports.extractNumberUnit = extractNumberUnit; /** * Takes a CSS number as JavaScript string or number. * Splits the given input in two as value and unit parts. * Provides a callback with value of the input. * Returns the returned value from the callback along with unit of the input. */ const toCSSValue = (input) => { let value; let unit; if (typeof input === 'number') { value = input; unit = 'px'; } else { value = Number(input.replace(/[A-z|%]+/, '')); unit = input.replace(/[\d|.]+/, ''); } return callback => `${callback(value)}${unit}`; }; exports.toCSSValue = toCSSValue;