Skip to content

Commit

Permalink
feat(app): Provide date 'collapsing' for same-month ranges
Browse files Browse the repository at this point in the history
Closes #45
  • Loading branch information
alexpovel committed Sep 3, 2022
1 parent b6c97b5 commit 5e4cc52
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ancv/visualization/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def date_range(
fmt: str,
sep: str = "-",
ongoing: str = "present",
collapse: bool = True,
) -> str:
if start is None:
if end is None:
Expand All @@ -31,6 +32,10 @@ def date_range(
if end is None:
return f"{start.strftime(fmt)} {sep} {ongoing}"

collapsible = start.month == end.month and start.year == end.year
if collapsible and collapse:
return f"{end.strftime(fmt)}"

return f"{start.strftime(fmt)} {sep} {end.strftime(fmt)}"


Expand Down
73 changes: 71 additions & 2 deletions tests/visualization/test_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@pytest.mark.parametrize(
["start", "end", "fmt", "sep", "ongoing", "expected"],
["start", "end", "fmt", "sep", "ongoing", "expected", "collapse"],
[
(
None,
Expand All @@ -16,6 +16,7 @@
"",
"",
"",
False,
),
(
None,
Expand All @@ -24,6 +25,7 @@
"",
"",
"",
False,
),
(
None,
Expand All @@ -32,6 +34,7 @@
"-",
"",
"",
False,
),
(
None,
Expand All @@ -40,6 +43,7 @@
"-",
"present",
"",
False,
),
(
date(2020, 1, 1),
Expand All @@ -48,6 +52,7 @@
"",
"",
"2020-01-01 ",
False,
),
(
date(2020, 1, 10),
Expand All @@ -56,6 +61,7 @@
"",
"",
"2020-01-10 ",
False,
),
(
date(2020, 1, 1),
Expand All @@ -64,6 +70,7 @@
"-",
"",
"2020-01-01 - ",
False,
),
(
date(2020, 1, 1),
Expand All @@ -72,6 +79,7 @@
"-",
"present",
"2020-01-01 - present",
False,
),
(
date(2020, 1, 1),
Expand All @@ -80,6 +88,7 @@
"...",
"present",
"2020-01-01 ... present",
False,
),
(
date(2020, 1, 1),
Expand All @@ -88,6 +97,7 @@
"",
"",
"2020-01-01 2020-01-02",
False,
),
(
date(2020, 1, 1),
Expand All @@ -96,6 +106,7 @@
"-",
"",
"2020-01-01 - 2020-01-02",
False,
),
(
None,
Expand All @@ -104,6 +115,7 @@
"",
"",
" 2020-01-02",
False,
),
(
None,
Expand All @@ -112,6 +124,7 @@
"-",
"",
"- 2020-01-02",
False,
),
(
None,
Expand All @@ -120,6 +133,61 @@
"-",
"present",
"- 2020-01-02",
False,
),
(
date(2020, 1, 1),
date(2020, 1, 2),
"%Y-%m",
"",
"",
"2020-01",
True,
),
(
date(2020, 1, 1),
date(2020, 1, 2),
"%Y-%m",
"-",
"",
"2020-01",
True,
),
(
date(2020, 1, 1),
date(2020, 1, 31),
"%Y-%m",
"-",
"",
"2020-01",
True,
),
(
date(2020, 1, 1),
date(2020, 2, 1),
"%Y-%m",
"-",
"",
"2020-01 - 2020-02",
True,
),
(
date(2020, 12, 1),
date(2020, 12, 31),
"%B %Y",
"-",
"",
"December 2020",
True,
),
(
date(2020, 1, 1),
date(2020, 2, 1),
"%B %Y",
"-",
"",
"January 2020 - February 2020",
True,
),
],
)
Expand All @@ -130,5 +198,6 @@ def test_default_date_range(
sep: str,
ongoing: str,
expected: str,
collapse: bool,
) -> None:
assert Theme.date_range(start, end, fmt, sep, ongoing) == expected
assert Theme.date_range(start, end, fmt, sep, ongoing, collapse) == expected

0 comments on commit 5e4cc52

Please sign in to comment.