Skip to content
Permalink
Browse files
Improving smartjoin for non-strings.
  • Loading branch information
felixfontein committed May 9, 2018
1 parent 74faaba commit 5b7e8002f0e93d5ce929d879fe960c8b9ffbb183
Showing with 6 additions and 4 deletions.
  1. +6 −4 nikola/utils.py
@@ -1870,17 +1870,19 @@ def sort_posts(posts, *keys):


def smartjoin(join_char: str, string_or_iterable) -> str:
"""Join string_or_iterable with join_char if it is not a string already.
"""Join string_or_iterable with join_char if it is iterable; otherwise converts it to string.
>>> smartjoin('; ', 'foo, bar')
'foo, bar'
>>> smartjoin('; ', ['foo', 'bar'])
'foo; bar'
>>> smartjoin(' to ', ['count', 42])
'count to 42'
"""
if isinstance(string_or_iterable, (unicode_str, bytes_str)):
return string_or_iterable
if hasattr(string_or_iterable, '__iter__'):
return join_char.join([str(e) for e in string_or_iterable])
else:
return join_char.join(string_or_iterable)
return str(string_or_iterable)


def _smartjoin_filter(string_or_iterable, join_char: str) -> str:

0 comments on commit 5b7e800

Please sign in to comment.