]> git.r.bdr.sh - rbdr/dotfiles/blob
2ab11f1a396350b5fa571e8c5387155d9cff680a
[rbdr/dotfiles] /
1 'use strict';
2
3 var strCompare = require('../string/#/case-insensitive-compare')
4 , isObject = require('./is-object')
5
6 , resolve, typeMap;
7
8 typeMap = {
9 undefined: 0,
10 object: 1,
11 boolean: 2,
12 string: 3,
13 number: 4
14 };
15
16 resolve = function (a) {
17 if (isObject(a)) {
18 if (typeof a.valueOf !== 'function') return NaN;
19 a = a.valueOf();
20 if (isObject(a)) {
21 if (typeof a.toString !== 'function') return NaN;
22 a = a.toString();
23 if (typeof a !== 'string') return NaN;
24 }
25 }
26 return a;
27 };
28
29 module.exports = function (a, b) {
30 if (a === b) return 0; // Same
31
32 a = resolve(a);
33 b = resolve(b);
34 if (a == b) return typeMap[typeof a] - typeMap[typeof b]; //jslint: ignore
35 if (a == null) return -1;
36 if (b == null) return 1;
37 if ((typeof a === 'string') || (typeof b === 'string')) {
38 return strCompare.call(a, b);
39 }
40 if ((a !== a) && (b !== b)) return 0; //jslint: ignore
41 return Number(a) - Number(b);
42 };