• 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.object.selectors.js

  • ¶

    Underscore-contrib (underscore.object.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 concat  = Array.prototype.concat;
  • ¶

    Mixing in the object selectors

  • ¶
      _.mixin({
  • ¶

    Returns a function that will attempt to look up a named field in any object that it's given.

        accessor: function(field) {
          return function(obj) {
            return (obj && obj[field]);
          };
        },
  • ¶

    Given an object, returns a function that will attempt to look up a field that it's given.

        dictionary: function (obj) {
          return function(field) {
            return (obj && field && obj[field]);
          };
        },
  • ¶

    Like _.pick except that it takes an array of keys to pick.

        selectKeys: function (obj, ks) {
          return _.pick.apply(null, concat.call([obj], ks));
        },
  • ¶

    Returns the key/value pair for a given property in an object, undefined if not found.

        kv: function(obj, key) {
          if (_.has(obj, key)) {
            return [key, obj[key]];
          }
    
          return void 0;
        },
  • ¶

    Gets the value at any depth in a nested object based on the path described by the keys given.

        getPath: function getPath (obj, ks) {
  • ¶

    If we have reached an undefined property then stop executing and return undefined

          if (obj === undefined) return void 0;
  • ¶

    If the path array has no more elements, we've reached the intended property and return its value

          if (ks.length === 0) return obj;
  • ¶

    If we still have elements in the path array and the current value is null, stop executing and return undefined

          if (obj === null) return void 0;
    
          return getPath(obj[[].shift.call(ks)], ks);
        },
  • ¶

    Returns a boolean indicating whether there is a property at the path described by the keys given

        hasPath: function hasPath (obj, ks) {
          var numKeys = ks.length;
    
          if (obj == null && numKeys > 0) return false;
    
          if (!(ks[0] in obj)) return false;
    
          if (numKeys === 1) return true;
    
          return hasPath(obj[[].shift.call(ks)], ks);
        }
      });
    
    })(this);