ut2
Version:
一个现代 JavaScript 实用工具库。[点击查看在线文档]。
42 lines (38 loc) • 1.42 kB
JavaScript
;
var isNumber = require('../isNumber.js');
var isSymbol = require('../isSymbol.js');
var toString = require('../toString.js');
function createCompare(dir) {
var asc = dir === 1;
function wrapper(value, other) {
var valueIsSymbol = isSymbol(value);
var otherIsSymbol = isSymbol(other);
var isNeedConvertString = !valueIsSymbol && !otherIsSymbol && !(isNumber(value) && isNumber(other));
var _value = isNeedConvertString ? toString(value) : value;
var _other = isNeedConvertString ? toString(other) : other;
if (!otherIsSymbol && (valueIsSymbol || _value > _other)) {
return asc ? 1 : -1;
}
if (!valueIsSymbol && (otherIsSymbol || _value < _other)) {
return asc ? -1 : 1;
}
return 0;
}
return wrapper;
}
function compareMultiple(object, other, orders) {
var objCriteria = object.criteria;
var othCriteria = other.criteria;
var length = objCriteria.length;
var index = -1;
while (++index < length) {
var order = orders[index];
var cmpFn = typeof order === 'function' ? order : order === 'desc' ? createCompare(0) : createCompare(1);
var result = cmpFn(objCriteria[index], othCriteria[index]);
if (result) {
return result;
}
}
return object.index - other.index;
}
exports.compareMultiple = compareMultiple;