propertyOf.js 764 B

123456789101112131415161718192021222324252627282930
  1. var baseGet = require('../internal/baseGet'),
  2. toPath = require('../internal/toPath');
  3. /**
  4. * The opposite of `_.property`; this method creates a function that returns
  5. * the property value at a given path on `object`.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @category Utility
  10. * @param {Object} object The object to query.
  11. * @returns {Function} Returns the new 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 baseGet(object, toPath(path), (path + ''));
  26. };
  27. }
  28. module.exports = propertyOf;