Skip to content

Boundaries#

Boundaries define where a string is divided into words. They allow you to control how a string is split during conversions between different naming conventions.

Consider an abstract example where the "_" character is used as the boundary:

Imagine the string: "A_B_C"

Using "_" as a boundary, the string is divided into distinct segments: ["A", "B", "C"]

Specificity of Boundaries#

It can be difficult to determine how to split a string into words.

Let's say the string contains the word "2D", for example "scale2D", and we want to translate it to snake case. How do we decide what boundaries to use to split this string into words? Should it be "scale_2_d", "scale_2d" or just "scale2d"?

By default, the conversion method uses some predefined boundaries, but sometimes the predefined boundaries are not enough to meet a specific use case, so you can explicitly set which ones to use by providing instances of the Boundary class.

boundaries/specificity.py
1
2
3
4
5
6
import textcase

textcase.snake("scale2D")  # scale_2_d

textcase.snake("scale2D", boundaries=[textcase.LOWER_DIGIT])  # scale_2d
textcase.snake("scale2D", boundaries=[])  # scale2d

You can see a complete list of all built-in boundaries in the API Reference.

Creating Custom Boundaries#

This library provides a number of constants for boundaries associated with common cases. But if you need to handle more specific cases, you can easily create custom boundaries and use them as well as built-in ones:

boundaries/custom_boundary.py
1
2
3
4
5
6
from textcase import Boundary, title

title("colors.brown")  # Colorsbrown (1)

DOT = Boundary.from_delimiter(".")  # (2)!
title("colors.brown", boundaries=[DOT])  # Colors Brown (3)
  1. 😢 That is quite not what we want. Since the library does not handle boundary with a dot (".") by default, we need to create it manually using the Boundary class.
  2. 😄 To achieve our goal we need to create a custom boundary using the Boundary.from_delimiter method.
  3. 😄 Now we can explicitly set our custom boundary to the boundaries argument and it will be used when splitting text!

To learn more about building a custom boundary from scratch, take a look at the Boundary class.