Skip to content

Commit

Permalink
Merge pull request #46 from k7sleeper/feature-v3
Browse files Browse the repository at this point in the history
Make to_string() convert None to empty string. Make capitalize() use to_string().
  • Loading branch information
dgilland committed Jan 14, 2015
2 parents d25582d + f342196 commit e2d870d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pydash/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,13 @@ def to_string(obj):
.. versionadded:: 2.0.0
"""
return text_type(obj) if not pyd.is_string(obj) else obj
if pyd.is_string(obj):
res = obj
elif obj is None:
res = ''
else:
res = text_type(obj)
return res


def transform(obj, callback=None, accumulator=None):
Expand Down
1 change: 1 addition & 0 deletions pydash/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def capitalize(text, lower_rest=True):
.. versionchanged:: 3.0.0
Added `lower_rest` option.
"""
text = pyd.to_string(text)
return (text.capitalize() if lower_rest
else text[:1].upper() + text[1:])

Expand Down
8 changes: 8 additions & 0 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# -*- coding: utf-8 -*-

import datetime as dt

import pydash as _

from . import fixtures
from .fixtures import parametrize


today = dt.date.today()


@parametrize('case,expected', [
(({'name': 'fred'}, {'employer': 'slate'}),
{'name': 'fred', 'employer': 'slate'}),
Expand Down Expand Up @@ -498,6 +503,9 @@ def test_to_number(case, expected):
(True, 'True'),
([1], '[1]'),
('d\xc3\xa9j\xc3\xa0 vu', 'd\xc3\xa9j\xc3\xa0 vu'),
('', ''),
(None, ''),
(today, str(today)),
])
def test_to_string(case, expected):
assert _.to_string(case) == expected
Expand Down
6 changes: 6 additions & 0 deletions tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
('foo-_bar-_-baz', 'fooBarBaz'),
('foo!bar,baz', 'fooBarBaz'),
('--foo.bar;baz', 'fooBarBaz'),
(8, '8'),
('', ''),
(None, ''),
])
def test_camel_case(case, expected):
assert _.camel_case(case) == expected
Expand All @@ -29,6 +31,10 @@ def test_camel_case(case, expected):
(('foo bar', False), 'Foo bar'),
(('fOO bar', False), 'FOO bar'),
(('foo Bar', False), 'Foo Bar'),
((8,), '8'),
((' ',), ' '),
(('',), ''),
((None,), ''),
])
def test_capitalize(case, expected):
assert _.capitalize(*case) == expected
Expand Down

0 comments on commit e2d870d

Please sign in to comment.