All files / platform/core/src/utils sortBy.js

90% Statements 18/20
75% Branches 6/8
100% Functions 2/2
90% Lines 18/20

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    236x 236x   236x     1180x 3244x 3244x   3244x   3244x 3244x   3244x         3244x   3244x 584x     3244x 596x     3244x 1180x       1180x      
// Return the array sorting function for its object's properties
export default function sortBy() {
  var fields = [].slice.call(arguments),
    n_fields = fields.length;
 
  return function (A, B) {
    var a, b, field, key, reverse, result, i;
 
    for (i = 0; i < n_fields; i++) {
      result = 0;
      field = fields[i];
 
      key = typeof field === 'string' ? field : field.name;
 
      a = A[key];
      b = B[key];
 
      Iif (typeof field.primer !== 'undefined') {
        a = field.primer(a);
        b = field.primer(b);
      }
 
      reverse = field.reverse ? -1 : 1;
 
      if (a < b) {
        result = reverse * -1;
      }
 
      if (a > b) {
        result = reverse * 1;
      }
 
      if (result !== 0) {
        break;
      }
    }
 
    return result;
  };
}