All files / src/utils compare.js

100% Statements 12/12
100% Branches 16/16
100% Functions 1/1
100% Lines 8/8

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                        52x 50x   48x 11x 11x   48x 17x 2x    
/* Copyright © 2016 Kuali, Inc. - All Rights Reserved
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 *
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 */
 
import { isEmpty } from 'lodash'
 
export function compare (a, b, sortAsc) {
  // Always put empty values at the bottom
  if (isEmpty(a)) return 1
  if (isEmpty(b)) return -1
  // Cast values to numbers if they can be
  if (!isNaN(a) && !isNaN(b)) {
    a = parseFloat(a)
    b = parseFloat(b)
  }
  if (a > b) return sortAsc ? 1 : -1
  if (a < b) return sortAsc ? -1 : 1
  return 0
}