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 pathArr = path.split('.');
16 for (let i = 0; i < pathArr.length; i++) {
19 while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) {
20 p = p.slice(0, -1) + '.';
27 if (!isValidPath(parts)) {
35 get(obj, path, value) {
36 if (!isObj(obj) || typeof path !== 'string') {
37 return value === undefined ? obj : value;
40 const pathArr = getPathSegments(path);
41 if (pathArr.length === 0) {
45 for (let i = 0; i < pathArr.length; i++) {
46 if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) {
50 obj = obj[pathArr[i]];
52 if (obj === undefined || obj === null) {
53 // `obj` 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 `obj` is `null`
57 // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null`
58 if (i !== pathArr.length - 1) {
69 set(obj, path, value) {
70 if (!isObj(obj) || typeof path !== 'string') {
75 const pathArr = getPathSegments(path);
76 if (pathArr.length === 0) {
80 for (let i = 0; i < pathArr.length; i++) {
87 if (i === pathArr.length - 1) {
98 if (!isObj(obj) || typeof path !== 'string') {
102 const pathArr = getPathSegments(path);
104 for (let i = 0; i < pathArr.length; i++) {
105 const p = pathArr[i];
107 if (i === pathArr.length - 1) {
121 if (!isObj(obj) || typeof path !== 'string') {
125 const pathArr = getPathSegments(path);
127 for (let i = 0; i < pathArr.length; i++) {
129 if (!(pathArr[i] in obj)) {
133 obj = obj[pathArr[i]];