Ensure _.sortBy moves symbols to the end.

This commit is contained in:
John-David Dalton
2016-04-21 00:36:50 -07:00
parent 076e4e6db5
commit 3c144b8601
2 changed files with 23 additions and 17 deletions

View File

@@ -4062,27 +4062,27 @@
*/
function compareAscending(value, other) {
if (value !== other) {
var valIsNull = value === null,
valIsUndef = value === undefined,
var valIsDefined = value !== undefined,
valIsNull = value === null,
valIsReflexive = value === value,
valIsSymbol = isSymbol(value);
var othIsNull = other === null,
othIsUndef = other === undefined,
var othIsDefined = other !== undefined,
othIsNull = other === null,
othIsReflexive = other === other,
othIsSymbol = isSymbol(other);
if ((valIsSymbol && !othIsSymbol) ||
(!othIsNull && !othIsSymbol && value > other) ||
(valIsNull && !othIsUndef && othIsReflexive) ||
(valIsUndef && othIsReflexive) ||
if ((!othIsNull && !othIsSymbol && !valIsSymbol && value > other) ||
(valIsSymbol && othIsDefined && othIsReflexive && !othIsNull && !othIsSymbol) ||
(valIsNull && othIsDefined && othIsReflexive) ||
(!valIsDefined && othIsReflexive) ||
!valIsReflexive) {
return 1;
}
if ((othIsSymbol && !valIsSymbol) ||
(!valIsNull && !valIsSymbol && value < other) ||
(othIsNull && !valIsUndef && valIsReflexive) ||
(othIsUndef && valIsReflexive) ||
if ((!valIsNull && !valIsSymbol && !othIsSymbol && value < other) ||
(othIsSymbol && valIsDefined && valIsReflexive && !valIsNull && !valIsSymbol) ||
(othIsNull && valIsDefined && valIsReflexive) ||
(!othIsDefined && valIsReflexive) ||
!othIsReflexive) {
return -1;
}

View File

@@ -20250,14 +20250,20 @@
assert.deepEqual(actual, [3, 1, 2]);
});
QUnit.test('should move `null`, `undefined`, and `NaN` values to the end', function(assert) {
QUnit.test('should move symbol, `null`, `undefined`, and `NaN` values to the end', function(assert) {
assert.expect(2);
var array = [NaN, undefined, null, 4, null, 1, undefined, 3, NaN, 2];
assert.deepEqual(_.sortBy(array), [1, 2, 3, 4, null, null, undefined, undefined, NaN, NaN]);
var symbol1 = Symbol ? Symbol('a') : null,
symbol2 = Symbol ? Symbol('b') : null,
array = [NaN, undefined, null, 4, symbol1, null, 1, symbol2, undefined, 3, NaN, 2],
expected = [1, 2, 3, 4, symbol1, symbol2, null, null, undefined, undefined, NaN, NaN];
array = [NaN, undefined, null, 'd', null, 'a', undefined, 'c', NaN, 'b'];
assert.deepEqual(_.sortBy(array), ['a', 'b', 'c', 'd', null, null, undefined, undefined, NaN, NaN]);
assert.deepEqual(_.sortBy(array), expected);
array = [NaN, undefined, symbol1, null, 'd', null, 'a', symbol2, undefined, 'c', NaN, 'b'];
expected = ['a', 'b', 'c', 'd', symbol1, symbol2, null, null, undefined, undefined, NaN, NaN];
assert.deepEqual(_.sortBy(array), expected);
});
QUnit.test('should treat number values for `collection` as empty', function(assert) {