def normalize()

in facebook_business/adobjects/serverside/normalize.py [0:0]


    def normalize(field, data, hash_field):
        """Computes the normalized value for the given field type and data.

        :param field: The field name that is being normalized.
        :param data: The data that is being normalized.
        :return: Normalized value.
        :rtype: str
        """
        if field is None:
            raise TypeError('Field Type must be passed for Normalization')
        if data is None or len(data) == 0:
            return None

        normalized_data = data.lower().strip()
        if Normalize.is_already_hashed(normalized_data):
            return normalized_data

        if field == "em":
            normalized_data = Normalize.validate_email(normalized_data)

        elif field == "ct":
            # Remove numbers, space and period character
            normalized_data = location_excluded_chars.sub("", normalized_data)

        elif field == "zp":
            normalized_data = re.sub(r"\s","", normalized_data)
            normalized_data = normalized_data.split("-")[0]

        elif field == "st":
            # Remove numbers, space and period character
            normalized_data = location_excluded_chars.sub("", normalized_data)

        elif field == "country":
            # Remove any non-alpha characters from the data
            normalized_data = isocode_included_chars.sub("", normalized_data)
            if not Normalize.is_valid_country_code(normalized_data):
                raise TypeError("Invalid format for country:'" + data + "'.Please follow ISO 2-letter ISO 3166-1 standard for representing country. eg: us")

        elif field == "currency":
             # Remove any non-alpha characters from the data
            normalized_data = isocode_included_chars.sub("", normalized_data)
            if len(normalized_data) != 3:
                raise TypeError("Invalid format for currency:'" + data + "'.Please follow ISO 3-letter ISO 4217 standard for representing currency. Eg: usd")

        elif field == "ph":
            # Remove spaces and parenthesis within phone number
            normalized_data = re.sub(r"[\s\-()]", "", normalized_data)

            # Removes the starting + and leading two 0's
            normalized_data = re.sub(r"^\+?0{0,2}", "", normalized_data)

            international_number = Normalize.get_international_number(normalized_data)

            if international_number is None:
                raise ValueError("Invalid format for phone number:'" + normalized_data + "'. Please check passed phone number.")
            else:
                normalized_data = international_number

        elif field == "f5first" or field == "f5last":
            normalized_data = normalized_data[:5]

        elif field == "fi":
            normalized_data = normalized_data[:1]

        elif field == "dobd":
            if len(normalized_data) == 1:
                normalized_data = '0' + normalized_data

            try:
                dobd_int = int(normalized_data)
                if dobd_int < 1 or dobd_int > 31:
                    raise ValueError
            except ValueError:
                raise ValueError("Invalid format for dobd: '%s'. Day should be specified in 'DD' format." % data)

        elif field == "dobm":
            if len(normalized_data) == 1:
                normalized_data = '0' + normalized_data

            try:
                dobm_int = int(normalized_data)
                if dobm_int < 1 or dobm_int > 12:
                    raise ValueError
            except ValueError:
                raise ValueError("Invalid format for dobm: '%s'. Month should be specified in 'MM' format." % data)

        elif field == "doby":
            if not year_pattern.match(normalized_data):
                raise ValueError("Invalid format for doby: '%s'. Year should be specified in 'YYYY' format." % data)

        if hash_field:
            normalized_data = Normalize.hash_sha_256(normalized_data)

        return normalized_data