mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-23 00:33:50 +02:00
refactor findUserByDisplayName
This commit is contained in:
@@ -17,6 +17,19 @@ describe('findUserByDisplayName', () => {
|
||||
return map;
|
||||
}
|
||||
|
||||
function createDisplayNameIndex(entries) {
|
||||
const index = new Map();
|
||||
for (const entry of entries) {
|
||||
let ids = index.get(entry.displayName);
|
||||
if (!ids) {
|
||||
ids = new Set();
|
||||
index.set(entry.displayName, ids);
|
||||
}
|
||||
ids.add(entry.id);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
test('returns the user matching displayName', () => {
|
||||
const users = createCachedUsers([
|
||||
{ id: 'usr_1', displayName: 'Alice' },
|
||||
@@ -56,4 +69,31 @@ describe('findUserByDisplayName', () => {
|
||||
expect(findUserByDisplayName(users, 'alice')).toBeUndefined();
|
||||
expect(findUserByDisplayName(users, 'ALICE')).toBeUndefined();
|
||||
});
|
||||
|
||||
test('uses displayName index when provided', () => {
|
||||
const entries = [
|
||||
{ id: 'usr_1', displayName: 'Alice' },
|
||||
{ id: 'usr_2', displayName: 'Bob' }
|
||||
];
|
||||
const users = createCachedUsers(entries);
|
||||
const index = createDisplayNameIndex(entries);
|
||||
|
||||
const result = findUserByDisplayName(users, 'Bob', index);
|
||||
|
||||
expect(result).toEqual({ id: 'usr_2', displayName: 'Bob' });
|
||||
});
|
||||
|
||||
test('indexed lookup falls back to next duplicate when first user is missing', () => {
|
||||
const entries = [
|
||||
{ id: 'usr_1', displayName: 'Alice' },
|
||||
{ id: 'usr_2', displayName: 'Alice' }
|
||||
];
|
||||
const users = createCachedUsers(entries);
|
||||
users.delete('usr_1');
|
||||
const index = createDisplayNameIndex(entries);
|
||||
|
||||
const result = findUserByDisplayName(users, 'Alice', index);
|
||||
|
||||
expect(result).toEqual({ id: 'usr_2', displayName: 'Alice' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -275,9 +275,23 @@ function userOnlineFor(ref) {
|
||||
* Find a user object from cachedUsers by displayName.
|
||||
* @param {Map} cachedUsers
|
||||
* @param {string} displayName
|
||||
* @param {Map<string, Set<string>>} [cachedUserIdsByDisplayName]
|
||||
* @returns {object|undefined}
|
||||
*/
|
||||
function findUserByDisplayName(cachedUsers, displayName) {
|
||||
function findUserByDisplayName(
|
||||
cachedUsers,
|
||||
displayName,
|
||||
cachedUserIdsByDisplayName
|
||||
) {
|
||||
const indexedUserIds = cachedUserIdsByDisplayName?.get(displayName);
|
||||
if (indexedUserIds) {
|
||||
for (const userId of indexedUserIds) {
|
||||
const ref = cachedUsers.get(userId);
|
||||
if (ref?.displayName === displayName) {
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const ref of cachedUsers.values()) {
|
||||
if (ref.displayName === displayName) {
|
||||
return ref;
|
||||
|
||||
Reference in New Issue
Block a user