-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathprocessUsername.ts
More file actions
93 lines (83 loc) · 3.14 KB
/
Copy pathprocessUsername.ts
File metadata and controls
93 lines (83 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2025 The ThunderID Authors
// SPDX-License-Identifier: Apache-2.0
/**
* Regular expression to match userstore prefixes in usernames.
* Matches patterns like "DEFAULT/", "ASGARDEO_USER/", "PRIMARY/", etc.
* The pattern matches any uppercase letters, numbers, and underscores followed by a forward slash.
*/
const USERSTORE_PREFIX_REGEX = /^[A-Z_][A-Z0-9_]*\//;
/**
* Removes userstore prefixes from a username if they exist.
* This is commonly used to clean usernames returned from profile endpoints
* that include userstore prefixes like "DEFAULT/", "ASGARDEO_USER/", "PRIMARY/", etc.
*
* @param username - The username string to process
* @returns The username without the userstore prefix, or the original username if no prefix exists
*
* @example
* ```typescript
* const cleanUsername = removeUserstorePrefix("DEFAULT/john.doe");
* console.log(cleanUsername); // "john.doe"
*
* const thunderidUser = removeUserstorePrefix("ASGARDEO_USER/jane.doe");
* console.log(thunderidUser); // "jane.doe"
*
* const primaryUser = removeUserstorePrefix("PRIMARY/admin");
* console.log(primaryUser); // "admin"
*
* const alreadyClean = removeUserstorePrefix("user.name");
* console.log(alreadyClean); // "user.name"
*
* const emptyInput = removeUserstorePrefix("");
* console.log(emptyInput); // ""
* ```
*/
export const removeUserstorePrefix = (username?: string): string => {
if (!username) {
return '';
}
return username.replace(USERSTORE_PREFIX_REGEX, '');
};
/**
* Processes a user object to remove userstore prefixes from username fields.
* This is a helper function for processing user objects returned from profile endpoints.
* Handles various username field variations: username, userName, and user_name.
*
* @param user - The user object to process
* @returns The user object with processed username fields
*
* @example
* ```typescript
* const user = { username: "DEFAULT/john.doe", email: "john@example.com" };
* const processedUser = processUserUsername(user);
* console.log(processedUser.username); // "john.doe"
*
* const camelCaseUser = { userName: "ASGARDEO_USER/jane.doe", email: "jane@example.com" };
* const processedCamelCaseUser = processUserUsername(camelCaseUser);
* console.log(processedCamelCaseUser.userName); // "jane.doe"
*
* const snakeCaseUser = { user_name: "PRIMARY/admin", email: "admin@example.com" };
* const processedSnakeCaseUser = processUserUsername(snakeCaseUser);
* console.log(processedSnakeCaseUser.user_name); // "admin"
* ```
*/
const processUsername = <T extends {userName?: string; user_name?: string; username?: string}>(user: T): T => {
if (!user) {
return user;
}
const processedUser: T = {...user};
// Process username field
if (processedUser.username) {
processedUser.username = removeUserstorePrefix(processedUser.username);
}
// Process userName field
if (processedUser.userName) {
processedUser.userName = removeUserstorePrefix(processedUser.userName);
}
// Process user_name field
if (processedUser.user_name) {
processedUser.user_name = removeUserstorePrefix(processedUser.user_name);
}
return processedUser;
};
export default processUsername;