Skip to content

Commit 9973916

Browse files
committed
feat: Add string_trimmed_before
Added to support stripping parents from account name.
1 parent a19c522 commit 9973916

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/move2gnucash/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ def custom_join(strings: Series) -> LiteralString | Literal[""]:
4141
def string_trimmed_after(string: str, delimiter: str, occurrences=0) -> str:
4242
"""Function to trim a string after the nth occurrence of the specified delimiter.
4343
44-
If occurrence == 0, only the last word/segment is trimmed.
44+
If occurrences == 0, only the last word/segment is trimmed.
4545
"""
4646
groups: list[str] = string.split(delimiter)
4747
n_th: int = occurrences if occurrences >> 0 else len(groups) - 1
4848
return delimiter.join((string.split(delimiter))[:n_th])
4949

5050

51+
def string_trimmed_before(string: str, delimiter, occurrences=0) -> str:
52+
"""Function to trim a string before the nth occurrence of the specified delimiter.
53+
54+
If occurrences = 0, only the last word/segment is returned."""
55+
groups: list[str] = string.split(delimiter)
56+
n_th: int = occurrences if occurrences >> 0 else len(groups) - 1
57+
return delimiter.join((groups)[n_th:])
58+
59+
5160
def hierarchy_from(delimited_words: str) -> list[str]:
5261
"""
5362
Function to make list of all parents from a string delimited by a colon.

tests/unit/test_utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
decimal_to,
1111
hierarchy_from,
1212
string_trimmed_after,
13+
string_trimmed_before,
1314
)
1415

1516

@@ -50,6 +51,30 @@ def test_custom_join():
5051
assert custom_join(pd.Series([""])) == ""
5152

5253

54+
def test_string_trimmed_after():
55+
"""
56+
GIVEN a string of words separated by a specified delimiter and a specified number of occurrences,
57+
WHEN executed by string_trimmed_after,
58+
THEN a literal string is returned comprised of the left hand part of the original string
59+
before the nth occurrence of the delimiter.
60+
"""
61+
test_string = "foo:bar:doo:day"
62+
assert string_trimmed_after(test_string, ":") == "foo:bar:doo"
63+
assert string_trimmed_after(test_string, ":", 1) == "foo"
64+
65+
66+
def test_string_trimmed_before():
67+
"""
68+
GIVEN a string of words separated by a specified delimiter and a specified number of occurrences,
69+
WHEN executed by string_trimmed_before,
70+
THEN a literal string is returned comprised of the right hand part of the original string
71+
after the nth occurrence of the delimiter.
72+
"""
73+
test_string = "foo:bar:doo:day"
74+
assert string_trimmed_before(test_string, ":") == "day"
75+
assert string_trimmed_before(test_string, ":", 1) == "bar:doo:day"
76+
77+
5378
def test_hierarchy_from():
5479
"""
5580
GIVEN a string of words delimited by colons

0 commit comments

Comments
 (0)