Skip to content

Commit

Permalink
feat: add util groupArrayItemsByCriteria
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon committed Dec 7, 2024
1 parent c28403e commit c694ca7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 23 additions & 1 deletion __tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { validateGithubUrl, ensureGithubToken, getSeverityFromPriorityGroup } = require('../src/utils/index')
const { validateGithubUrl, ensureGithubToken, groupArrayItemsByCriteria, getSeverityFromPriorityGroup } = require('../src/utils/index')

describe('ensureGithubToken', () => {
let originalGithubToken
Expand Down Expand Up @@ -44,6 +44,28 @@ describe('validateGithubUrl', () => {
})
})

describe('groupArrayItemsByCriteria', () => {
const groupByProject = groupArrayItemsByCriteria('project_id')

it('should group array items by criteria', () => {
const items = [
{ project_id: 1, name: 'item1' },
{ project_id: 1, name: 'item2' },
{ project_id: 2, name: 'item3' }
]
const expected = [
[
{ project_id: 1, name: 'item1' },
{ project_id: 1, name: 'item2' }
],
[
{ project_id: 2, name: 'item3' }
]
]
expect(groupByProject(items)).toEqual(expected)
})
})

describe('getSeverityFromPriorityGroup', () => {
it('should return the correct severity based on the priority group', () => {
expect(getSeverityFromPriorityGroup('P0')).toBe('critical')
Expand Down
11 changes: 11 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ const getSeverityFromPriorityGroup = (priorityGroup) => {
}
}

const groupArrayItemsByCriteria = criteria => items => Object.values(
items.reduce((acc, item) => {
if (!acc[item[criteria]]) {
acc[item[criteria]] = []
}
acc[item[criteria]].push(item)
return acc
}, {})
)

module.exports = {
validateGithubUrl,
ensureGithubToken,
getSeverityFromPriorityGroup,
groupArrayItemsByCriteria,
logger
}

0 comments on commit c694ca7

Please sign in to comment.