1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | module.exports = function get(obj, path, def) {
|
9 | const parts = path.split('.');
|
10 | let rest = path;
|
11 | let cur = obj;
|
12 | for (const part of parts) {
|
13 | if (cur == null) {
|
14 | return def;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | if (cur[rest] != null) {
|
20 | return cur[rest];
|
21 | }
|
22 |
|
23 | cur = getProperty(cur, part);
|
24 |
|
25 | rest = rest.substr(part.length + 1);
|
26 | }
|
27 |
|
28 | return cur == null ? def : cur;
|
29 | };
|
30 |
|
31 | function getProperty(obj, prop) {
|
32 | if (obj == null) {
|
33 | return obj;
|
34 | }
|
35 | if (obj instanceof Map) {
|
36 | return obj.get(prop);
|
37 | }
|
38 | return obj[prop];
|
39 | } |
\ | No newline at end of file |