luckywood.string.String.decode_to_url()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 33
rs 9.352
c 0
b 0
f 0
cc 2
nop 1
1
"""
2
String class for string operations
3
"""
4
5
__author__ = "Glücks GmbH - Frederik Glücks"
6
__email__ = "[email protected]"
7
__copyright__ = "Copyright 2020, Glücks GmbH"
8
9
10
# Generic/Built-in Imports
11
import re
12
from typing import Dict
13
14
15
class String:
16
    """String class for string operations"""
17
18
    @staticmethod
19
    def decode_to_url(raw_string: str) -> str:
20
        """Decode a string to a url string
21
22
        :param raw_string: String to decode
23
        :return: Decoded string
24
        """
25
26
        url_string = raw_string.lower()
27
28
        decode_chars: Dict[str, str] = {
29
            ", ": "_",
30
            "+": "_",
31
            "/": "_",
32
            " ": "_",
33
            "ä": "ae",
34
            "ü": "ue",
35
            "ö": "oe",
36
            "ß": "ss",
37
            ",": ".",
38
            "é": "e",
39
            "&": "",
40
            "(": "",
41
            ")": ""
42
        }
43
44
        for char in decode_chars:
45
            url_string = url_string.replace(char, decode_chars[char])
46
47
        url_string = re.sub(r"([^a-z0-9_\-.]*)", "", url_string)
48
        url_string = re.sub(r"([_]+)", "_", url_string)
49
50
        return url_string
51