Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit 950edba

Browse files
committed
feat: add kebab_case function with unit tests for string conversion to kebab-case
1 parent 2446c69 commit 950edba

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ pub fn camel_case(s: &str, normalize: bool) -> String {
123123
lower_first(&pascal_case(s, normalize))
124124
}
125125

126+
/// Convert a string to kebab-case.
127+
/// Splits by case, lowercases each part, joins with "-". Matches scule kebabCase.
128+
pub fn kebab_case(s: &str) -> String {
129+
split_by_case(s, None)
130+
.into_iter()
131+
.map(|p| p.to_lowercase())
132+
.collect::<Vec<_>>()
133+
.join("-")
134+
}
135+
126136
pub fn hello(name: &str) -> String {
127137
log!(LogLevel::Info, "lib.rs");
128138
format!("Hello, {}!", name)
@@ -157,6 +167,18 @@ mod tests {
157167
assert_eq!(camel_case("FOO_BAR", true), "fooBar");
158168
}
159169

170+
#[test]
171+
fn test_kebab_case() {
172+
assert_eq!(kebab_case(""), "");
173+
assert_eq!(kebab_case("foo"), "foo");
174+
assert_eq!(kebab_case("foo/Bar"), "foo-bar");
175+
assert_eq!(kebab_case("foo-bAr"), "foo-b-ar");
176+
assert_eq!(kebab_case("foo--bar"), "foo--bar");
177+
assert_eq!(kebab_case("FooBAR"), "foo-bar");
178+
assert_eq!(kebab_case("ALink"), "a-link");
179+
assert_eq!(kebab_case("FOO_BAR"), "foo-bar");
180+
}
181+
160182
#[test]
161183
fn test_pascal_case() {
162184
assert_eq!(pascal_case("", true), "");

0 commit comments

Comments
 (0)