use .reduce from @HaiDang666

This commit is contained in:
Michael C 2021-07-07 00:51:08 -04:00
parent 3371c6a099
commit 50e7f4af8e
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4

View file

@ -14,16 +14,14 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
try {
// Get existing lock categories markers
const row = await db.prepare('all', 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
// map to array in JS becaues of SQL incompatibilities
// map categories to array in JS becaues of SQL incompatibilities
const categories = row.map(item => item.category);
if (categories.length === 0 || !categories[0]) return res.sendStatus(404);
// Get existing lock categories markers
const reasons = row.map(item => item.reason);
let longReason = "";
// set longReason if current length is longer
reasons.forEach((e) => { if (e.length > longReason.length) longReason = e; });
// Get longest lock reason
const reason = row.map(item => item.reason)
.reduce((a,b) => (a.length > b.length) ? a : b);
return res.send({
reason: longReason,
reason,
categories
});
} catch (err) {