Skip to content
Snippets Groups Projects
Commit b1dcaedd authored by Marlène SIMONDANT's avatar Marlène SIMONDANT
Browse files

Merge branch '545-annuaire-mon-compte-casse-prenom' into 'dev'

fix(annuaire, compte) - case for composed first name

See merge request !863
parents 284c9b4a dccf542b
Branches
Tags
2 merge requests!907V3.2.0,!863fix(annuaire, compte) - case for composed first name
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'userName' })
export class UserNamePipe implements PipeTransform {
private readonly WORD_SEPARATOR_REGEX = /([- ])/;
/**
* Transforms a user name by capitalizing each word while preserving separators.
* @param userName - The input user name string.
* @returns The transformed user name.
* @example
* {{ 'dominique' | userName }}
* // returns 'Dominique'
* @example
* {{ 'jEaN-piERre' | userName }}
* // returns 'Jean-Pierre'
* {{ 'dominique' | userName }} // returns 'Dominique'
* {{ 'jEaN-piERre' | userName }} // returns 'Jean-Pierre'
* {{ 'abdou elanyou' | userName }} // returns 'Abdou Elanyou'
*/
transform(userName: string): string {
const words = userName.split('-');
return words.map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase()).join('-');
// If the userName is not a string, return it as is
if (!userName || typeof userName !== 'string') {
return userName;
}
// Split the userName by the word separator regex,
// capitalize (if index is even, it's a word, we capitalize. If index is odd, it's a separator, we leave it as is)
// join and return
return userName
.split(this.WORD_SEPARATOR_REGEX)
.map((part, index) => (index % 2 === 0 ? this.capitalizeWord(part) : part))
.join('');
}
private capitalizeWord(word: string): string {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment