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.
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);.snake-hello_world.camel-helloWorld.pascal-HelloWorld.constant-HELLO_WORLD.kebab-hello-world.title-Hello World
For advanced use cases, you can create custom configs to control acronym casing and handle ambiguous word boundaries.
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.
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"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"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 = "_" });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"); // falseMIT