From 549498649d3197da098a5fc5521915ea8a0991e4 Mon Sep 17 00:00:00 2001 From: Filippos Date: Mon, 2 Sep 2024 20:47:49 +0300 Subject: [PATCH 1/5] Refactor Flags.getFlagIdsWithFilters function to reduce cognitive complexity --- src/flags.js | 93 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/src/flags.js b/src/flags.js index 00bce1d9bd..1a3d2e5d98 100644 --- a/src/flags.js +++ b/src/flags.js @@ -135,44 +135,69 @@ Flags.getCount = async function ({ uid, filters, query }) { }; Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) { - let sets = []; - const orSets = []; - - // Default filter - filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1; - filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20; - - for (const type of Object.keys(filters)) { - if (Flags._filters.hasOwnProperty(type)) { - Flags._filters[type](sets, orSets, filters[type], uid); - } else { - winston.warn(`[flags/list] No flag filter type found: ${type}`); - } - } - sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; // No filter default + let sets = []; + const orSets = []; - let flagIds = []; - if (sets.length === 1) { - flagIds = await db.getSortedSetRevRange(sets[0], 0, -1); - } else if (sets.length > 1) { - flagIds = await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' }); - } + // Apply default filters + applyDefaultFilters(filters); - if (orSets.length) { - let _flagIds = await Promise.all(orSets.map(async orSet => await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }))); + // Process filters + processFilters(filters, uid, sets, orSets); - // Each individual orSet is ANDed together to construct the final list of flagIds - _flagIds = _.intersection(..._flagIds); + // Set default set if no filters are applied + sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; - // Merge with flagIds returned by sets - if (sets.length) { - // If flag ids are already present, return a subset of flags that are in both sets - flagIds = _.intersection(flagIds, _flagIds); - } else { - // Otherwise, return all flags returned via orSets - flagIds = _.union(flagIds, _flagIds); - } - } + // Fetch flag IDs + let flagIds = await fetchFlagIds(sets); + + // Process orSets and combine results + if (orSets.length) { + const _flagIds = await fetchOrSetsFlagIds(orSets); + flagIds = combineFlagIds(sets, flagIds, _flagIds); + } + + return flagIds; +}; + +function applyDefaultFilters(filters) { + filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1; + filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20; +} + +function processFilters(filters, uid, sets, orSets) { + for (const type of Object.keys(filters)) { + if (Flags._filters.hasOwnProperty(type)) { + Flags._filters[type](sets, orSets, filters[type], uid); + } else { + winston.warn(`[flags/list] No flag filter type found: ${type}`); + } + } +} + +async function fetchFlagIds(sets) { + if (sets.length === 1) { + return await db.getSortedSetRevRange(sets[0], 0, -1); + } else if (sets.length > 1) { + return await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' }); + } + return []; +} + +async function fetchOrSetsFlagIds(orSets) { + return await Promise.all(orSets.map(async orSet => + await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) + )); +} + +function combineFlagIds(sets, flagIds, _flagIds) { + _flagIds = _.intersection(..._flagIds); + + if (sets.length) { + return _.intersection(flagIds, _flagIds); + } else { + return _.union(flagIds, _flagIds); + } +} const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { filters, From 4bc9c697f4661b465befc219d729d2459b385109 Mon Sep 17 00:00:00 2001 From: Filippos Date: Mon, 2 Sep 2024 21:05:49 +0300 Subject: [PATCH 2/5] Added result constant within the async function --- src/flags.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/flags.js b/src/flags.js index 1a3d2e5d98..7c9e14cc59 100644 --- a/src/flags.js +++ b/src/flags.js @@ -197,16 +197,16 @@ function combineFlagIds(sets, flagIds, _flagIds) { } else { return _.union(flagIds, _flagIds); } -} - const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { - filters, - uid, - query, - flagIds, - }); - return result.flagIds; -}; + const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { + filters, + uid, + query, + flagIds, + }); + return result.flagIds; + }; +} Flags.list = async function (data) { const filters = data.filters || {}; From 41e087cfca441f25ea31e3d405b15ed7bf59a515 Mon Sep 17 00:00:00 2001 From: Filippos Date: Tue, 3 Sep 2024 02:07:07 +0300 Subject: [PATCH 3/5] Refactor getFlagIdsWithFilters to reduce Cognitive Complexity from 16 to 15 --- src/flags.js | 110 +++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 57 deletions(-) diff --git a/src/flags.js b/src/flags.js index 7c9e14cc59..330c0f485a 100644 --- a/src/flags.js +++ b/src/flags.js @@ -135,79 +135,75 @@ Flags.getCount = async function ({ uid, filters, query }) { }; Flags.getFlagIdsWithFilters = async function ({ filters, uid, query }) { - let sets = []; - const orSets = []; + initializeFilters(filters); - // Apply default filters - applyDefaultFilters(filters); + const { sets, orSets } = buildSets(filters, uid); - // Process filters - processFilters(filters, uid, sets, orSets); + let flagIds = await getFlagIdsFromSets(sets); - // Set default set if no filters are applied - sets = (sets.length || orSets.length) ? sets : ['flags:datetime']; - - // Fetch flag IDs - let flagIds = await fetchFlagIds(sets); - - // Process orSets and combine results - if (orSets.length) { - const _flagIds = await fetchOrSetsFlagIds(orSets); - flagIds = combineFlagIds(sets, flagIds, _flagIds); - } + if (orSets.length) { + const _flagIds = await getFlagIdsFromOrSets(orSets); + flagIds = mergeFlagIds(flagIds, _flagIds, sets.length > 0); + } - return flagIds; + const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { + filters, + uid, + query, + flagIds, + }); + return result.flagIds; }; -function applyDefaultFilters(filters) { - filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1; - filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20; +function initializeFilters(filters) { + filters.page = filters.hasOwnProperty('page') ? Math.abs(parseInt(filters.page, 10) || 1) : 1; + filters.perPage = filters.hasOwnProperty('perPage') ? Math.abs(parseInt(filters.perPage, 10) || 20) : 20; } -function processFilters(filters, uid, sets, orSets) { - for (const type of Object.keys(filters)) { - if (Flags._filters.hasOwnProperty(type)) { - Flags._filters[type](sets, orSets, filters[type], uid); - } else { - winston.warn(`[flags/list] No flag filter type found: ${type}`); - } - } -} +function buildSets(filters, uid) { + let sets = []; + const orSets = []; -async function fetchFlagIds(sets) { - if (sets.length === 1) { - return await db.getSortedSetRevRange(sets[0], 0, -1); - } else if (sets.length > 1) { - return await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' }); - } - return []; -} + for (const type of Object.keys(filters)) { + if (Flags._filters.hasOwnProperty(type)) { + Flags._filters[type](sets, orSets, filters[type], uid); + } else { + winston.warn(`[flags/list] No flag filter type found: ${type}`); + } + } -async function fetchOrSetsFlagIds(orSets) { - return await Promise.all(orSets.map(async orSet => - await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) - )); + if (!(sets.length || orSets.length)) { + sets = ['flags:datetime']; // No filter default + } + + return { sets, orSets }; } -function combineFlagIds(sets, flagIds, _flagIds) { - _flagIds = _.intersection(..._flagIds); +async function getFlagIdsFromSets(sets) { + if (sets.length === 1) { + return await db.getSortedSetRevRange(sets[0], 0, -1); + } else if (sets.length > 1) { + return await db.getSortedSetRevIntersect({ sets: sets, start: 0, stop: -1, aggregate: 'MAX' }); + } + return []; +} - if (sets.length) { - return _.intersection(flagIds, _flagIds); - } else { - return _.union(flagIds, _flagIds); - } +async function getFlagIdsFromOrSets(orSets) { + const _flagIds = await Promise.all(orSets.map(async orSet => + await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) + )); + return _.intersection(..._flagIds); +} - const result = await plugins.hooks.fire('filter:flags.getFlagIdsWithFilters', { - filters, - uid, - query, - flagIds, - }); - return result.flagIds; - }; +function mergeFlagIds(flagIds, _flagIds, hasSets) { + if (hasSets) { + return _.intersection(flagIds, _flagIds); + } else { + return _.union(flagIds, _flagIds); + } } + Flags.list = async function (data) { const filters = data.filters || {}; let flagIds = await Flags.getFlagIdsWithFilters({ From 825271884e8bde1afbe7eb48966a64dffebab8b3 Mon Sep 17 00:00:00 2001 From: Filippos Date: Tue, 3 Sep 2024 02:11:56 +0300 Subject: [PATCH 4/5] Fix linting errors in flags.js: adjust implicit arrow linebreak, remove unnecessary else, and correct function paren newline --- src/flags.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/flags.js b/src/flags.js index 330c0f485a..4680f40d36 100644 --- a/src/flags.js +++ b/src/flags.js @@ -189,8 +189,8 @@ async function getFlagIdsFromSets(sets) { } async function getFlagIdsFromOrSets(orSets) { - const _flagIds = await Promise.all(orSets.map(async orSet => - await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) + const _flagIds = await Promise.all(orSets.map(async (orSet) => + db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) )); return _.intersection(..._flagIds); } @@ -198,12 +198,13 @@ async function getFlagIdsFromOrSets(orSets) { function mergeFlagIds(flagIds, _flagIds, hasSets) { if (hasSets) { return _.intersection(flagIds, _flagIds); - } else { - return _.union(flagIds, _flagIds); } + // Since there's no need for 'else' after 'return', simply return the union directly + return _.union(flagIds, _flagIds); } + Flags.list = async function (data) { const filters = data.filters || {}; let flagIds = await Flags.getFlagIdsWithFilters({ From 6f6b003f4341955724e1c6262c1b21fb2d6c4693 Mon Sep 17 00:00:00 2001 From: Filippos Date: Tue, 3 Sep 2024 02:15:46 +0300 Subject: [PATCH 5/5] Fix linting errors: remove unnecessary parentheses, adjust line breaks, and correct function paren newline in flags.js --- src/flags.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/flags.js b/src/flags.js index 4680f40d36..2583e03bb5 100644 --- a/src/flags.js +++ b/src/flags.js @@ -189,9 +189,7 @@ async function getFlagIdsFromSets(sets) { } async function getFlagIdsFromOrSets(orSets) { - const _flagIds = await Promise.all(orSets.map(async (orSet) => - db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }) - )); + const _flagIds = await Promise.all(orSets.map(async orSet => db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }))); return _.intersection(..._flagIds); } @@ -199,7 +197,6 @@ function mergeFlagIds(flagIds, _flagIds, hasSets) { if (hasSets) { return _.intersection(flagIds, _flagIds); } - // Since there's no need for 'else' after 'return', simply return the union directly return _.union(flagIds, _flagIds); }