getPathValue.js 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*!
  2. * Chai - getPathValue utility
  3. * Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
  4. * @see https://github.com/logicalparadox/filtr
  5. * MIT Licensed
  6. */
  7. var getPathInfo = require('./getPathInfo');
  8. /**
  9. * ### .getPathValue(path, object)
  10. *
  11. * This allows the retrieval of values in an
  12. * object given a string path.
  13. *
  14. * var obj = {
  15. * prop1: {
  16. * arr: ['a', 'b', 'c']
  17. * , str: 'Hello'
  18. * }
  19. * , prop2: {
  20. * arr: [ { nested: 'Universe' } ]
  21. * , str: 'Hello again!'
  22. * }
  23. * }
  24. *
  25. * The following would be the results.
  26. *
  27. * getPathValue('prop1.str', obj); // Hello
  28. * getPathValue('prop1.att[2]', obj); // b
  29. * getPathValue('prop2.arr[0].nested', obj); // Universe
  30. *
  31. * @param {String} path
  32. * @param {Object} object
  33. * @returns {Object} value or `undefined`
  34. * @name getPathValue
  35. * @api public
  36. */
  37. module.exports = function(path, obj) {
  38. var info = getPathInfo(path, obj);
  39. return info.value;
  40. };