UNPKG

783 BJavaScriptView Raw
1'use strict';
2
3/*!
4 * Simplified lodash.get to work around the annoying null quirk. See:
5 * https://github.com/lodash/lodash/issues/3659
6 */
7
8module.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 // `lib/cast.js` depends on being able to get dotted paths in updates,
18 // like `{ $set: { 'a.b': 42 } }`
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
31function 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