propertyOf.js 732 B

12345678910111213141516171819202122232425262728293031
  1. var baseGet = require('./_baseGet');
  2. /**
  3. * The opposite of `_.property`; this method creates a function that returns
  4. * the value at a given path of `object`.
  5. *
  6. * @static
  7. * @memberOf _
  8. * @since 3.0.0
  9. * @category Util
  10. * @param {Object} object The object to query.
  11. * @returns {Function} Returns the new accessor function.
  12. * @example
  13. *
  14. * var array = [0, 1, 2],
  15. * object = { 'a': array, 'b': array, 'c': array };
  16. *
  17. * _.map(['a[2]', 'c[0]'], _.propertyOf(object));
  18. * // => [2, 0]
  19. *
  20. * _.map([['a', '2'], ['c', '0']], _.propertyOf(object));
  21. * // => [2, 0]
  22. */
  23. function propertyOf(object) {
  24. return function(path) {
  25. return object == null ? undefined : baseGet(object, path);
  26. };
  27. }
  28. module.exports = propertyOf;