diff --git a/asfsmd/_utils.py b/asfsmd/_utils.py new file mode 100644 index 0000000..aada29f --- /dev/null +++ b/asfsmd/_utils.py @@ -0,0 +1,14 @@ +"""Utility functions for asfsmd.""" + +from typing import Any, Iterable, List + + +def unique(data: Iterable[Any]) -> List[Any]: + """Return a list of unique items preserving the input ordering.""" + unique_items = [] + unique_items_set = set() + for item in data: + if item not in unique_items_set: + unique_items.append(item) + unique_items_set.add(item) + return unique_items diff --git a/tests/test__utils.py b/tests/test__utils.py new file mode 100644 index 0000000..f8c118a --- /dev/null +++ b/tests/test__utils.py @@ -0,0 +1,23 @@ +"""Unit tests for the `asfsmd._utils` module.""" + +import itertools +from asfsmd._utils import unique + +import pytest + + +@pytest.mark.parametrize( + ["in_", "out"], + [ + pytest.param(["a", "b", "c"], ["a", "b", "c"], id="unique-list"), + pytest.param(["a", "b", "c", "b"], ["a", "b", "c"], id="list"), + pytest.param((1, 2, 2, 3, 1, 2), [1, 2, 3], id="tuple"), + pytest.param(range(3), [0, 1, 2], id="generator"), + pytest.param( + itertools.chain(range(3, 0, -1), range(3)), + [3, 2, 1, 0], + id="reversed-generator"), + ], +) +def test_unique(in_, out): + assert unique(in_) == out