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) { function compareAscending(value, other) {
if (value !== other) { if (value !== other) {
var valIsNull = value === null, var valIsDefined = value !== undefined,
valIsUndef = value === undefined, valIsNull = value === null,
valIsReflexive = value === value, valIsReflexive = value === value,
valIsSymbol = isSymbol(value); valIsSymbol = isSymbol(value);
var othIsNull = other === null, var othIsDefined = other !== undefined,
othIsUndef = other === undefined, othIsNull = other === null,
othIsReflexive = other === other, othIsReflexive = other === other,
othIsSymbol = isSymbol(other); othIsSymbol = isSymbol(other);
if ((valIsSymbol && !othIsSymbol) || if ((!othIsNull && !othIsSymbol && !valIsSymbol && value > other) ||
(!othIsNull && !othIsSymbol && value > other) || (valIsSymbol && othIsDefined && othIsReflexive && !othIsNull && !othIsSymbol) ||
(valIsNull && !othIsUndef && othIsReflexive) || (valIsNull && othIsDefined && othIsReflexive) ||
(valIsUndef && othIsReflexive) || (!valIsDefined && othIsReflexive) ||
!valIsReflexive) { !valIsReflexive) {
return 1; return 1;
} }
if ((othIsSymbol && !valIsSymbol) || if ((!valIsNull && !valIsSymbol && !othIsSymbol && value < other) ||
(!valIsNull && !valIsSymbol && value < other) || (othIsSymbol && valIsDefined && valIsReflexive && !valIsNull && !valIsSymbol) ||
(othIsNull && !valIsUndef && valIsReflexive) || (othIsNull && valIsDefined && valIsReflexive) ||
(othIsUndef && valIsReflexive) || (!othIsDefined && valIsReflexive) ||
!othIsReflexive) { !othIsReflexive) {
return -1; return -1;
} }

View File

@@ -20250,14 +20250,20 @@
assert.deepEqual(actual, [3, 1, 2]); 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); assert.expect(2);
var array = [NaN, undefined, null, 4, null, 1, undefined, 3, NaN, 2]; var symbol1 = Symbol ? Symbol('a') : null,
assert.deepEqual(_.sortBy(array), [1, 2, 3, 4, null, null, undefined, undefined, NaN, NaN]); 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), expected);
assert.deepEqual(_.sortBy(array), ['a', 'b', 'c', 'd', null, null, undefined, undefined, NaN, NaN]);
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) { QUnit.test('should treat number values for `collection` as empty', function(assert) {