blob: 5879a8848d78a7a4fb24c8d32cc5895438259309 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/*!
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
8module.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
40function isObject(val) {
41 return val !== null && (typeof val === 'object' || typeof val === 'function');
42}
43
44function toString(val) {
45 if (!val) return '';
46 if (Array.isArray(val)) {
47 return val.join('.');
48 }
49 return val;
50}