You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
793 B

4 years ago
  1. var baseProperty = require('./_baseProperty'),
  2. basePropertyDeep = require('./_basePropertyDeep'),
  3. isKey = require('./_isKey'),
  4. toKey = require('./_toKey');
  5. /**
  6. * Creates a function that returns the value at `path` of a given object.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 2.4.0
  11. * @category Util
  12. * @param {Array|string} path The path of the property to get.
  13. * @returns {Function} Returns the new accessor function.
  14. * @example
  15. *
  16. * var objects = [
  17. * { 'a': { 'b': 2 } },
  18. * { 'a': { 'b': 1 } }
  19. * ];
  20. *
  21. * _.map(objects, _.property('a.b'));
  22. * // => [2, 1]
  23. *
  24. * _.map(_.sortBy(objects, _.property(['a', 'b'])), 'a.b');
  25. * // => [1, 2]
  26. */
  27. function property(path) {
  28. return isKey(path) ? baseProperty(toKey(path)) : basePropertyDeep(path);
  29. }
  30. module.exports = property;