import re


def ocd_type_id(text, strip_leading_zeros=True):
    """
    Format a string in a way that's suitable for an OCD type ID

    Args:
        text: String to format.
        strip_leading_zeros: Remove leading zeros from name. Default is True.
            For example, '08' would become '8'.

    Returns:
        Formatted string.  See https://github.com/opencivicdata/ocd-division-ids
        for more on the Open Civic Data divsion identifier spec.

        * Valid characters are lowercase UTF-8 letters, numerals (0-9),
          period (.), hyphen (-), underscore (_), and tilde (~).
        * Characters should be converted to UTF-8.
        * Uppercase characters should be converted to lowercase.
        * Spaces should be converted to underscores.
        * All invalid characters should be converted to tildes (~).
        * Leading zeros should be dropped unless doing so changes the meaning
          of the identifier.
    """
    # Convert to lowercase
    text = text.lower()
    text = text.replace('(','')
    text = text.replace(')','')
    # Convert spaces to underscores
    text = re.sub(r'\s', '_', text)
    text = re.sub(r'[^\w.\-~]', '~', text)

    if strip_leading_zeros:
        # Remove leading zeros
        text = text.lstrip('0')

    return text