Skip to main content

CLI that converts the case of strings.

Project description

casec

This tool was made to fix the problem of converting variable or field names from one type of casing to another, for instance from camel case to snake case or from pascal case to kebab case. There are often times when consuming data from an API or database which require converting field names to another case in order to adhere to a style guide. This can become very tedious very quickly. A regular expression may be able to solve the problem once, but this will only convert one case to another. You would need several regular expressions for each conversion if you were to want to handle multiple naming conventions. casec makes all of this much easier.

Converting one naming convention to another reduces to two basic problems. Parsing a delimited string into words and formatting words into a delimited string. The delimiter in this case can either be a character, such as an underscore or dash, or the case of a character.

There is also some nuance in naming conventions that is difficult to account for with regular expressions, especially concerning camel case and pascal case. How should acronyms be handled? Should a camel cased name have "VPN" formatted in all caps or as "Vpn"? What of proper nouns with a standard casing such as "iOS"? Should it be formatted as "IOs" in pascal case? When converting a camel cased name, "iOSUsername," to snake case, how should "iOS" be parsed? Should the result be "ios_username" or "i_os_username"? The answers to these questions can vary based on the style guide you're using. One the goals of casec is to handle situations like these dynamically in straightforward way.

Installation

$ pip install casec

Usage

Basic usage is as simple as casec snake-case -c -f <file name>. This converts a file of snake cased names to camel-case.

$ cat some_snake_cased_names
login
id
node_id
avatar_url
gravatar_id
url
html_url
$ casec snake-case -c -f some_camel_cased_names
login
id
nodeId
avatarUrl
gravatarId
url
htmlUrl

Other cases are just as simple.

$ cat some_pascal_cased_names
Login
Id
NodeId
AvatarUrl
GravatarId
Url
HtmlUrl
$ casec pascal-case -c -f some_pascal_cased_names
login
id
nodeId
avatarUrl
gravatarId
url
htmlUrl
$ casec pascal-case -s -f some_pascal_cased_names
login
id
node_id
avatar_url
gravatar_id
url
html_url
$ casec pascal-case -k -f some_pascal_cased_names
login
id
node-id
avatar-url
gravatar-id
url
html-url

Stdout may be piped into as well.

$ cat some_kebab_cased_names
login
id
node-id
avatar-url
gravatar-id
url
html-url
$ cat some_kebab_cased_names | casec kebab-case -c
login
id
nodeId
avatarUrl
gravatarId
url
htmlUrl
$ cat some_kebab_cased_names | casec kebab-case -c | casec camel-case -s
login
id
node_id
avatar_url
gravatar_id
url
html_url

As mentioned above, case sensitive string literals may be provided to assist in parsing or formatting names. When parsing camel-case and pascal-case, the -l, --literals switch can be used to provide a comma separated list of literals.

$ cat some_other_camel_cased_names
iOSApp
iTunesStoreId
$ cat some_other_camel_cased_names | casec camel-case -s
i_os_app
i_tunes_store_id
$ cat some_other_camel_cased_names | casec camel-case -s -l iOS,iTunes
ios_app
itunes_store_id

Using the -r, --format-literals switch, output can be formatted.

$ cat some_other_snake_cased_names | casec camel-case -s -l iOS,iTunes
ios_app
itunes_store_id
$ cat some_other_snake_cased_names | casec camel-case -s -l iOS,iTunes
iOSApp
iTunesstoreId
$ cat some_other_snake_cased_names | casec camel-case -s -l iOS,iTunes,ID
iOSApp
iTunesstoreID

Field names from a JSON object can be converted to another case easily with jq.

$ cat some_json.json
{
  "login": "octocat",
  "id": 1,
  "node_id": "MDQ6VXNlcjE=",
  "avatar_url": "https://github.com/images/error/octocat_happy.gif",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "site_admin": false
}
$ cat some_json.json | jq -r 'keys[]' | casec snake-case -c
avatarUrl
eventsUrl
followersUrl
followingUrl
gistsUrl
gravatarId
htmlUrl
id
login
nodeId
organizationsUrl
receivedEventsUrl
reposUrl
siteAdmin
starredUrl
subscriptionsUrl
type
url

There's other

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

casec-0.2.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

casec-0.2.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casec-0.2.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for casec-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e182f526e2992152b53be61039b3ea6287f11a8fae9e7cdecbcbfdb365576be5
MD5 3bf3f6e782637d811ebebab38c1a197a
BLAKE2b-256 b105583919c2ec4c33c9d03a2000e128bb8140b577e1891323415a080ee6a4c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: casec-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

File hashes

Hashes for casec-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a7c5d8c9ead7b1d0d7b226f470bb49cf30b5e66ce162368bad0b72f40a957984
MD5 524a8e351542ede458b22f4b45de7982
BLAKE2b-256 6c93b7605b8868f9fbce0b5a0488d8e8a76b332a9a13bf4d5556086badd867e5

See more details on using hashes here.

Supported by

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