Skip to content

Commit

Permalink
add _.isSymbol(), fix jashkenas#2501
Browse files Browse the repository at this point in the history
  • Loading branch information
xcodebuild committed Apr 25, 2016
1 parent ce591c8 commit 48e38ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
15 changes: 15 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@
assert.deepEqual(_.max([0, 2], function(c){ return c * this.x; }, {x: 1}), 2, 'Iterator context');
assert.deepEqual(_.max([[1], [2, 3], [-1, 4], [5]], 0), [5], 'Lookup falsy iterator');
assert.deepEqual(_.max([{0: 1}, {0: 2}, {0: -1}, {a: 1}], 0), {0: 2}, 'Lookup falsy iterator');

if (typeof Symbol !== 'undefined') {
assert.equal(_.min([1, 2, 3, Symbol()]), 1, 'Finds correct min in array starting with num and containing a Symbol');
assert.equal(_.min([Symbol(), 1, 2, 3]), 1, 'Finds correct min in array starting with Symbol');
}
});

QUnit.test('min', function(assert) {
Expand Down Expand Up @@ -630,6 +635,11 @@
assert.deepEqual(_.min([0, 2], function(c){ return c * this.x; }, {x: -1}), 2, 'Iterator context');
assert.deepEqual(_.min([[1], [2, 3], [-1, 4], [5]], 0), [-1, 4], 'Lookup falsy iterator');
assert.deepEqual(_.min([{0: 1}, {0: 2}, {0: -1}, {a: 1}], 0), {0: -1}, 'Lookup falsy iterator');

if (typeof Symbol !== 'undefined') {
assert.equal(_.min([1, 2, 3, Symbol()]), 1, 'Finds correct min in array starting with num and containing a Symbol');
assert.equal(_.min([Symbol(), 1, 2, 3]), 1, 'Finds correct min in array starting with Symbol');
}
});

QUnit.test('sortBy', function(assert) {
Expand Down Expand Up @@ -678,6 +688,11 @@

list = ['q', 'w', 'e', 'r', 't', 'y'];
assert.deepEqual(_.sortBy(list), ['e', 'q', 'r', 't', 'w', 'y'], 'uses _.identity if iterator is not specified');

if (typeof Symbol !== 'undefined') {
var sym = Symbol();
assert.deepEqual(_.sortBy([8, 6, sym, 4, 23]), [6, 8, sym, 4, 23], 'sortBy should work even containing a Symbol');
}
});

QUnit.test('groupBy', function(assert) {
Expand Down
8 changes: 7 additions & 1 deletion test/cross-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var _ = typeof require == 'function' ? require('..') : window._;

QUnit.module('Cross Document');
/* global iObject, iElement, iArguments, iFunction, iArray, iError, iString, iNumber, iBoolean, iDate, iRegExp, iNaN, iNull, iUndefined, ActiveXObject */
/* global iObject, iElement, iArguments, iFunction, iArray, iError, iString, iNumber, iBoolean, iDate, iRegExp, iNaN, iNull, iUndefined, iSymbol, ActiveXObject */

// Setup remote variables for iFrame tests.
var iframe = document.createElement('iframe');
Expand Down Expand Up @@ -110,6 +110,12 @@
assert.ok(_.isError(iError), 'even from another frame');
});

QUnit.test('isSymbol', function(assert) {
if (typeof Symbol !== 'undefined') {
assert.ok(_.isError(iSymbol), 'even from another frame');
}
});

if (typeof ActiveXObject != 'undefined') {
QUnit.test('IE host objects', function(assert) {
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
Expand Down
11 changes: 8 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value != null && value > result) {
if (value != null && !_.isSymbol(value) && value > result) {
result = value;
}
}
Expand All @@ -338,7 +338,7 @@
obj = isArrayLike(obj) ? obj : _.values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value != null && value < result) {
if (value != null && !_.isSymbol(value) && value < result) {
result = value;
}
}
Expand Down Expand Up @@ -395,7 +395,7 @@
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a !== b && !_.isSymbol(a) && !_.isSymbol(b)) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
Expand Down Expand Up @@ -1289,6 +1289,11 @@
return type === 'function' || type === 'object' && !!obj;
};

// Is a given value a Symbol?
_.isSymbol = function(obj) {
return typeof obj === 'symbol';
};

// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError, isMap, isWeakMap, isSet, isWeakSet.
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error', 'Symbol', 'Map', 'WeakMap', 'Set', 'WeakSet'], function(name) {
_['is' + name] = function(obj) {
Expand Down

0 comments on commit 48e38ca

Please sign in to comment.