Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / configstore / node_modules / dot-prop / index.js
1 'use strict';
2 const isObj = require('is-obj');
3
4 function getPathSegments(path) {
5         const pathArr = path.split('.');
6         const parts = [];
7
8         for (let i = 0; i < pathArr.length; i++) {
9                 let p = pathArr[i];
10
11                 while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) {
12                         p = p.slice(0, -1) + '.';
13                         p += pathArr[++i];
14                 }
15
16                 parts.push(p);
17         }
18
19         return parts;
20 }
21
22 module.exports = {
23         get(obj, path, value) {
24                 if (!isObj(obj) || typeof path !== 'string') {
25                         return value === undefined ? obj : value;
26                 }
27
28                 const pathArr = getPathSegments(path);
29
30                 for (let i = 0; i < pathArr.length; i++) {
31                         if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) {
32                                 return value;
33                         }
34
35                         obj = obj[pathArr[i]];
36
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) {
44                                         return value;
45                                 }
46
47                                 break;
48                         }
49                 }
50
51                 return obj;
52         },
53
54         set(obj, path, value) {
55                 if (!isObj(obj) || typeof path !== 'string') {
56                         return obj;
57                 }
58
59                 const root = obj;
60                 const pathArr = getPathSegments(path);
61
62                 for (let i = 0; i < pathArr.length; i++) {
63                         const p = pathArr[i];
64
65                         if (!isObj(obj[p])) {
66                                 obj[p] = {};
67                         }
68
69                         if (i === pathArr.length - 1) {
70                                 obj[p] = value;
71                         }
72
73                         obj = obj[p];
74                 }
75
76                 return root;
77         },
78
79         delete(obj, path) {
80                 if (!isObj(obj) || typeof path !== 'string') {
81                         return;
82                 }
83
84                 const pathArr = getPathSegments(path);
85
86                 for (let i = 0; i < pathArr.length; i++) {
87                         const p = pathArr[i];
88
89                         if (i === pathArr.length - 1) {
90                                 delete obj[p];
91                                 return;
92                         }
93
94                         obj = obj[p];
95
96                         if (!isObj(obj)) {
97                                 return;
98                         }
99                 }
100         },
101
102         has(obj, path) {
103                 if (!isObj(obj) || typeof path !== 'string') {
104                         return false;
105                 }
106
107                 const pathArr = getPathSegments(path);
108
109                 for (let i = 0; i < pathArr.length; i++) {
110                         if (isObj(obj)) {
111                                 if (!(pathArr[i] in obj)) {
112                                         return false;
113                                 }
114
115                                 obj = obj[pathArr[i]];
116                         } else {
117                                 return false;
118                         }
119                 }
120
121                 return true;
122         }
123 };