index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. * get-value <https://github.com/jonschlinkert/get-value>
  3. *
  4. * Copyright (c) 2014-2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. module.exports = function(obj, prop, a, b, c) {
  8. if (!isObject(obj) || !prop) {
  9. return obj;
  10. }
  11. prop = toString(prop);
  12. // allowing for multiple properties to be passed as
  13. // a string or array, but much faster (3-4x) than doing
  14. // `[].slice.call(arguments)`
  15. if (a) prop += '.' + toString(a);
  16. if (b) prop += '.' + toString(b);
  17. if (c) prop += '.' + toString(c);
  18. if (prop in obj) {
  19. return obj[prop];
  20. }
  21. var segs = prop.split('.');
  22. var len = segs.length;
  23. var i = -1;
  24. while (obj && (++i < len)) {
  25. var key = segs[i];
  26. while (key[key.length - 1] === '\\') {
  27. key = key.slice(0, -1) + '.' + segs[++i];
  28. }
  29. obj = obj[key];
  30. }
  31. return obj;
  32. };
  33. function isObject(val) {
  34. return val !== null && (typeof val === 'object' || typeof val === 'function');
  35. }
  36. function toString(val) {
  37. if (!val) return '';
  38. if (Array.isArray(val)) {
  39. return val.join('.');
  40. }
  41. return val;
  42. }