Skip to content

Commit

Permalink
add implementation for null and undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb committed Sep 10, 2017
1 parent 0770421 commit cb459d9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,23 @@ function compare(prop, a, b) {

/**
* Default compare function used as a fallback
* for sorting.
* for sorting. Built-in array sorting pushes
* null and undefined values to the end of the array.
*/

function defaultCompare(a, b) {
return a < b ? -1 : (a > b ? 1 : 0);
var typeA = typeOf(a);
var typeB = typeOf(b);

if (typeA === 'null') {
return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1);
} else if (typeA === 'undefined') {
return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1);
} else if (typeB === 'null' || typeB === 'undefined') {
return -1;
} else {
return a < b ? -1 : (a > b ? 1 : 0);
}
}

/**
Expand Down

0 comments on commit cb459d9

Please sign in to comment.