Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Increase identity generator supported age range to 80 years (#2558)

Leendert de Borst380710c1fbd5
diff --git a/core/rust/src/identity_generator/mod.rs b/core/rust/src/identity_generator/mod.rs
index 5b4144a64..eb8fe4d13 100644
--- a/core/rust/src/identity_generator/mod.rs
+++ b/core/rust/src/identity_generator/mod.rs
@@ -6,7 +6,7 @@
//!
//! An identity consists of a first name, last name, gender, birth date, email prefix and nickname (username). Names are drawn
//! from embedded per-language dictionaries; for languages with decade-based dictionaries (de, it, ro) the first name is
-//! matched to names that were popular around the generated birth year.
+//! matched to names that were popular in the decade nearest to the generated birth year.
pub(crate) mod dictionaries;
mod username_email;
@@ -29,7 +29,7 @@ const DEFAULT_MIN_AGE: i32 = 21;
const DEFAULT_MAX_AGE: i32 = 65;
/// The age range option values offered by the settings UIs; "random" means the default 21-65 range.
-const AGE_RANGES: &[&str] = &["random", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60", "61-65"];
+const AGE_RANGES: &[&str] = &["random", "21-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60", "61-65", "66-70", "71-75", "76-80"];
/// A generated identity.
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -176,23 +176,16 @@ fn random_birth_date<R: Rng + ?Sized>(rng: &mut R, options: Option<BirthdateOpti
start.checked_add_days(Days::new(unbiased_index(rng, days) as u64)).unwrap_or(start)
}
-/// The first name pool for a birth year: matching decade lists, else all decade names, else the flat per-gender list.
+/// The first name pool for a birth year: the decade list nearest to it, else the flat per-gender list.
fn select_firstnames_for_birth_year(dictionary: &'static dictionaries::LanguageDictionary, birth_year: i32, is_male: bool) -> Vec<&'static str> {
let decades = if is_male { dictionary.firstnames_male_by_decade } else { dictionary.firstnames_female_by_decade };
+ let distance = |d: &dictionaries::DecadeNames| (d.start_year - birth_year).max(birth_year - d.end_year).max(0);
- if !decades.is_empty() {
- let matching: Vec<&'static str> = decades.iter().filter(|d| (d.start_year..=d.end_year).contains(&birth_year)).flat_map(|d| d.names()).collect();
- if !matching.is_empty() {
- return matching;
- }
-
- let all: Vec<&'static str> = decades.iter().flat_map(|d| d.names()).collect();
- if !all.is_empty() {
- return all;
- }
+ match decades.iter().min_by_key(|d| distance(d)) {
+ Some(nearest) => nearest.names().collect(),
+ None if is_male => dictionary.firstnames_male(),
+ None => dictionary.firstnames_female(),
}
-
- if is_male { dictionary.firstnames_male() } else { dictionary.firstnames_female() }
}
/// The leading `yyyy` year of a date string or ISO 8601 timestamp, or `None` when absent.
diff --git a/core/rust/src/identity_generator/tests/mod.rs b/core/rust/src/identity_generator/tests/mod.rs
index b97996902..cc022fb03 100644
--- a/core/rust/src/identity_generator/tests/mod.rs
+++ b/core/rust/src/identity_generator/tests/mod.rs
@@ -164,16 +164,13 @@ fn german_decade_names_follow_the_birth_year() {
}
#[test]
-fn decade_language_falls_back_to_all_decades_for_uncovered_years() {
- // Italian decades stop at 2019, so a 2024 birth year has no direct match.
- let mut req = request("it");
- req.birthdate_options = Some(BirthdateOptions {
- target_year: 2024,
- year_deviation: 0,
- });
-
- let identity = generate_from_request(&req);
- assert!(!identity.first_name.is_empty());
+fn decade_language_uses_the_nearest_decade_for_uncovered_years() {
+ // Decade lists cover 1950-2019 for Italian, so 1940 and 2024 have no direct match.
+ let dictionary = dictionaries::resolve("it");
+ for (birth_year, expected) in [(1940, &dictionary.firstnames_male_by_decade[0]), (2024, dictionary.firstnames_male_by_decade.last().unwrap())] {
+ let names = select_firstnames_for_birth_year(dictionary, birth_year, true);
+ assert_eq!(names, expected.names().collect::<Vec<_>>(), "birth year {birth_year}");
+ }
}
#[test]
@@ -202,6 +199,8 @@ fn age_range_conversion_matches_reference_values() {
("21-25", 2002, 2),
("26-30", 1997, 2),
("61-65", 1962, 2),
+ ("71-75", 1952, 2),
+ ("76-80", 1947, 2),
("25-25", 2000, 0),
("20-24", 2003, 2),
];
@@ -227,8 +226,8 @@ fn available_age_ranges_starts_with_random() {
let ranges = available_age_ranges();
assert_eq!(ranges[0], "random");
assert!(ranges.contains(&"21-25".to_string()));
- assert!(ranges.contains(&"61-65".to_string()));
- assert_eq!(ranges.len(), 10);
+ assert!(ranges.contains(&"76-80".to_string()));
+ assert_eq!(ranges.len(), 13);
}
#[test]