1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 'use strict';
- var typeOf = require('kind-of');
- 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);
- }
- };
|