You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
'use strict'; |
|
|
|
var typeOf = require('kind-of'); |
|
|
|
/** |
|
* Basic sort algorithm that has similar behavior to `Array.prototype.sort` |
|
* for null and undefined, but also allows sorting by an object property. |
|
* |
|
* @param {Mixed} `a` First value to compare. |
|
* @param {Mixed} `b` Second value to compare. |
|
* @param {String} `prop` Optional property to use when comparing objects. If specified must be a string. |
|
* @return {Number} Returns 1 when `a` should come after `b`, -1 when `a` should come before `b`, and 0 when `a` and `b` are equal. |
|
* @api public |
|
*/ |
|
|
|
module.exports = function defaultCompare(a, b, prop) { |
|
if (prop != null && typeOf(prop) !== 'string') { |
|
throw new TypeError('expected "prop" to be undefined or a string'); |
|
} |
|
|
|
var typeA = typeOf(a); |
|
var typeB = typeOf(b); |
|
|
|
if (prop) { |
|
if (typeA === 'object') { |
|
a = a[prop]; |
|
typeA = typeOf(a); |
|
} |
|
if (typeB === 'object') { |
|
b = b[prop]; |
|
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); |
|
} |
|
};
|
|
|