(pieces, value, index, obj)
| 172 | } |
| 173 | |
| 174 | function _setVal(pieces, value, index, obj) { |
| 175 | // iterate until one before the end (the last piece is the final key to set) |
| 176 | var max = pieces.length - 1; |
| 177 | while (index < max) { |
| 178 | var piece = pieces[index++]; |
| 179 | if (typeof piece == 'function') { |
| 180 | // filter an array based on a filter function, all matching array items |
| 181 | // should be set to the proper value recursively |
| 182 | return obj.filter(piece).forEach(bind(this, _setVal, pieces, value, index)); |
| 183 | } |
| 184 | |
| 185 | // if the next value is not defined or of the wrong type, set it to an |
| 186 | // empty object or array |
| 187 | var nextType = typeof pieces[0]; |
| 188 | if (nextType == 'number' || nextType == 'function') { |
| 189 | if (!Array.isArray(obj[piece])) { |
| 190 | obj[piece] = []; |
| 191 | } |
| 192 | } else { |
| 193 | if (typeof obj[piece] != 'object' || obj[piece] == null) { |
| 194 | obj[piece] = {}; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | obj = obj[piece]; |
| 199 | } |
| 200 | |
| 201 | if (typeof pieces[max] == 'function') { |
| 202 | // if the last part of the path is a filter, we need to conditionally set |
| 203 | // the elements in the array |
| 204 | var filter = pieces[max]; |
| 205 | var i = obj.length; |
| 206 | while (i) { |
| 207 | // iterate backward in case we're deleting elements |
| 208 | if (filter(obj[--i])) { |
| 209 | setValueForKey(obj, i, value); |
| 210 | } |
| 211 | } |
| 212 | } else { |
| 213 | setValueForKey(obj, pieces[max], value); |
| 214 | } |
| 215 | }; |
no test coverage detected
searching dependent graphs…