Skip to content

Commit

Permalink
strutil: Add 'nym' function
Browse files Browse the repository at this point in the history
Helper function for dealing with names while avoiding pesky
things like quoting, spaces, and case sensitivity
  • Loading branch information
lhh committed Aug 24, 2023
1 parent 8cbe4b9 commit d43e050
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/test_strutil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from toolchest.strutil import list_or_splitstr
from toolchest.strutil import list_or_splitstr, nym


class test_list_or_splitstr(unittest.TestCase):
Expand All @@ -23,3 +23,15 @@ def test_normal(self):
self.assertEqual(list_or_splitstr('''a
b c'''), ['a', 'b', 'c'])
self.assertEqual(list_or_splitstr('a b c'), ['a', 'b', 'c'])


class test_nym(unittest.TestCase):

def test_identity(self):
self.assertEqual(nym('abc'), 'abc')

def test_spaces(self):
self.assertEqual(nym('abc def'), 'abc_def')

def test_lowercase(self):
self.assertEqual(nym('aBcDeF'), 'abcdef')
6 changes: 6 additions & 0 deletions toolchest/strutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ def regex_match(pattern, arg):
if re.match(pattern, arg) is not None:
return True
return False


def nym(arg):
ret = arg.lower().replace(' ', '_')
ret.replace('\t', '_')
return ret

0 comments on commit d43e050

Please sign in to comment.