Skip to content

Commit

Permalink
Added arrays.is_empty() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 25, 2016
1 parent 8f12628 commit 54ac723
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
- Add ``includes()``.
- Add ``is_array()``.
- Add ``is_boolean()``.
- Add ``is_empty()``.
- Add ``is_number()``.
- Add ``is_string()``.

Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
first,
from_,
includes,
is_empty,
subtract
)

Expand Down
21 changes: 21 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down

0 comments on commit 54ac723

Please sign in to comment.