dashboard/utils/object.js

40 lines
673 B
JavaScript

import { cloneDeep } from 'lodash';
const quotedKey = /['"]/;
export function get(obj, path) {
let parts;
if ( path.match(quotedKey) ) {
// Path with quoted section
parts = path.match(/[^."']+|"([^"]*)"|'([^']*)'/g).map(x => x.replace(/['"]/g, ''));
} else {
// Regular path
parts = path.split('.');
}
for (let i = 0; i < parts.length; i++) {
if (!obj) {
return;
}
obj = obj[parts[i]];
}
return obj;
}
export function getter(path) {
return function(obj) {
return get(obj, path);
};
}
export function clone(obj) {
return cloneDeep(obj);
}
export function isEmpty(obj) {
return !Object.keys(obj).length;
}