Skip to content

Commit

Permalink
shrunk down all of the 'is' functions into a single generation, added…
Browse files Browse the repository at this point in the history
… isRegExp, added a regexp equality test to isEqual, after grayrest's patch
  • Loading branch information
jashkenas committed Dec 7, 2009
1 parent 4bd535e commit 66dc6c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
1 change: 1 addition & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ $(document).ready(function() {
ok(!_.isEqual(5, NaN), '5 is not equal to NaN');
ok(_.isEqual(NaN, NaN), 'NaN is equal to NaN');
ok(_.isEqual(new Date(100), new Date(100)), 'identical dates are equal');
ok(_.isEqual((/hello/ig), (/hello/ig)), 'identical regexes are equal');
});

test("objects: isEmpty", function() {
Expand Down
2 changes: 1 addition & 1 deletion test/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $(document).ready(function() {
"compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isDate", "isElement", "isEmpty", "isEqual",
"isFunction", "isNaN", "isNull", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
"size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
Expand Down
50 changes: 18 additions & 32 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;

// Maintain a reference to the Object prototype for quick access.
var objPro = Object.prototype;

// Current version.
_.VERSION = '0.4.7';

Expand Down Expand Up @@ -395,7 +392,7 @@
_.keys = function(obj) {
if(_.isArray(obj)) return _.range(0, obj.length);
var keys = [];
for (var key in obj) if (objPro.hasOwnProperty.call(obj, key)) keys.push(key);
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) keys.push(key);
return keys;
};

Expand Down Expand Up @@ -425,16 +422,22 @@
if (atype != btype) return false;
// Basic equality test (watch out for coercions).
if (a == b) return true;
// Check dates' integer values.
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
// One of them implements an isEqual()?
if (a.isEqual) return a.isEqual(b);
// Check dates' integer values.
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
// Both are NaN?
if (_.isNaN(a) && _.isNaN(b)) return true;
// Compare regular expressions.
if (_.isRegExp(a) && _.isRegExp(b))
return a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline;
// If a is not an object by this point, we can't handle it.
if (atype !== 'object') return false;
// Check for different array lengths before comparing contents.
if (!_.isUndefined(a.length) && a.length !== b.length) return false;
if (a.length && (a.length !== b.length)) return false;
// Nothing else worked, deep compare the contents.
var aKeys = _.keys(a), bKeys = _.keys(b);
// Different object sizes?
Expand All @@ -454,31 +457,6 @@
return !!(obj && obj.nodeType == 1);
};

// Is a given value a real Array?
_.isArray = function(obj) {
return objPro.toString.call(obj) == '[object Array]';
};

// Is a given value a Function?
_.isFunction = function(obj) {
return objPro.toString.call(obj) == '[object Function]';
};

// Is a given value a String?
_.isString = function(obj) {
return objPro.toString.call(obj) == '[object String]';
};

// Is a given value a Number?
_.isNumber = function(obj) {
return objPro.toString.call(obj) == '[object Number]';
};

// Is a given value a Date?
_.isDate = function(obj) {
return objPro.toString.call(obj) == '[object Date]';
};

// Is the given value NaN -- this one is interesting. NaN != NaN, and
// isNaN(undefined) == true, so we make sure it's a number first.
_.isNaN = function(obj) {
Expand All @@ -495,6 +473,14 @@
return typeof obj == 'undefined';
};

// Define the isArray, isDate, isFunction, isNumber, isRegExp, and
// isString functions based on their toString identifiers.
_.each(['Array', 'Date', 'Function', 'Number', 'RegExp', 'String'], function(type) {
_['is' + type] = function(obj) {
return Object.prototype.toString.call(obj) == '[object ' + type + ']';
};
});

/* -------------------------- Utility Functions: -------------------------- */

// Run Underscore.js in noConflict mode, returning the '_' variable to its
Expand Down

0 comments on commit 66dc6c2

Please sign in to comment.