Of course. Here is a complete Angular mapper service with a method that maps RechercheDokumentElement[] to RechercheModel[].
This solution is designed to be robust and handles cases where parts of the source model might be null or undefined.
1. The Mapper Service (recherche-mapper.service.ts)
This service contains the core mapping logic. It's best practice to place mappers in their own injectable services to keep your components clean and your logic reusable.
Service Structure: The logic is encapsulated in an Injectable service (RechercheMapperService). This promotes separation of concerns and makes the mapping logic easily testable and reusable throughout your application.
Main Method (mapToRechercheModel):
This is the public method you will call from your components or other services.
It takes an array of the source model (RechercheDokumentElement[]) and returns an array of the target model (RechercheModel[]).
It includes a guard clause if (!sourceElements ...) to safely handle null, undefined, or empty arrays, returning an empty array in such cases.
It uses the standard JavaScript Array.prototype.map() function to iterate over each element and transform it using a helper method.
Single Element Mapping (mapSingleElement):
This private helper method contains the logic for transforming one RechercheDokumentElement into one RechercheModel.
Data Source Priority: The source data is fragmented between two main properties: data and dokumentData. The code establishes a clear priority: it first attempts to get a value from the data property. If that value is null or undefined, it falls back to the corresponding value in the dokumentData property.
Optional Chaining (?.): This is used extensively (e.g., element.data?.nachname). It prevents runtime errors (TypeError: Cannot read property 'nachname' of undefined) if element.data itself is missing. If any part of the chain is null or undefined, the entire expression evaluates to undefined.
Nullish Coalescing Operator (??): This operator is the key to the fallback logic. The expression a ?? b returns b only if a is null or undefined; otherwise, it returns a. This is perfect for setting default or fallback values. The final ?? null ensures that if no source provides a value, the property is explicitly set to null to match the target model's type.
Type Conversion: For personenummer and kontonummer, the source types can be string (from data) or number (from dokumentData). The mapper ensures the final output is always a string by calling .toString() on the number value.
3. How to Use It in a Component
typescript
import { Component, OnInit } from '@angular/core';
import { RechercheMapperService, RechercheModel, RechercheDokumentElement } from './recherche-mapper.service';
@Component({
selector: 'app-my-component',
template: `<!-- Your template here -->`
})
export class MyComponent implements OnInit {
// This would typically come from an API call
sourceDataFromApi: RechercheDokumentElement[] = [
// ... your array of RechercheDokumentElement objects
];
mappedData: RechercheModel[] = [];
constructor(private rechercheMapper: RechercheMapperService) { }
ngOnInit(): void {
// Call the mapper to transform the data
this.mappedData = this.rechercheMapper.mapToRechercheModel(this.sourceDataFromApi);
console.log('Mapped Data:', this.mappedData);
}
}