This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -6,7 +6,30 @@
*/
module.exports = function get(obj, path, def) {
const parts = path.split('.');
let parts;
let isPathArray = false;
if (typeof path === 'string') {
if (path.indexOf('.') === -1) {
const _v = getProperty(obj, path);
if (_v == null) {
return def;
}
return _v;
}
parts = path.split('.');
} else {
isPathArray = true;
parts = path;
if (parts.length === 1) {
const _v = getProperty(obj, parts[0]);
if (_v == null) {
return def;
}
return _v;
}
}
let rest = path;
let cur = obj;
for (const part of parts) {
@@ -16,13 +39,15 @@ module.exports = function get(obj, path, def) {
// `lib/cast.js` depends on being able to get dotted paths in updates,
// like `{ $set: { 'a.b': 42 } }`
if (cur[rest] != null) {
if (!isPathArray && cur[rest] != null) {
return cur[rest];
}
cur = getProperty(cur, part);
rest = rest.substr(part.length + 1);
if (!isPathArray) {
rest = rest.substr(part.length + 1);
}
}
return cur == null ? def : cur;