• Jump To … +
    index.js underscore.array.builders.js underscore.array.selectors.js underscore.collections.walk.js underscore.function.arity.js underscore.function.combinators.js underscore.function.iterators.js underscore.function.predicates.js underscore.object.builders.js underscore.object.selectors.js underscore.util.existential.js underscore.util.strings.js underscore.util.trampolines.js
  • underscore.array.selectors.js

  • ¶

    Underscore-contrib (underscore.array.selectors.js 0.0.1) (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors Underscore-contrib may be freely distributed under the MIT license.

    (function(root) {
  • ¶

    Baseline setup

  • ¶

    Establish the root object, window in the browser, or global on the server.

      var _ = root._ || require('underscore');
  • ¶

    Helpers

  • ¶

    Create quick reference variables for speed access to core prototypes.

      var slice   = Array.prototype.slice,
          concat  = Array.prototype.concat;
    
      var existy = function(x) { return x != null; };
      var truthy = function(x) { return (x !== false) && existy(x); };
      var isSeq = function(x) { return (_.isArray(x)) || (_.isArguments(x)); };
  • ¶

    Mixing in the array selectors

  • ¶
      _.mixin({
  • ¶

    Returns the second element of an array. Passing n will return all but the first of the head N values in the array. The guard check allows it to work with _.map.

        second: function(array, n, guard) {
          if (array == null) return void 0;
          return (n != null) && !guard ? slice.call(array, 1, n) : array[1];
        },
  • ¶

    A function to get at an index into an array

        nth: function(array, index) {
          if ((index < 0) || (index > array.length - 1)) throw Error("Attempting to index outside the bounds of the array.");
    
          return array[index];
        },
  • ¶

    Takes all items in an array while a given predicate returns truthy.

        takeWhile: function(array, pred) {
          if (!isSeq(array)) throw new TypeError;
    
          var sz = _.size(array);
    
          for (var index = 0; index < sz; index++) {
            if(!truthy(pred(array[index]))) {
              break;
            }
          }
    
          return _.take(array, index);
        },
  • ¶

    Drops all items from an array while a given predicate returns truthy.

        dropWhile: function(array, pred) {
          if (!isSeq(array)) throw new TypeError;
    
          var sz = _.size(array);
    
          for (var index = 0; index < sz; index++) {
            if(!truthy(pred(array[index])))
              break;
          }
    
          return _.drop(array, index);
        },
  • ¶

    Returns an array with two internal arrays built from taking an original array and spliting it at the index where a given function goes falsey.

        splitWith: function(array, pred) {
          return [_.takeWhile(pred, array), _.dropWhile(pred, array)];
        },
  • ¶

    Takes an array and partitions it as the given predicate changes truth sense.

        partitionBy: function(array, fun){
          if (_.isEmpty(array) || !existy(array)) return [];
    
          var fst    = _.first(array);
          var fstVal = fun(fst);
          var run    = concat.call([fst], _.takeWhile(_.rest(array), function(e) {
            return _.isEqual(fstVal, fun(e));
          }));
    
          return concat.call([run], _.partitionBy(_.drop(array, _.size(run)), fun));
        },
  • ¶

    Returns the 'best' value in an array based on the result of a given function.

        best: function(array, fun) {
          return _.reduce(array, function(x, y) {
            return fun(x, y) ? x : y;
          });
        },
  • ¶

    Returns an array of existy results of a function over an source array.

        keep: function(array, fun) {
          if (!isSeq(array)) throw new TypeError("expected an array as the first argument");
    
          return _.filter(_.map(array, function(e) {
            return fun(e);
          }), existy);
        }
      });
    
    })(this);