2 const isObj = require('is-obj');
4 function getPathSegments(path) {
5 const pathArr = path.split('.');
8 for (let i = 0; i < pathArr.length; i++) {
11 while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) {
12 p = p.slice(0, -1) + '.';
23 get(obj, path, value) {
24 if (!isObj(obj) || typeof path !== 'string') {
25 return value === undefined ? obj : value;
28 const pathArr = getPathSegments(path);
30 for (let i = 0; i < pathArr.length; i++) {
31 if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) {
35 obj = obj[pathArr[i]];
37 if (obj === undefined || obj === null) {
38 // `obj` is either `undefined` or `null` so we want to stop the loop, and
39 // if this is not the last bit of the path, and
40 // if it did't return `undefined`
41 // it would return `null` if `obj` is `null`
42 // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null`
43 if (i !== pathArr.length - 1) {
54 set(obj, path, value) {
55 if (!isObj(obj) || typeof path !== 'string') {
60 const pathArr = getPathSegments(path);
62 for (let i = 0; i < pathArr.length; i++) {
69 if (i === pathArr.length - 1) {
80 if (!isObj(obj) || typeof path !== 'string') {
84 const pathArr = getPathSegments(path);
86 for (let i = 0; i < pathArr.length; i++) {
89 if (i === pathArr.length - 1) {
103 if (!isObj(obj) || typeof path !== 'string') {
107 const pathArr = getPathSegments(path);
109 for (let i = 0; i < pathArr.length; i++) {
111 if (!(pathArr[i] in obj)) {
115 obj = obj[pathArr[i]];