Skip to content

Commit dff1721

Browse files
committed
feat(strings): add in_many
1 parent 8a489fa commit dff1721

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

iarp_utils/strings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ def find_between(value: str, first: str, second: str):
9090
return value[data_start:]
9191

9292

93+
def in_many(value: str, finders: list) -> bool:
94+
""" Checks if value contains any of the finders
95+
96+
Args:
97+
value: The string to search in
98+
finders: A list of strings to search for within value
99+
100+
Returns:
101+
boolean if value contains any of the finders
102+
"""
103+
for f in finders:
104+
if f in value:
105+
return True
106+
return False
107+
108+
93109
def slugify(value, replace_with='-', allow_unicode=False, lowercase=True):
94110
""" Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
95111
Remove characters that aren't alphanumerics, underscores, or hyphens.

tests/test_strings.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from iarp_utils.strings import slugify, find_between, replace_all, random_character_generator
3+
from iarp_utils.strings import slugify, find_between, in_many, replace_all, random_character_generator
44

55

66
class StringsTests(unittest.TestCase):
@@ -51,3 +51,27 @@ def test_slugify_default_allow_unicode(self):
5151

5252
def test_slugify_custom_replace_with(self):
5353
self.assertEqual('input/with/spaces', slugify('input with spaces', replace_with='/'))
54+
55+
def test_in_many_at_least_one_match(self):
56+
find_within_this = "Show string finale"
57+
find_one_of_these = [
58+
" finale",
59+
" string",
60+
]
61+
self.assertTrue(in_many(find_within_this, find_one_of_these))
62+
63+
def test_in_many_ensure_space_included(self):
64+
find_within_this = "Show finale"
65+
find_one_of_these = [
66+
" finale",
67+
" string",
68+
]
69+
self.assertTrue(in_many(find_within_this, find_one_of_these))
70+
71+
def test_in_many_no_matches(self):
72+
find_within_this = "showfinale"
73+
find_one_of_these = [
74+
" finale",
75+
" string",
76+
]
77+
self.assertFalse(in_many(find_within_this, find_one_of_these))

0 commit comments

Comments
 (0)