Skip to content

Age Service ​

AgeService determines which age strings are valid and which ones the user has chosen so far. It is providedIn: 'root', so every component in the app shares one instance. The Age Selector uses it, as do the phenoboard table editors.

typescript
import { inject } from '@angular/core';
import { AgeService } from 'ng-hpo-uikit';

private ageService = inject(AgeService);

Accepted formats ​

FormatExamples
HPO onset labelInfantile onset, Congenital onset
ISO 8601 durationP1Y6M, P35Y, P10D
Gestational ageG20w, G32w3d
Not availablena

Remembered ages ​

The service keeps a list of the ages chosen in this session. The Age Selector shows them as "Choose existing" tags so the curator can reuse them quickly.

MemberDescription
selectedTermsRead-only signal. The remembered ages. Starts as ['na'].
allAvailableTermsComputed signal. All HPO onset labels plus the remembered ages, used for autocomplete.
addSelectedTerm(term) / addSelectedTerms(terms)Remember one or more ages. Duplicates are ignored.
removeSelectedTerm(term) / removeSelectedTerms(terms)Forget ages.
clearSelectedTerms()Reset to ['na']. Call this when the user starts a new cohort.

Every way of choosing an age in the Age Selector, including clicking an autocomplete suggestion, adds the age to this list.

Validating and normalizing ​

MethodDescription
validateAgeInput(input)true if the string is already in one of the accepted formats.
validator()Angular ValidatorFn using the same rule, for reactive forms. Empty values pass, so combine with Validators.required.
normalizeAgeInput(raw)Converts free text to a canonical age and validates it. Returns the string, or null if invalid. Use this for user-typed input.

normalizeAgeInput examples:

InputResult
childhoodChildhood onset
neonateNeonatal onset
p1yP1Y
4y2mP4Y2M
3 wP21D
g35w1dG35w1d
garbagenull

Full-label matching is case-sensitive: Infantile onset is accepted, infantile onset is not. Use the short keywords (infantile is not one of them, see AGE_TERM_MAP) or pick from the suggestions.

Lower-level helpers ​

mapAgeStringToSymbolic, mapEtlAgeString, mapYmdToIso and numericYearToIso do the individual conversion steps. Prefer normalizeAgeInput unless you need one step on its own.