From 8759f8dbf24ad730f2aab74fa2e19030ba41ee1f Mon Sep 17 00:00:00 2001 From: Ajay Date: Sun, 16 Jan 2022 15:17:36 -0500 Subject: [PATCH] Use object for merging locks Co-authored-by: Nishant Arora --- src/routes/getLockCategoriesByHash.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/routes/getLockCategoriesByHash.ts b/src/routes/getLockCategoriesByHash.ts index 7fc5de3..598f833 100644 --- a/src/routes/getLockCategoriesByHash.ts +++ b/src/routes/getLockCategoriesByHash.ts @@ -19,27 +19,27 @@ interface DBLock { actionType: ActionType, } -const mergeLocks = (source: DBLock[], actionTypes: ActionType[]) => { - const dest: LockResultByHash[] = []; +const mergeLocks = (source: DBLock[], actionTypes: ActionType[]): LockResultByHash[] => { + const dest: { [videoID: VideoID]: LockResultByHash } = {}; for (const obj of source) { if (!actionTypes.includes(obj.actionType)) continue; // videoID already exists - const destMatch = dest.find(s => s.videoID === obj.videoID); - if (destMatch) { + if (obj.videoID in dest) { // override longer reason + const destMatch = dest[obj.videoID]; if (obj.reason?.length > destMatch.reason?.length) destMatch.reason = obj.reason; // push to categories destMatch.categories.push(obj.category); } else { - dest.push({ + dest[obj.videoID] = { videoID: obj.videoID, hash: obj.hash, reason: obj.reason, categories: [obj.category] - }); + }; } } - return dest; + return Object.values(dest); }; export async function getLockCategoriesByHash(req: Request, res: Response): Promise {