from typing import Union
import re


def join(*args: Union[None, str], trailing_slash: bool = False) -> str:
    """
    Return a url path joined from the arguments. It correctly handles blank/None
    arguments, and removes back-to-back slashes, eg::

        assert join('/', 'foo', None, 'bar', '', 'baz') == '/foo/bar/baz'
        assert join('/', '/foo', '/', '/bar/') == '/foo/bar'

    Note that it removes trailing slashes by default, so if you want to keep those,
    then you need to pass the ``trailing_slash`` keyword argument::

        assert join('/foo', 'baz', None, trailing_slash=True) == '/foo/baz/'
    """
    dirty_path = '/'.join(map(lambda x: x and x or '', args))
    path = re.sub(r'/+', '/', dirty_path)
    if path in {'', '/'}:
        return '/'
    path = path.rstrip('/')
    return path if not trailing_slash else path + '/'