Morning improvements
This commit is contained in:
parent
3e9fba5303
commit
53fff3efaa
41 changed files with 2438 additions and 637 deletions
|
|
@ -236,19 +236,24 @@ impl TravelTimeStore {
|
|||
}
|
||||
}
|
||||
|
||||
/// Slugify a place name to match travel time file naming convention.
|
||||
/// "Abbey Hey" → "abbey-hey", "A'Bhuaile Ghlas" → "a-bhuaile-ghlas"
|
||||
/// Slugify a place name to match Java `originFilename()` convention.
|
||||
/// Strips non-alphanumeric chars (except spaces/hyphens) first, then collapses
|
||||
/// whitespace to hyphens. This matches Java's `replaceAll("[^a-z0-9 -]", "")`
|
||||
/// followed by `replaceAll("\\s+", "-")`.
|
||||
/// "King's Cross" → "kings-cross", "Abbey Hey" → "abbey-hey"
|
||||
pub fn slugify(name: &str) -> String {
|
||||
let mut result = String::with_capacity(name.len());
|
||||
let mut last_was_hyphen = true; // Start true to skip leading hyphens
|
||||
for ch in name.chars() {
|
||||
let lower = ch.to_ascii_lowercase();
|
||||
if ch.is_ascii_alphanumeric() {
|
||||
result.push(ch.to_ascii_lowercase());
|
||||
result.push(lower);
|
||||
last_was_hyphen = false;
|
||||
} else if !last_was_hyphen {
|
||||
} else if (ch == ' ' || ch == '-') && !last_was_hyphen {
|
||||
result.push('-');
|
||||
last_was_hyphen = true;
|
||||
}
|
||||
// Other non-alphanumeric chars (apostrophes, ampersands, etc.) are stripped
|
||||
}
|
||||
if result.ends_with('-') {
|
||||
result.pop();
|
||||
|
|
@ -266,6 +271,32 @@ mod tests {
|
|||
assert_eq!(slugify("London"), "london");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slugify_apostrophes_stripped() {
|
||||
assert_eq!(slugify("King's Cross"), "kings-cross");
|
||||
assert_eq!(
|
||||
slugify("Earl's Court tube station"),
|
||||
"earls-court-tube-station"
|
||||
);
|
||||
assert_eq!(slugify("St. Paul's tube station"), "st-pauls-tube-station");
|
||||
assert_eq!(
|
||||
slugify("Regent's Park tube station"),
|
||||
"regents-park-tube-station"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slugify_special_chars_stripped() {
|
||||
assert_eq!(
|
||||
slugify("Cobham & Stoke d'Abernon railway station"),
|
||||
"cobham-stoke-dabernon-railway-station"
|
||||
);
|
||||
assert_eq!(
|
||||
slugify("Ravenglass (R&ER) railway station"),
|
||||
"ravenglass-rer-railway-station"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strip_numeric_prefix_basic() {
|
||||
assert_eq!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue