Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / internal / compareMultiple.js
1 var baseCompareAscending = require('./baseCompareAscending');
2
3 /**
4  * Used by `_.sortByOrder` to compare multiple properties of a value to another
5  * and stable sort them.
6  *
7  * If `orders` is unspecified, all valuess are sorted in ascending order. Otherwise,
8  * a value is sorted in ascending order if its corresponding order is "asc", and
9  * descending if "desc".
10  *
11  * @private
12  * @param {Object} object The object to compare.
13  * @param {Object} other The other object to compare.
14  * @param {boolean[]} orders The order to sort by for each property.
15  * @returns {number} Returns the sort order indicator for `object`.
16  */
17 function compareMultiple(object, other, orders) {
18   var index = -1,
19       objCriteria = object.criteria,
20       othCriteria = other.criteria,
21       length = objCriteria.length,
22       ordersLength = orders.length;
23
24   while (++index < length) {
25     var result = baseCompareAscending(objCriteria[index], othCriteria[index]);
26     if (result) {
27       if (index >= ordersLength) {
28         return result;
29       }
30       var order = orders[index];
31       return result * ((order === 'asc' || order === true) ? 1 : -1);
32     }
33   }
34   // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
35   // that causes it, under certain circumstances, to provide the same value for
36   // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247
37   // for more details.
38   //
39   // This also ensures a stable sort in V8 and other engines.
40   // See https://code.google.com/p/v8/issues/detail?id=90 for more details.
41   return object.index - other.index;
42 }
43
44 module.exports = compareMultiple;