Skip to main content

Convert strings between camelCase, snake_case, PascalCase, kebab-case, and more

Project description

philiprehberger-str-case

Tests PyPI version License

Convert strings between camelCase, snake_case, PascalCase, kebab-case, and more.

Installation

pip install philiprehberger-str-case

Usage

from philiprehberger_str_case import to_snake, to_camel, to_pascal, to_kebab

to_snake("HTMLParser")       # "html_parser"
to_camel("html_parser")      # "htmlParser"
to_pascal("get-https-url")   # "GetHttpsUrl"
to_kebab("getHTTPSUrl")      # "get-https-url"

Acronym handling

Acronyms are split correctly:

from philiprehberger_str_case import to_snake

to_snake("HTMLParser")    # "html_parser"
to_snake("getHTTPSUrl")  # "get_https_url"
to_snake("XMLToJSON")    # "xml_to_json"

Number-aware splitting

Numbers at letter-digit boundaries are split into separate words:

from philiprehberger_str_case import to_snake, to_kebab

to_snake("html2json")      # "html_2_json"
to_snake("item123Name")    # "item_123_name"
to_kebab("v2Api")          # "v2-api"  (single-letter prefix stays attached)

More formats

from philiprehberger_str_case import to_constant, to_title, to_dot, to_path

to_constant("htmlParser")  # "HTML_PARSER"
to_title("html_parser")    # "Html Parser"
to_dot("htmlParser")       # "html.parser"
to_path("htmlParser")      # "html/parser"

Sentence case and Header-Case

from philiprehberger_str_case import to_sentence, to_header

to_sentence("html_parser")    # "Html parser"
to_sentence("getHTTPSUrl")    # "Get https url"

to_header("content_type")     # "Content-Type"
to_header("xForwardedFor")    # "X-Forwarded-For"

Convert dictionary keys

from philiprehberger_str_case import convert_keys, to_snake

data = {
    "firstName": "John",
    "lastName": "Doe",
    "contactInfo": {
        "emailAddress": "john@example.com",
        "phoneNumber": "555-1234",
    },
}

convert_keys(data, to_snake)
# {
#     "first_name": "John",
#     "last_name": "Doe",
#     "contact_info": {
#         "email_address": "john@example.com",
#         "phone_number": "555-1234",
#     },
# }

Lists of dicts are also supported:

records = [{"firstName": "Alice"}, {"firstName": "Bob"}]
convert_keys(records, to_snake)
# [{"first_name": "Alice"}, {"first_name": "Bob"}]

Detect case

from philiprehberger_str_case import detect_case, is_case

detect_case("my_variable")   # "snake_case"
detect_case("myVariable")    # "camelCase"
detect_case("MyVariable")    # "PascalCase"
detect_case("my-variable")   # "kebab-case"
detect_case("MY_VARIABLE")   # "CONSTANT_CASE"
detect_case("Content-Type")  # "Header-Case"
detect_case("Hello world")   # "Sentence case"

is_case("my_variable", "snake_case")  # True
is_case("my_variable", "camelCase")   # False

API

Function Description
to_snake(s) Convert to snake_case
to_camel(s) Convert to camelCase
to_pascal(s) Convert to PascalCase
to_kebab(s) Convert to kebab-case
to_constant(s) Convert to CONSTANT_CASE
to_title(s) Convert to Title Case
to_dot(s) Convert to dot.case
to_path(s) Convert to path/case
to_sentence(s) Convert to Sentence case
to_header(s) Convert to HTTP Header-Case
convert_keys(data, converter) Recursively convert dict/list keys
detect_case(s) Detect case convention of a string
is_case(s, case) Check if string matches a case convention

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_str_case-0.2.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_str_case-0.2.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_str_case-0.2.1.tar.gz.

File metadata

  • Download URL: philiprehberger_str_case-0.2.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_str_case-0.2.1.tar.gz
Algorithm Hash digest
SHA256 aeaa5a621eab083021853b3dca003fd844b7d02f2f2d902e134d7d15ba2238a2
MD5 9682757777309939697330b53c0de557
BLAKE2b-256 bca2146075aee6321b68baa399fd6011029f4d2ef75759e38f536f0b10e69a2d

See more details on using hashes here.

File details

Details for the file philiprehberger_str_case-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_str_case-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cebb7903034ed4bb8f8e23fb4b59bc9e898014c2a94113324a8f3d0d626a89e2
MD5 a417a8412b1a0b98be3d27d42f2248b2
BLAKE2b-256 e88c7445b930f3c65eb33b4eba8c6f3961be0702b056e8025da4d0ee4c089cde

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page