| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 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 | |
| 8 | module.exports = function(obj, prop, a, b, c) { |
| 9 | if (!isObject(obj) || !prop) { |
| 10 | return obj; |
| 11 | } |
| 12 | |
| 13 | prop = toString(prop); |
| 14 | |
| 15 | // allowing for multiple properties to be passed as |
| 16 | // a string or array, but much faster (3-4x) than doing |
| 17 | // `[].slice.call(arguments)` |
| 18 | if (a) prop += '.' + toString(a); |
| 19 | if (b) prop += '.' + toString(b); |
| 20 | if (c) prop += '.' + toString(c); |
| 21 | |
| 22 | if (prop in obj) { |
| 23 | return obj[prop]; |
| 24 | } |
| 25 | |
| 26 | var segs = prop.split('.'); |
| 27 | var len = segs.length; |
| 28 | var i = -1; |
| 29 | |
| 30 | while (obj && (++i < len)) { |
| 31 | var key = segs[i]; |
| 32 | while (key[key.length - 1] === '\\') { |
| 33 | key = key.slice(0, -1) + '.' + segs[++i]; |
| 34 | } |
| 35 | obj = obj[key]; |
| 36 | } |
| 37 | return obj; |
| 38 | }; |
| 39 | |
| 40 | function isObject(val) { |
| 41 | return val !== null && (typeof val === 'object' || typeof val === 'function'); |
| 42 | } |
| 43 | |
| 44 | function toString(val) { |
| 45 | if (!val) return ''; |
| 46 | if (Array.isArray(val)) { |
| 47 | return val.join('.'); |
| 48 | } |
| 49 | return val; |
| 50 | } |