Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

convert-case

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

convert-case

Convert between string cases with built-in case inference.

  • 1.2.3
  • PyPI
  • Socket score

Maintainers
1

Convert Case

Convert between string cases with built-in case inference.

Status

SourceShields
Projectrelease license lines languages
Healthreadthedocs github_reviewcodacy codacy_coverage
Publisherspypi pypi_downloads
Repositoryissues issues_closed pulls pulls_closed
Activitycontributors monthly_commits last_commit

Installing

Install the package from pypi:

pip install convert-case

To experiment with the source code locally, clone the repository:

git clone https://github.com/joellefkowitz/convert-case

To install linters, formatters and test runners:

pip install .[all]
npm install

Usage

from convert_case import camel_case

camel_case('camel case') # Inferred: lower -> camel
'camelCase'

camel_case('Camel Case') # Inferred: title -> camel
'camelCase'

camel_case('CamelCase') # Inferred: pascal -> camel
'camelCase'

Exports

def pascal_case(string: str) -> str:
    ...
def is_pascal_case(string: str) -> bool:
    ...

def camel_case(string: str) -> str:
    ...
def is_camel_case(string: str) -> bool:
    ...

def kebab_case(string: str) -> str:
    ...
def is_kebab_case(string: str) -> bool:
    ...

def sentence_case(string: str) -> str:
    ...
def is_sentence_case(string: str) -> bool:
    ...

def snake_case(string: str) -> str:
    ...
def is_snake_case(string: str) -> bool:
    ...

def title_case(string: str) -> str:
    ...
def is_title_case(string: str) -> bool:
    ...

def upper_case(string: str) -> str:
    ...
def is_upper_case(string: str) -> bool:
    ...

Definitions

LOWER = re.compile(r"^[a-z0-9\s]*$")
UPPER = re.compile(r"^[A-Z0-9\s]*$")

TITLE = re.compile(r"^(([A-Z0-9][a-z0-9]*)(\s[A-Z0-9][a-z0-9]*)*)?$")
SENTENCE = re.compile(r"^(([A-Z0-9][a-z0-9]*)(\s[a-z0-9]*)*)?$")

CAMEL = re.compile(r"^([a-z0-9][a-zA-Z0-9]*)?$")
PASCAL = re.compile(r"^([A-Z0-9]|([A-Z0-9][a-z0-9]+)*)?$")

SNAKE = re.compile(r"^([a-z0-9]+(_[a-z0-9]+)*)?$")
KEBAB = re.compile(r"^([a-z0-9]+(-[a-z0-9]+)*)?$")

Tests

To run unit tests:

grunt test

Test cases

testloweruppersentencetitlecamelsnakekebabpascal
aaAAAaaaA
AaAAAaaaA
abcabcABCAbcAbcabcabcabcAbc
ab cdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
Ab cdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
Ab Cdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
ab_cdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
ab-cdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
abCdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
ABCDabcdABCDAbcdAbcdabcdabcdabcdAbcd
AbCdab cdAB CDAb cdAb CdabCdab_cdab-cdAbCd
ab cd efab cd efAB CD EFAb cd efAb Cd EfabCdEfab_cd_efab-cd-efAbCdEf
AbCdEfab cd efAB CD EFAb cd efAb Cd EfabCdEfab_cd_efab-cd-efAbCdEf
ab-cd-efab cd efAB CD EFAb cd efAb Cd EfabCdEfab_cd_efab-cd-efAbCdEf
Ab cd efab cd efAB CD EFAb cd efAb Cd EfabCdEfab_cd_efab-cd-efAbCdEf
Numbers

Numbers are treated as letters with no specific case.

testloweruppersentencetitlecamelsnakekebabpascal
111111111
1bc1bc1BC1bc1bc1bc1bc1bc1bc
a1ca1cA1CA1cA1ca1ca1ca1cA1c
ab1ab1AB1Ab1Ab1ab1ab1ab1Ab1
a1 ca1 cA1 CA1 cA1 Ca1Ca1_ca1-cA1C
a1-ca1 cA1 CA1 cA1 Ca1Ca1_ca1-cA1C

Advanced

A goal of this converter is that it is deterministic. If we consider the following examples we can see that this is not simple to achieve. How should we interpret the string 'ABC', is it in upper case or pascal case?

testupperpascal
abcABCAbc
a b cA B CABC

Our options are:

  • To consider strings with consecutive capitals like 'ABC' not to be pascal case. If in this case 'a b c' is parsed to 'Abc' it would clash with parsing 'abc' into pascal case.

  • To store some state that remembers the string's case before parsing. This would introduce too much complexity.

  • To prioritize parsing the string as one case unless told otherwise. We choose to pick upper case as the inferred case. The caveat here is that we will no longer be performing 'round trip' conversion.

Round trip conversion:

kebab_case('a b c')
'a-b-c'

lower_case('a-b-c')
'a b c'

Not round trip conversion:

pascal_case('a b c')
'ABC'

lower_case('ABC')
'abc'

Documentation

This repository's documentation is hosted on readthedocs.

Tooling

To run linters:

grunt lint

To run formatters:

grunt format

Continuous integration

This repository uses github actions to lint and test each commit. Formatting tasks and writing/generating documentation must be done before committing new code.

Versioning

This repository adheres to semantic versioning standards. For more information on semantic versioning visit SemVer.

Bump2version is used to version and tag changes. For example:

bump2version patch

Changelog

Please read this repository's changelog for details on changes that have been made.

Contributing

Please read this repository's guidelines on contributing for details on the process for submitting pull requests. Moreover, our code of conduct declares our collaboration standards.

Contributors

Buy Me A Coffee

Remarks

Lots of love to the open source community!

Be kind

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc