2 const isObj = require('is-obj');
4 const disallowedKeys = [
10 const isValidPath = pathSegments => !pathSegments.some(segment => disallowedKeys.includes(segment));
12 function getPathSegments(path) {
13 const pathArray = path.split('.');
16 for (let i = 0; i < pathArray.length; i++) {
19 while (p[p.length - 1] === '\\' && pathArray[i + 1] !== undefined) {
20 p = p.slice(0, -1) + '.';
27 if (!isValidPath(parts)) {
35 get(object, path, value) {
36 if (!isObj(object) || typeof path !== 'string') {
37 return value === undefined ? object : value;
40 const pathArray = getPathSegments(path);
41 if (pathArray.length === 0) {
45 for (let i = 0; i < pathArray.length; i++) {
46 if (!Object.prototype.propertyIsEnumerable.call(object, pathArray[i])) {
50 object = object[pathArray[i]];
52 if (object === undefined || object === null) {
53 // `object` is either `undefined` or `null` so we want to stop the loop, and
54 // if this is not the last bit of the path, and
55 // if it did't return `undefined`
56 // it would return `null` if `object` is `null`
57 // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null`
58 if (i !== pathArray.length - 1) {
69 set(object, path, value) {
70 if (!isObj(object) || typeof path !== 'string') {
75 const pathArray = getPathSegments(path);
77 for (let i = 0; i < pathArray.length; i++) {
78 const p = pathArray[i];
80 if (!isObj(object[p])) {
84 if (i === pathArray.length - 1) {
94 delete(object, path) {
95 if (!isObj(object) || typeof path !== 'string') {
99 const pathArray = getPathSegments(path);
101 for (let i = 0; i < pathArray.length; i++) {
102 const p = pathArray[i];
104 if (i === pathArray.length - 1) {
111 if (!isObj(object)) {
118 if (!isObj(object) || typeof path !== 'string') {
122 const pathArray = getPathSegments(path);
123 if (pathArray.length === 0) {
127 // eslint-disable-next-line unicorn/no-for-loop
128 for (let i = 0; i < pathArray.length; i++) {
130 if (!(pathArray[i] in object)) {
134 object = object[pathArray[i]];