pub fn to_tag_name(name: &str) -> Cow<'_, str>Expand description
Converts an arbitrary string into a valid Haystack tag name.
The conversion follows the same rules as the TypeScript toTagName utility:
- If the string is already a valid tag name, it is returned unchanged.
.,-, and/are replaced with_; if at position 0 they becomev; if at the last position they are dropped.- A leading digit or
_is prefixed withv. - All remaining invalid characters are stripped.
- Spaces trigger camelCase conversion: each word after the first has its first letter uppercased (provided it is a lowercase letter).
- A leading run of uppercase letters on the first word is lowercased.
- Returns
"empty"if no valid characters remain.
ยงExamples
use libhaystack::util::to_tag_name;
assert_eq!(to_tag_name("oh what a time to be alive"), "ohWhatATimeToBeAlive");
assert_eq!(to_tag_name("AIR TEMP"), "airTEMP");
assert_eq!(to_tag_name("1test"), "v1test");
assert_eq!(to_tag_name(""), "empty");
assert_eq!(to_tag_name("alreadyValid"), "alreadyValid");