Anycase
💼 A case conversion library for Go, Rust, and Python with Unicode support
Install / Use
/learn @rossmacarthur/AnycaseREADME
anycase
A case conversion library for Go, Rust, and Python.
<img src="https://github.com/rossmacarthur/anycase/assets/17109887/ab3a4c71-6090-49f3-a67f-b1ab395999c6" width="500">Anycase provides a consistent way of converting between different case styles. And has a similar API across languages.
Go
Install using
go get -u github.com/rossmacarthur/anycase/go
Now convert a string using the relevant function.
import "github.com/rossmacarthur/anycase/go"
anycase.ToSnake("XMLHttpRequest") // returns "xml_http_request"
Rust
Add the anycase crate to your Cargo manifest.
cargo add anycase
Now convert a string using the relevant function.
anycase::to_snake("XMLHttpRequest"); // returns "xml_http_request"
Python
Install using
pip install py-anycase
Now convert a string using the relevant function.
import anycase
anycase.to_snake("XMLHttpRequest") # returns "xml_http_request"
How does it work?
Each implementation divides the input string into words and applies a “word function” to each word and calls a “delimiter function” for each word boundary (the space between words).
Word boundaries are defined as follows:
- A set of consecutive non-letter/number/symbol e.g.
foo _baris two wordsfooandbar. - A transition from a lowercase letter to an uppercase letter e.g.
fooBaris two wordsfooandBar. - The second last uppercase letter in a word with multiple uppercase letters
e.g.
FOOBaris two wordsFOOandBar.
License
This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.
