Additional normalisation

Signed-off-by: Tim Jacomb <timjacomb1+github@gmail.com>
This commit is contained in:
Tim Jacomb
2021-11-05 14:24:43 +00:00
committed by Tim Jacomb
parent 779d7a2304
commit 01df5bc436
2 changed files with 22 additions and 1 deletions
@@ -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;
}