All files / utils sortHelper.js

100% Statements 11/11
100% Branches 15/15
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                      179x         21347x   8x     21339x   21339x     21339x   21339x 16745x 4594x 4591x   21339x          
 
// compareValues comes from this article
//   https://www.sitepoint.com/sort-an-array-of-objects-in-javascript/
// It is used to sort arrays of objects, like this:
// For
//  const singers = [
//     { name: 'Steven Tyler', band: 'Aerosmith', born: 1948 },
//     { name: 'Karen Carpenter', band: 'The Carpenters', born: 1950 },
//     { name: 'Kurt Cobain', band: 'Nirvana', born: 1967 },
//     { name: 'Stevie Nicks', band: 'Fleetwood Mac', born: 1948 },
//   ];
//
// Use: 
// // array is sorted by band, in ascending order by default
// singers.sort(compareValues('band'));
 
// // array is sorted by band in descending order
// singers.sort(compareValues('band', 'desc'));
 
// // array is sorted by name in ascending order
// singers.sort(compareValues('name'));
 
// // array is sorted by date if birth in descending order
// singers.sort(compareValues('born', 'desc'));
 
// Stryker disable next-line StringLiteral: The "" mutant results in equivalent mutant due to it defaulting to ascending sort if order is not "desc"
export function compareValues(key, order = 'asc') {
    return function innerSort(a, b) {
      // Logical Operator: Requiring only one object to have the property causes comparisons to undefined to be equivalent (> or < to undefined evaluates to false which will return 0)
      // Conditional Expression: Changing to false could have both properties be undefined but < > comparisons to undefeind are equivalent to false, returning 0 anyway
      // BlockStatement: Removing "Return 0" reults in equivalent mutant since one or both elements will be undefined and < > comparisons will evaluate to false
      // Stryker disable next-line LogicalOperator, ConditionalExpression, BlockStatement: Each mutant results in equivalent code (reasons in above code comment)
      if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
        // property doesn't exist on either object
        return 0;
      }
      
      const varA = (typeof a[key] === 'string')
        ? a[key].toUpperCase() : a[key];
      const varB = (typeof b[key] === 'string')
        ? b[key].toUpperCase() : b[key];
  
      let comparison = 0;
      // Stryker disable next-line EqualityOperator: the >= mutant results in an equivalent mutant (if two elems are equal switching them is the same)
      if (varA > varB) {
        comparison = 1;
      } else if (varA < varB) {
        comparison = -1;
      }
      return (
        // Stryker disable next-line ArithmeticOperator: The / mutant results in an equivalent mutant
        (order === 'desc') ? (comparison * -1) : comparison
      );
    };
  }