Skip to content
Snippets Groups Projects
Verified Commit 46f7aad7 authored by Tom Teichler's avatar Tom Teichler :beers:
Browse files

Add utility function to split string into css value and unit.

parent 97870e4f
No related branches found
No related tags found
1 merge request!2Add utility function to split string into css value and unit.
import re
def split_css_unit(string: str):
"""Split string into value and unit.
>>> split_css_unit("12px")
(12, 'px')
>>> split_css_unit("1.5em")
(1.5, 'em')
>>> split_css_unit("18%")
(18, '%')
>>> split_css_unit("200")
(200, '')
"""
_value = re.findall("^[0-9.]+", string)
value = float(_value[0]) if "." in _value[0] else int(_value[0])
unit = string[len(_value[0]):]
return value, unit
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment