/**
  @module utils/nf/array-helpers
*/

/**
  returns whatever you pass into it.
  @method identity
  @param x {Any}
  @private
  @return {Any} x
*/
function identity(x) {
  return x;
}

/**
  Performs a binary search on the array and finds the nearest index to the value passed.
  @method nearestIndexTo
  @param arr {Array} the *sorted* array to search.
  @param val {Number} the value to find the nearest index to.
  @param mappingFn {Function} an optional function for pulling values out of the 
  array items.
*/
export function nearestIndexTo(arr, val, mappingFn) {
  mappingFn = mappingFn || identity;
  var startIndex  = 0;
  var stopIndex = arr.length - 1;
  var middle = (stopIndex + startIndex) / 2;
  var a = Math.floor(middle);
  var b = Math.floor(middle + 1);

  var getItem = function(i){
    return mappingFn(arr[i]);
  };

  var av = getItem(a);
  var bv = getItem(b);

  while(!(av <= val && val <= bv) && startIndex < stopIndex){

    if (val < av){
        stopIndex = middle - 1;
    } else if (val > av){
        startIndex = middle + 1;
    }

    middle = (stopIndex + startIndex) / 2;
    a = Math.floor(middle);
    b = Math.floor(middle + 1);
    av = getItem(a);
    bv = getItem(b);
  }

  return (Math.abs(val - av) < Math.abs(val - bv)) ? a : b;
}
@@ -1273,8 +1273,1287 @@
 a : b;%0A%7D
+%0A%0Avar NATURAL_SORT_REGEXP = /%5B+-%5D?%5Cd+%5C.?%5Cd*%7C%5CS+/g;%0A%0A/**%0A  Parses an array into an array of arrays of tokens to %0A  use in the natural sort.%0A  %0A  @method naturalParse%0A  @param arr %7BArray%7D the array to parse%0A*/%0Avar naturalParse = function(arr) %7B%0A  return arr.map(function(item) %7B%0A    NATURAL_SORT_REGEXP.lastIndex = 0;%0A    var matches;%0A    var pieces = %5B%5D;%0A    while(matches = NATURAL_SORT_REGEXP.exec(item)) %7B%0A      pieces.push(matches%5B0%5D);%0A    %7D%0A    return %7B tokens: pieces, value: item %7D;%0A  %7D);%0A%7D;%0A%0A/**%0A  Returns a new array, sorted %22naturally%22. Meaning taking into account both%0A  alphabetical and numeric sorting within strings.%0A%0A  @method natualSort%0A  @param arr %7BArray%7D the array to sort%0A  @return %7BArray%7D sorted array%0A*/%0Aexport function naturalSort(arr) %7B%0A  var parsed = naturalParse(arr);%0A  parsed.sort(function(a, b) %7B%0A     var len = Math.max(a.tokens.length, b.tokens.length);%0A     var i = 0, bx, ax, na, nb;%0A     while((ax = a.tokens%5Bi%5D) && (bx = b.tokens%5Bi++%5D)) %7B%0A       na = +ax;%0A       nb = +bx;%0A       if(nb === nb && na === na) %7B%0A         if(na !== nb) %7B%0A           return na %3E nb ? 1 : -1;%0A         %7D%0A       %7D%0A       if(ax !== bx) %7B%0A         return ax %3E bx ? 1 : -1;%0A       %7D%0A     %7D%0A    return 0;%0A  %7D);%0A%0A  return parsed.map(function(p) %7B%0A    return p.value;%0A  %7D);%0A%7D

add natural sort function