Skip to content

gdzig/casez

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

casez

Flexible case conversion library for Zig with comptime and runtime support.

This library was created to address the more esoteric case conversion needs of gdzig.

Usage

const casez = @import("casez");

// Comptime conversion
const snake = casez.comptimeConvert(.snake, "helloWorld"); // "hello_world"
const pascal = casez.comptimeConvert(.pascal, "hello_world"); // "HelloWorld"

// Runtime conversion (buffer-based)
var buf: [64]u8 = undefined;
const result = try casez.bufConvert(.camel, &buf, input);

// Runtime conversion (allocating)
const allocated = try casez.allocConvert(.kebab, allocator, input);
defer allocator.free(allocated);

Configs

  • .snake - hello_world
  • .camel - helloWorld
  • .pascal - HelloWorld
  • .constant - HELLO_WORLD
  • .kebab - hello-world
  • .title - Hello World

Custom Configs

For advanced use cases, you can create custom configs to control acronym casing and handle ambiguous word boundaries.

Acronym Dictionary

Preserve acronym casing in output:

const config: casez.Config = .{
    .first = .title,
    .rest = .title,
    .acronym = .upper,  // acronyms get uppercased
    .delimiter = "",
    .dictionary = .{
        .acronyms = .initComptime(&.{
            .{ "http", {} },
            .{ "url", {} },
        }),
    },
};

comptimeConvert(config, "http_request");    // "HTTPRequest"
comptimeConvert(config, "parse_url");       // "ParseURL"
comptimeConvert(config, "http_url_parser"); // "HTTPURLParser"

Acronyms are also used to split ambiguous input like "XRVRS" into separate words when both "xr" and "vrs" are defined.

Splits Dictionary

Explicitly define how ambiguous words should be split:

const config: casez.Config = .{
    .first = .lower,
    .rest = .lower,
    .acronym = .lower,
    .delimiter = "_",
    .dictionary = .{
        .splits = .initComptime(&.{
            .{ "vector2d", &.{ "vector", "2d" } },
        }),
    },
};

comptimeConvert(config, "Vector2D"); // "vector_2d"

Prefix and Suffix

Add a custom prefix or suffix string:

const prefixed: casez.Config = comptime .with(.snake, .{ .prefix = "_" });
comptimeConvert(prefixed, "helloWorld"); // "_hello_world"

const suffixed: casez.Config = comptime .with(.snake, .{ .suffix = "_" });
comptimeConvert(suffixed, "helloWorld"); // "hello_world_"

// Useful for virtual methods: _someMethod from some_method
const prefixed_camel: casez.Config = comptime .with(.camel, .{ .prefix = "_" });
comptimeConvert(prefixed_camel, "some_method"); // "_someMethod"

Helpers

Use the with method to extend built-in configs:

const custom: casez.Config = comptime .with(.pascal, .{
    .dictionary = .{
        .acronyms = &.{ "http" },
    },
});

const prefixed: casez.Config = comptime .with(custom, .{ .prefix = "_" });
const suffixed: casez.Config = comptime .with(custom, .{ .suffix = "_" });

Detection

Check if a string matches a case format:

casez.is(.snake, "hello_world");  // true
casez.is(.camel, "helloWorld");   // true
casez.is(.pascal, "HelloWorld");  // true
casez.is(.constant, "HELLO_WORLD"); // true
casez.is(.kebab, "hello-world");  // true

// Works with custom configs including prefix/suffix
casez.is(.withPrefix(.snake, "_"), "_private");  // true
casez.is(.camel, "_notCamel");  // false

License

MIT

About

Flexible case conversion library for Zig

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages