From 21dfb4b5ae8fd7472218b68d50bdf419c79178db Mon Sep 17 00:00:00 2001 From: Julian <38703159+jesp1999@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:35:15 -0500 Subject: [PATCH] Fix snakecase converter for multi-numeric strings --- dataclasses_json/stringcase.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dataclasses_json/stringcase.py b/dataclasses_json/stringcase.py index da0f546f..7bc18091 100644 --- a/dataclasses_json/stringcase.py +++ b/dataclasses_json/stringcase.py @@ -95,10 +95,14 @@ def snakecase(string): string = re.sub(r"[\-\.\s]", '_', str(string)) if not string: return string - return (uplowcase(string[0], 'low') - + re.sub(r"[A-Z0-9]", - lambda matched: '_' + uplowcase(matched.group(0), 'low'), - string[1:])) + return uplowcase( + '_'.join( + re.sub( + '([A-Z][a-z]+)', r' \1', + re.sub('([A-Z]+)', r' \1', string.replace('-', ' ')) + ).split() + ), 'low' + ) def spinalcase(string):