From 025dff62ddc7e7d7ed9fa08a2be4d6645374c3c1 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 15 Apr 2016 22:49:55 +0200 Subject: [PATCH] server/misc: add icase_unique --- server/szurubooru/tests/util/test_misc.py | 11 +++++++++++ server/szurubooru/util/misc.py | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/server/szurubooru/tests/util/test_misc.py b/server/szurubooru/tests/util/test_misc.py index 01c667bd..c8c99cf2 100644 --- a/server/szurubooru/tests/util/test_misc.py +++ b/server/szurubooru/tests/util/test_misc.py @@ -28,3 +28,14 @@ def test_parsing_empty_date_time(): def test_parsing_date_time(input, output): misc.datetime.datetime = FakeDatetime assert misc.parse_time_range(input) == output + +@pytest.mark.parametrize('input,output', [ + ([], []), + (['a', 'b', 'c'], ['a', 'b', 'c']), + (['a', 'b', 'a'], ['a', 'b']), + (['a', 'a', 'b'], ['a', 'b']), + (['a', 'A', 'b'], ['a', 'b']), + (['a', 'A', 'b', 'B'], ['a', 'b']), +]) +def test_icase_unique(input, output): + assert misc.icase_unique(input) == output diff --git a/server/szurubooru/util/misc.py b/server/szurubooru/util/misc.py index 40955192..d51556a8 100644 --- a/server/szurubooru/util/misc.py +++ b/server/szurubooru/util/misc.py @@ -61,3 +61,12 @@ def parse_time_range(value, timezone=datetime.timezone(datetime.timedelta())): datetime.datetime(year, month, day + 1) - one_second) raise ValidationError('Invalid date format: %r.' % value) + +def icase_unique(source): + target = [] + target_low = [] + for source_item in source: + if source_item.lower() not in target_low: + target.append(source_item) + target_low.append(source_item.lower()) + return target