Skip to content

Commit

Permalink
fix(KUI-1309): sort modules by digit, then letter
Browse files Browse the repository at this point in the history
  • Loading branch information
belanglos committed May 2, 2024
1 parent a84375c commit 9641a3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const { createPlannedModularString } = require('../createPlannedModularString')

const alphabeticallySortedModules = [
'module_p1_G2',
'module_p1_B2',
'module_p1_J2',
'module_p1_B1',
'module_p1_D1',
'module_p1_E2',
'module_p1_G1',
'module_p1_I1',
]

describe('createPlannedModularString', () => {
test('returns empty string if empty modules', () => {
expect(createPlannedModularString([])).toStrictEqual('')
Expand Down Expand Up @@ -31,4 +42,8 @@ describe('createPlannedModularString', () => {
])('combines several schemas for different periods', (expected, modules) => {
expect(createPlannedModularString(modules)).toStrictEqual(expected)
})

test('sorts modules first by digit, then letter', () => {
expect(createPlannedModularString(alphabeticallySortedModules)).toStrictEqual('P1: B1, D1, G1, I1, B2, E2, G2, J2.')
})
})
19 changes: 18 additions & 1 deletion server/apiCalls/timeTable/utils/createPlannedModularString.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ const addKeyIfNonexistent = (obj, key) => {
}
}

const sortModulesByDigitAndLetter = (a, b) => {
if (a === b) return 0

const [letterA, digitA] = a
const [letterB, digitB] = b

if (digitA === digitB) {
if (letterA < letterB) return -1
if (letterA > letterB) return 1
return 0
}

return Number(digitA) - Number(digitB)
}

const createPeriodSchemaMapping = modules => {
const mapping = {}
modules.forEach(moduleString => {
Expand All @@ -36,7 +51,9 @@ const createPeriodSchemaStrings = modules => {
const mapping = createPeriodSchemaMapping(modules)

return Object.keys(mapping).map(period => {
const schemaString = mapping[period].join(', ')
const sortedModules = mapping[period].sort(sortModulesByDigitAndLetter)

const schemaString = sortedModules.join(', ')

return `${period}: ${schemaString}.`
})
Expand Down

0 comments on commit 9641a3e

Please sign in to comment.