You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

// @flow
const cssRegex = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)$/
/**
* Returns a given CSS value minus its unit of measure.
*
* @deprecated - stripUnit's unitReturn functionality has been marked for deprecation in polished 4.0. It's functionality has been been moved to getValueAndUnit.
*
* @example
* // Styles as object usage
* const styles = {
* '--dimension': stripUnit('100px')
* }
*
* // styled-components usage
* const div = styled.div`
* --dimension: ${stripUnit('100px')};
* `
*
* // CSS in JS Output
*
* element {
* '--dimension': 100
* }
*/
export default function stripUnit(value: string | number, unitReturn?: boolean): any {
if (typeof value !== 'string') return unitReturn ? [value, undefined] : value
const matchedValue = value.match(cssRegex)
if (unitReturn) {
// eslint-disable-next-line no-console
console.warn(
"stripUnit's unitReturn functionality has been marked for deprecation in polished 4.0. It's functionality has been been moved to getValueAndUnit.",
)
if (matchedValue) return [parseFloat(value), matchedValue[2]]
return [value, undefined]
}
if (matchedValue) return parseFloat(value)
return value
}