property.js 791 B

1234567891011121314151617181920212223242526272829303132
  1. import baseProperty from './_baseProperty.js';
  2. import basePropertyDeep from './_basePropertyDeep.js';
  3. import isKey from './_isKey.js';
  4. import toKey from './_toKey.js';
  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. export default property;