import re
import six


def remove_whitespace(text):
    """ Collapses duplicated spaces, tabs and line-breaks, strips the result. """
    text = re.sub('\n+', '\n', text)

    # Get rid of duplicate spaces
    text = re.sub(r' +', ' ', text)

    text = re.sub(r"\t+", "\t", text)
    return six.text_type(text).strip()