Additional normalisation
Signed-off-by: Tim Jacomb <timjacomb1+github@gmail.com>
This commit is contained in:
@@ -21,6 +21,14 @@ describe('normalizeEntityName', () => {
|
||||
expect(normalizeEntityName('User Name')).toBe('user_name');
|
||||
});
|
||||
|
||||
it('should normalize complex name to valid entity name', () => {
|
||||
expect(normalizeEntityName('User (Name)')).toBe('user_name');
|
||||
});
|
||||
|
||||
it('should normalize complex name to valid entity name without extra underscore', () => {
|
||||
expect(normalizeEntityName('User :(Name:)')).toBe('user_name');
|
||||
});
|
||||
|
||||
it('should normalize e-mail to valid entity name', () => {
|
||||
expect(normalizeEntityName('user.name@example.com')).toBe(
|
||||
'user.name_example.com',
|
||||
|
||||
@@ -15,8 +15,21 @@
|
||||
*/
|
||||
|
||||
export function normalizeEntityName(name: string): string {
|
||||
return name
|
||||
let cleaned = name
|
||||
.trim()
|
||||
.toLocaleLowerCase()
|
||||
.replace(/[^a-zA-Z0-9_\-\.]/g, '_');
|
||||
|
||||
// invalid to end with _
|
||||
while (cleaned.endsWith('_')) {
|
||||
cleaned = cleaned.substring(0, cleaned.length - 1);
|
||||
}
|
||||
|
||||
// cleans up format for groups like 'my group (Reader)'
|
||||
while (cleaned.includes('__')) {
|
||||
// replaceAll from node.js >= 15
|
||||
cleaned = cleaned.replace('__', '_');
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user