MCPcopy
hub / github.com/plotly/plotly.js / npSet

Function npSet

src/lib/nested_property.js:131–184  ·  view source on GitHub ↗
(cont, parts, propStr)

Source from the content-addressed store, hash-verified

129}
130
131function npSet(cont, parts, propStr) {
132 return function(val) {
133 var curCont = cont;
134 var propPart = '';
135 var containerLevels = [[cont, propPart]];
136 var toDelete = isDeletable(val, propStr);
137 var curPart;
138 var i;
139
140 for(i = 0; i < parts.length - 1; i++) {
141 curPart = parts[i];
142
143 if(typeof curPart === 'number' && !isArrayOrTypedArray(curCont)) {
144 throw 'array index but container is not an array';
145 }
146
147 // handle special -1 array index
148 if(curPart === -1) {
149 toDelete = !setArrayAll(curCont, parts.slice(i + 1), val, propStr);
150 if(toDelete) break;
151 else return;
152 }
153
154 if(!checkNewContainer(curCont, curPart, parts[i + 1], toDelete)) {
155 break;
156 }
157
158 curCont = curCont[curPart];
159
160 if(typeof curCont !== 'object' || curCont === null) {
161 throw 'container is not an object';
162 }
163
164 propPart = joinPropStr(propPart, curPart);
165
166 containerLevels.push([curCont, propPart]);
167 }
168
169 if(toDelete) {
170 if(i === parts.length - 1) {
171 delete curCont[parts[i]];
172
173 // The one bit of pruning we still do: drop `undefined` from the end of arrays.
174 // In case someone has already unset previous items, continue until we hit a
175 // non-undefined value.
176 if(Array.isArray(curCont) && +parts[i] === curCont.length - 1) {
177 while(curCont.length && curCont[curCont.length - 1] === undefined) {
178 curCont.pop();
179 }
180 }
181 }
182 } else curCont[parts[i]] = val;
183 };
184}
185
186function joinPropStr(propStr, newPart) {
187 var toAdd = newPart;

Callers 3

nested_property.jsFile · 0.85
setArrayAllFunction · 0.85
makeNPFunction · 0.85

Calls 5

isDeletableFunction · 0.85
setArrayAllFunction · 0.85
checkNewContainerFunction · 0.85
joinPropStrFunction · 0.85
isArrayOrTypedArrayFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…