Skip to content

Commit

Permalink
Merge pull request #47 from k7sleeper/feature-v3
Browse files Browse the repository at this point in the history
Make string functions None-safe and add tests.
  • Loading branch information
dgilland committed Jan 16, 2015
2 parents e611091 + 81904dc commit d1bb9bc
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 31 deletions.
21 changes: 16 additions & 5 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,24 @@ v3.0.0 (xxxx-xx-xx)
- Add ``to_number``.
- Add ``underscore_case`` as alias of ``snake_case``.
- Add ``unquote``.
- Make ``camel_case`` work with empty string.
- Make the following functions work with empty strings and ``None`` (**breaking change**):

- ``camel_case``, ``kebab_case`` and ``class_case``
- ``capitalize`` and ``decapitalize``
- ``chop`` and ``chop_right``
- ``lines`` and ``join``
- ``clean``
- ``chars``
- ``count_substr``
- ``ends_with``
- ``quote``, ``surround``

- Make ``capitalize`` accept ``lower_rest`` argument to determine whether to convert the rest of the string to lower case or not. Defaults to ``True``.
- Make ``splice`` work with strings.
- Make ``to_string`` convert ``None`` to empty string.
- Move ``arrays.join`` to ``strings.join``. (**breaking change**)
- Rename ``join``/``implode``'s second parameter from ``delimiter`` to ``separator``. (**breaking change**)
- Rename ``split``/``explode``'s second parameter from ``delimiter`` to ``separator``. (**breaking change**)
- Make ``to_string`` convert ``None`` to empty string (**breaking change**).
- Move ``arrays.join`` to ``strings.join`` (**breaking change**).
- Rename ``join``/``implode``'s second parameter from ``delimiter`` to ``separator`` (**breaking change**).
- Rename ``split``/``explode``'s second parameter from ``delimiter`` to ``separator`` (**breaking change**).


v2.4.1 (2015-01-11)
Expand Down
20 changes: 15 additions & 5 deletions pydash/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
.. versionadded:: 1.1.0
"""

from functools import partial
import re
import unicodedata

Expand Down Expand Up @@ -230,15 +229,17 @@ def chop(text, step):
Returns:
list: List of chopped characters.
If `text` is `None` an empty list is returned.
.. versionadded:: 3.0.0
"""
if text is None:
return []
text = pyd.to_string(text)
if step <= 0:
chopped = [text]
else:
chopped = [text[i:i + step] for i in _range(0, len(text), step)]

return chopped


Expand All @@ -254,14 +255,15 @@ def chop_right(text, step):
.. versionadded:: 3.0.0
"""
if text is None:
return []
text = pyd.to_string(text)
if step <= 0:
chopped = [text]
else:
text_len = len(text)
chopped = [text[-(i + step):text_len - i]
for i in _range(0, text_len, step)][::-1]

return chopped


Expand Down Expand Up @@ -306,7 +308,10 @@ def count_substr(text, subtext):
..versionadded:: 3.0.0
"""
if text is None or subtext is None:
return 0
text = pyd.to_string(text)
subtext = pyd.to_string(subtext)
return text.count(subtext)


Expand Down Expand Up @@ -358,6 +363,9 @@ def ends_with(text, target, position=None):
.. versionadded:: 1.1.0
"""
if text is None or target is None:
return False
target = pyd.to_string(target)
text = pyd.to_string(text)

if position is None:
Expand Down Expand Up @@ -519,7 +527,9 @@ def join(array, separator=''):
Modified :func:`implode` to have :func:`join` as main definition and
:func:`implode` as alias.
"""
return separator.join(pyd.map_(array, pyd.to_string))
return pyd.to_string(separator).join(pyd.map_(
array or tuple(), pyd.to_string)
)


implode = join
Expand Down Expand Up @@ -1137,7 +1147,7 @@ def surround(text, wrapper):
.. versionadded:: 2.4.0
"""
return '{1}{0}{1}'.format(text, wrapper)
return '{1}{0}{1}'.format(pyd.to_string(text), pyd.to_string(wrapper))


def swap_case(text):
Expand Down
Loading

0 comments on commit d1bb9bc

Please sign in to comment.