in-any-case 1.0.5

A library for converting strings into specific cases


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Main Repo   ·    Mirror   ·    Dub Package Registry   ·    Documentation

In Any Case

In any case is a D language library for converting strings into specific capitalizations.

There are six built-in cases:

writeln("Hello World".toCase(Case.pascal));         // HelloWorld
writeln("Hello World".toCase(Case.camel));          // helloWorld
writeln("Hello World".toCase(Case.snake));          // hello_world
writeln("Hello World".toCase(Case.screamingSnake)); // HELLO_WORLD
writeln("Hello World".toCase(Case.kebab));          // hello-world
writeln("Hello World".toCase(Case.sentence));       // Hello world

Custom cases can be defined using the Case struct. It takes a capitalizer function to apply correct casing to words, and an optional separator to place between words.

Case spongebobCase = Case(
    (words) {
        string[] result;

        foreach (string word; words) {
            string newWord;
            foreach (idx, c; word) {
                import std.uni : toUpper, toLower;

                if (idx % 2 == 0)
                    newWord ~= c.toLower;
                else
                    newWord ~= c.toUpper;
            }
            result ~= newWord;
        }

        return result;
    },
    " " // Separate words using spaces
);

expect("hello world".toCase(spongebobCase)).toEqual("hElLo wOrLd");

wstring (UTF-16) and dstring (UTF-32) inputs are accepted, but the library's internals will convert them to UTF-8 and then back. A bit wasteful, but it beats having the Case struct be a template.

As of v1.0, the library is designed for the ASCII alphabet. Letters beyond those found in English may not be converted as expected.

Authors:
  • Andrej Petrović
Dependencies:
none
Versions:
1.0.5 2022-Jun-01
1.0.4 2022-May-29
1.0.3 2022-May-29
1.0.2 2022-May-28
1.0.1 2022-May-28
Show all 7 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 3 downloads this month

  • 3 downloads total

Score:
0.0
Short URL:
in-any-case.dub.pm