vkit 0.1.4

Fast Rust dev CLI: manage git worktrees, Node ports, run scripts, install & sync VS Code / Cursor extensions.
//! Worktree Name:由分支名派生的目录名。

/// 将分支名转为 Worktree Name(kebab-case:非字母数字连续段收成单个 `-`)。
///
/// 例如 `feat/auth/oauth` → `feat-auth-oauth`,`Feat_Auth` → `feat-auth`。
pub fn from_branch(branch: &str) -> String {
    let mut out = String::new();
    let mut pending_sep = false;
    for ch in branch.chars() {
        if ch.is_ascii_alphanumeric() {
            if pending_sep && !out.is_empty() {
                out.push('-');
            }
            pending_sep = false;
            out.extend(ch.to_lowercase());
        } else {
            pending_sep = true;
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn slashes_become_kebab() {
        assert_eq!(from_branch("feat/auth/oauth"), "feat-auth-oauth");
    }

    #[test]
    fn plain_branch_lowercased() {
        assert_eq!(from_branch("Hotfix"), "hotfix");
    }

    #[test]
    fn underscores_and_dots() {
        assert_eq!(from_branch("feat_auth.oauth"), "feat-auth-oauth");
    }

    #[test]
    fn trims_leading_trailing_separators() {
        assert_eq!(from_branch("/feat/login/"), "feat-login");
    }

    #[test]
    fn empty_and_only_separators() {
        assert_eq!(from_branch(""), "");
        assert_eq!(from_branch("///"), "");
    }
}