Skip to main content

to_tag_name

Function to_tag_name 

Source
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:

  1. If the string is already a valid tag name, it is returned unchanged.
  2. ., -, and / are replaced with _; if at position 0 they become v; if at the last position they are dropped.
  3. A leading digit or _ is prefixed with v.
  4. All remaining invalid characters are stripped.
  5. Spaces trigger camelCase conversion: each word after the first has its first letter uppercased (provided it is a lowercase letter).
  6. A leading run of uppercase letters on the first word is lowercased.
  7. 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");