From 46f7aad7e73215d4d064cdc78c87a9df4e064785 Mon Sep 17 00:00:00 2001 From: Tom Teichler <tom.teichler@teckids.org> Date: Mon, 15 Feb 2021 23:00:59 +0100 Subject: [PATCH] Add utility function to split string into css value and unit. --- dj_iconify/util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 dj_iconify/util.py diff --git a/dj_iconify/util.py b/dj_iconify/util.py new file mode 100644 index 0000000..c639245 --- /dev/null +++ b/dj_iconify/util.py @@ -0,0 +1,20 @@ +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 -- GitLab