Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _.comparator and align sortedIndex to sortBy #1939

Merged
merged 1 commit into from
Dec 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"no-floating-decimal": 2,
"no-inner-declarations": 2,
"no-lonely-if": 1,
"no-nested-ternary": 2,
"no-nested-ternary": 0,
"no-new-object": 0,
"no-new-func": 0,
"no-underscore-dangle": 0,
Expand Down
11 changes: 11 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@
equal(_.sortedIndex(array, 2147483648), 2147483648, 'should work with large indexes');
});

test('sortedIndex conforms to sortBy (#1768)', function() {
var sorted = _([{a: NaN}, {a: NaN}, {a: void 0}, {a: 0}, {a: 1}, {a: 2}, {a: 3}, {a: '4'}, {a: 'foo'}])
.sortBy('a');
_.each([0, 1, 2, 3, '4', 'foo'], function(val) {
equal(_.sortedIndex(sorted, {a: val}, 'a'), _.findIndex(sorted, {a: val}), 'For val: ' + val);
});

sorted = _.sortBy([undefined, 1, undefined, 2]);
equal(_.sortedIndex(sorted, undefined, _.identity), 2);
});

test('uniq', function() {
var list = [1, 2, 1, 3, 1, 4];
deepEqual(_.uniq(list), [1, 2, 3, 4], 'can find the unique values of an unsorted array');
Expand Down
34 changes: 34 additions & 0 deletions test/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@
ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds');//within 5ms
});

test('comparator', function() {
_.each([
[2, 1],
[2, '1'],
['2', 1],
[1.0001, 1],
[1, null],
[null, -1],
[void 0, null],
[void 0, 5],
[NaN, 'something'],
['b', 'a'],
[-5, '-6.05']
], function(vals) {
var a = vals[0], b = vals[1];
equal(_.comparator(a, b), 1, '\'' + a + '\' >= \'' + b + '\'');
equal(_.comparator(b, a), -1, '\'' + b + '\' <= \'' + a + '\'');
});

_.each([
[0, 0],
[null, 0],
[void 0, void 0],
[NaN, NaN],
[1, {valueOf: _.constant(1)}],
[1, '1'],
['zap', 'zap']
], function(vals) {
var a = vals[0], b = vals[1];
equal(_.comparator(a, b), 0, '\'' + a + '\' == \'' + b + '\'');
equal(_.comparator(b, a), 0, '\'' + b + '\' == \'' + a + '\'');
});
});

test('uniqueId', function() {
var ids = [], i = 0;
while (i++ < 100) ids.push(_.uniqueId());
Expand Down
22 changes: 14 additions & 8 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,7 @@
criteria: iteratee(value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
return _.comparator(left.criteria, right.criteria) || left.index - right.index;
}), 'value');
};

Expand Down Expand Up @@ -675,7 +669,7 @@
var low = 0, high = array.length;
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
if (_.comparator(iteratee(array[mid]), value) < 0) low = mid + 1; else high = mid;
}
return low;
};
Expand Down Expand Up @@ -1299,6 +1293,18 @@
};
};

// Default internal comparator for determining whether a is greater (1),
// equal (0) or less than (-1) some object b
_.comparator = function(a, b) {
if (a === b) return 0;
var isAComparable = a >= a, isBComparable = b >= b;
if (isAComparable || isBComparable) {
if (isAComparable && !isBComparable) return -1;
if (isBComparable && !isAComparable) return 1;
}
return a > b ? 1 : (b > a) ? -1 : 0;
};

// Run a function **n** times.
_.times = function(n, iteratee, context) {
var accum = Array(Math.max(0, n));
Expand Down