diff --git a/index.html b/index.html index aae21514d..fef1bc7eb 100644 --- a/index.html +++ b/index.html @@ -71,12 +71,12 @@ Underscore provides 43-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, javascript - templating, deep equality testing, and so on. It delegates to the built-in + templating, deep equality testing, and so on. It delegates to built-in functions, if present, so Javascript 1.6 compliant browsers will use the native implementations of forEach, map, filter, - every and some. + every, some and indexOf.
@@ -203,7 +203,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
- all_.(list, [iterator], [context])
+ all_.all(list, [iterator], [context])
Returns true if all of the values in the list pass the iterator
truth test. If an iterator is not provided, the truthy value of
@@ -216,7 +216,7 @@ _.all([true, 1, null, 'yes']);
- any_.(list, iterator, [context])
+ any_.any(list, iterator, [context])
Returns true if any of the values in the list pass the
iterator truth test. Short-circuits and stops traversing the list
@@ -228,60 +228,90 @@ _.any([null, 0, 'yes', false]);
=> true
-
- include_.(list, iterator, [context])
+
+ include_.include(list, value)
-
+ Returns true if the value is present in the list, using
+ == to test equality. Uses indexOf internally, if list
+ is an Array.
+_.include([1, 2, 3], 3); +=> true-
- invoke_.(list, iterator, [context])
+
+ invoke_.invoke(list, methodName)
-
+ Calls the method named by methodName on each value in the list.
+ Any extra arguments passed to invoke will be forwarded on to the
+ method invocation.
+_.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); +=> [[1, 5, 7], [1, 2, 3]]-
- pluck_.(list, iterator, [context])
+
+ pluck_.pluck(list, propertyName)
-
+ An optimized version of what is perhaps the most common use-case for
+ map: returning a list of property values.
+var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
+_.pluck(stooges, 'name');
+=> ["moe", "larry", "curly"]
-
- max_.(list, iterator, [context])
+
+ max_.max(list, [iterator], [context])
-
+ Returns the maximum value in list. If iterator is passed,
+ it will be used on each value to generate the criterion by which the
+ value is ranked.
+var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
+_.max(stooges, function(stooge){ return stooge.age; });
+=> {name : 'curly', age : 60};
-
- min_.(list, iterator, [context])
+
+ min_.min(list, [iterator], [context])
-
+ Returns the minimum value in list. If iterator is passed,
+ it will be used on each value to generate the criterion by which the
+ value is ranked.
+var numbers = [10, 5, 100, 2, 1000]; +_.min(numbers); +=> 2-
- sortBy_.(list, iterator, [context])
+
+ sortBy_.sortBy(list, iterator, [context])
-
+ Returns a sorted list, ranked by the results of running each
+ value through iterator.
+_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
+=> [5, 4, 6, 3, 1, 2]
-
- sortedIndex_.(list, iterator, [context])
+
+ sortedIndex_.sortedIndex(list, value, [iterator])
-
+ Uses a binary search to determine the index at which the value
+ should be inserted into the list in order to maintain the list's
+ sorted order. If an iterator is passed, it will be used to compute
+ the sort ranking of each value.
+_.sortedIndex([10, 20, 30, 40, 50], 35); +=> 3
diff --git a/test/collections.js b/test/collections.js index 1ba3512b7..bed210ac2 100644 --- a/test/collections.js +++ b/test/collections.js @@ -100,7 +100,7 @@ $(document).ready(function() { test('collections: sortedIndex', function() { var numbers = [10, 20, 30, 40, 50], num = 35; - var index = _.sortedIndex(numbers, function(a, b) { return a < b ? -1 : a > b ? 1 : 0; }, num); + var index = _.sortedIndex(numbers, num); equals(index, 3, '35 should be inserted at index 3'); }); diff --git a/underscore.js b/underscore.js index 7c5ee29cb..2f7a58d30 100644 --- a/underscore.js +++ b/underscore.js @@ -171,11 +171,12 @@ window._ = { // Use a comparator function to figure out at what index an object should // be inserted so as to maintain order. Uses binary search. - sortedIndex : function(array, comparator, obj) { + sortedIndex : function(array, obj, iterator) { + iterator = iterator || function(val) { return val; }; var low = 0, high = array.length; while (low < high) { var mid = (low + high) >> 1; - comparator(array[mid], obj) < 0 ? low = mid + 1 : high = mid; + iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid; } return low; },