Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

added a few more test cases #964

Merged
merged 1 commit into from

2 participants

@clottes

I'd like to add a few more simple test cases, just for robustness. (they've helped me optimize a few functions to be faster without causing problems)

@jashkenas jashkenas merged commit e0dd1f7 into jashkenas:master

1 check passed

Details default The Travis build passed
@jashkenas
Owner

(they've helped me optimize a few functions to be faster without causing problems)

Feel like sharing?

@clottes

Sure, I'm not sure if you are in favor of simplicity, readability, or speed. Personally all are important to me.
I'll definitely share what I've done, especially if I can contribute! Some of them are small speed improvements, such as the _.isString _.isFunction and _.isNumber.

A few questions:

  1. What do you think of providing Implementations of the ECMAScript 5 native functions if not supported in browser adding to the prototype objects if not already there? This would ensure that all those functions actually exist and remove the need to check for their existence. (though if you feel that shouldn't be in the scope of this library, that makes sense), but to me it's where they belong since the functionality is so entwined, and what's wrong with providing those functions if not there in all browsers, I can't think of a reason.
  2. Should I just do more pull requests for ideas I have?
  3. calling Functions seems like an expensive operation, so in what I've changed in my branch, I've pulled some of the nested function calls out, say where the iterator inside of each simply does another function.call (I've replaced with a for or while loop, etc... I'll show you what I mean shortly. I've tried to pull it out where there are LARGE speed improvements without going nuts and keeping it still very simple, I'll be happy to share, I'll send a few your way in the future. (I'm at work now so don't have the time immediately)

    There are a few things I've seen/fixed which I'll share shortly when I get a chance, such as more problems with some functions handling NodeLists in IE<9.

@jashkenas
Owner
  1. Nope -- that's what es5-shim is good for. Underscore tries to stay away from that.

  2. Yep, if you think it's something worth merging in.

  3. We generally prefer delegating to other core Underscore functions when possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 6, 2013
  1. @clottes
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 1 deletion.
  1. +3 −1 test/collections.js
  2. +8 −0 test/objects.js
View
4 test/collections.js
@@ -438,6 +438,7 @@ $(document).ready(function() {
test('size', function() {
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
equal(_.size([1, 2, 3]), 3, 'can compute the size of an array');
+ equal(_.size($('<div>').add('<span>').add('<span>')), 3, 'can compute the size of jQuery objects');
var func = function() {
return _.size(arguments);
@@ -445,7 +446,8 @@ $(document).ready(function() {
equal(func(1, 2, 3, 4), 4, 'can test the size of the arguments object');
- equal(_.size('hello'), 5, 'can compute the size of a string');
+ equal(_.size('hello'), 5, 'can compute the size of a string literal');
+ equal(_.size(new String('hello')), 5, 'can compute the size of string object');
equal(_.size(null), 0, 'handles nulls');
});
View
8 test/objects.js
@@ -2,6 +2,8 @@ $(document).ready(function() {
module("Objects");
+ var undefined; //ensure undefined is really undefined
+
test("keys", function() {
equal(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
// the test above is not safe because it relies on for-in enumeration order
@@ -431,15 +433,19 @@ $(document).ready(function() {
});
test("isArray", function() {
+ ok(!_.isArray(undefined), 'undefined vars are not arrays');
ok(!_.isArray(arguments), 'the arguments object is not an array');
ok(_.isArray([1, 2, 3]), 'but arrays are');
ok(_.isArray(iArray), 'even from another frame');
});
test("isString", function() {
+ var obj = new String("I am a string object");
ok(!_.isString(document.body), 'the document body is not a string');
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
ok(_.isString(iString), 'even from another frame');
+ ok(_.isString("I am a string literal"), 'string literals are');
+ ok(_.isString(obj), 'so are String objects');
});
test("isNumber", function() {
@@ -468,10 +474,12 @@ $(document).ready(function() {
});
test("isFunction", function() {
+ ok(!_.isFunction(undefined), 'undefined vars are not functions');
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
ok(!_.isFunction('moe'), 'strings are not functions');
ok(_.isFunction(_.isFunction), 'but functions are');
ok(_.isFunction(iFunction), 'even from another frame');
+ ok(_.isFunction(function(){}), 'even anonymous ones');
});
test("isDate", function() {
Something went wrong with that request. Please try again.