File: camel.py

package info (click to toggle)
python-humps 0.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: python: 94; makefile: 7; sh: 5
file content (53 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
    camel - Convert any string to Python compatible camelCase

    Copyright (C) 2018  Erik Huizinga (GitHub: erikhuizinga, huizinga.erik@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser Public License for more details.

    You should have received a copy of the GNU Lesser Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""


def case(string: str) -> str:
    """
    Convert string to camelCase. camelCase starts with a lower case alphabetic character, the rest of the string
    contains alphanumeric characters. Any character case in the input is ignored. Any spaces in the input capitalise
    the following character if alphabetic, except for the first character. Any non-alphanumeric characters are ignored.

    :param string: The input string.
    :return: The input converted to camelCase; empty ('') if there are no valid characters in the input string.
    :raises: AssertionError if the input is not of type str.
    """

    assert isinstance(string, str), 'Input must be of type str'

    first_alphabetic_character_index = -1
    for index, character in enumerate(string):
        if character.isalpha():
            first_alphabetic_character_index = index
            break

    empty = ''

    if first_alphabetic_character_index == -1:
        return empty

    string = string[first_alphabetic_character_index:]

    titled_string_generator = (character for character in string.title() if character.isalnum())

    try:
        return next(titled_string_generator).lower() + empty.join(titled_string_generator)

    except StopIteration:
        return empty