diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fa758e0..78c88c4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ Changelog - Add ``includes()``. - Add ``is_array()``. - Add ``is_boolean()``. +- Add ``is_empty()``. - Add ``is_number()``. - Add ``is_string()``. diff --git a/README.rst b/README.rst index 7004931..1c5846b 100644 --- a/README.rst +++ b/README.rst @@ -129,6 +129,11 @@ Install using pip: >>> _.includes([11, 22, 33], 22, -2) True + >>> _.is_empty([]) + True + >>> _.is_empty([None]) + False + >>> _.subtract([1, 2, 3], 2) [1, 3] >>> _.subtract ([1, 2, 3], [1, 3]) diff --git a/sugar/__init__.py b/sugar/__init__.py index 03e0ef0..1ded3c0 100644 --- a/sugar/__init__.py +++ b/sugar/__init__.py @@ -26,6 +26,7 @@ first, from_, includes, + is_empty, subtract ) diff --git a/sugar/arrays.py b/sugar/arrays.py index a4f2c14..dcc44ad 100644 --- a/sugar/arrays.py +++ b/sugar/arrays.py @@ -402,6 +402,27 @@ def includes(array, search, fromindex=0): return search in array[fromindex:] +def is_empty(array): + """Returns True if the :attr:`array` has a length of zero. + + Args: + array (list): A list of values provided by the user. + + Returns: + bool: True if the list is empty else False. + + Example: + + >>> is_empty([]) + True + >>> is_empty([None]) + False + + .. versionadded:: TODO + """ + return True if not array else False + + def subtract(array, item): """Subtracts :attr:`item` from the :attr:`array` and returns the result as a new array. If :attr:`item` is also an array, all elements in it will diff --git a/tests/test_arrays.py b/tests/test_arrays.py index 8584abb..95ae329 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -150,6 +150,15 @@ def test_includes(array, search, fromindex, expected): assert _.includes(array, search, fromindex) == expected +@parametrize('array,expected', [ + ([], True), + ([None], False), + ([1, 2], False) +]) +def test_is_empty(array, expected): + assert _.is_empty(array) == expected + + @parametrize('array,item,expected', [ ([1, 2, 3], 3, [1, 2]), ([1, 2, 3], [1, 3], [2]),