Provided is the typescript function, where we need to reduce the cyclometric complexity. I am thinking off to provide the inverted if but that doesn't help much.
updateSort(s: Sort) {
if (s.active !== this.personSort.active || s.direction !== this.personSort.direction) {
let sortFunc: (a: PersonInfo, b: PersonInfo) => number;
switch (s.active) {
default:
case 'personName':
sortFunc = (a, b) => (a.name).localeCompare(b.name);
break;
case 'personAddress':
sortFunc = (a, b) => (a.pageTypeDescription.toLocaleString()).localeCompare(b.pageTypeDescription.toLocaleString());
break;
case 'personVisitors':
sortFunc = (a, b) => a.viewsCount - b.viewsCount;
break;
case 'personContacts':
sortFunc = (a, b) => a.clicksCount - b.clicksCount;
break;
}
if (s.direction === 'desc') {
this.sliceList.sort((a, b) => sortFunc(b, a));
} else {
this.sliceList.sort(sortFunc);
}
}
}
As noted in the comments, you can invert the boolean logic to return early, which is always nicer to read and you can easily extract the switch statement into it's own function. E.g.
and an example of getting rid of the
switchentirely like noted by @caTS could beThere's more you could do, similar to 3limin4t0r you could also refactor into