From 06c773b06e53ec9a089f17c18852098cb1e25c66 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Thu, 19 Apr 2012 10:48:10 -0600 Subject: [PATCH] Tighten specification for _.sortedIndex sortedIndex should specify that it returns the lowest index at which an item may be inserted to maintain sorted order, rather than any index which satisfies this property, in order to provide easier use maintaining a sorted list of unique elements. For example, with the previous specification, the following behavior is possible: _.sortedIndex([10, 20, 30, 40], 30) => 3 To accommodate this behavior users would have to check both array[si] and array[si-1] (when si > 0) before inserting to ensure uniqueness, or to examine the underscore.js implementation and rely on the implementation not changing. Note: This specification change does not require any changes to the code. Signed-off-by: Kevin Locke --- docs/underscore.html | 4 ++-- underscore.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/underscore.html b/docs/underscore.html index 3c213270e..183abb96b 100644 --- a/docs/underscore.html +++ b/docs/underscore.html @@ -198,8 +198,8 @@ (result[key] || (result[key] = [])).push(value); }); return result; - };

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, obj, iterator) {
+  };

Use a comparator function to figure out the smallest index at which +an object should be inserted so as to maintain order. Uses binary search.

  _.sortedIndex = function(array, obj, iterator) {
     iterator || (iterator = _.identity);
     var low = 0, high = array.length;
     while (low < high) {
diff --git a/underscore.js b/underscore.js
index 26c69e183..86e555331 100644
--- a/underscore.js
+++ b/underscore.js
@@ -285,8 +285,8 @@
     return result;
   };
 
-  // Use a comparator function to figure out at what index an object should
-  // be inserted so as to maintain order. Uses binary search.
+  // Use a comparator function to figure out the smallest index at which
+  // an object should be inserted so as to maintain order. Uses binary search.
   _.sortedIndex = function(array, obj, iterator) {
     iterator || (iterator = _.identity);
     var low = 0, high = array.length;