| 166 | // retrieves the next data object to recursively iterate on |
| 167 | // throws if trying to override a literal value or add props to an array |
| 168 | const next = () => { |
| 169 | const haveContents = !force && _data[_key] != null && value !== _delete |
| 170 | const shouldNotOverrideLiteralValue = !(typeof _data[_key] === 'object') |
| 171 | // if the next obj to recurse is an array and the next key to be appended to the resulting obj is not an array index, then it should throw since we can't append arbitrary props to arrays |
| 172 | const shouldNotAddPropsToArrays = |
| 173 | typeof keys[0] !== 'symbol' && Array.isArray(_data[_key]) && Number.isNaN(Number(keys[0])) |
| 174 | |
| 175 | const overrideError = haveContents && shouldNotOverrideLiteralValue |
| 176 | if (overrideError) { |
| 177 | throw Object.assign( |
| 178 | new Error(`Property ${_key} already exists and is not an Array or Object.`), |
| 179 | { code: 'EOVERRIDEVALUE' } |
| 180 | ) |
| 181 | } |
| 182 | |
| 183 | const addPropsToArrayError = haveContents && shouldNotAddPropsToArrays |
| 184 | if (addPropsToArrayError) { |
| 185 | throw Object.assign(new Error(`Can't add property ${key} to an Array.`), { |
| 186 | code: 'ENOADDPROP', |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | return typeof _data[_key] === 'object' ? _data[_key] || {} : {} |
| 191 | } |
| 192 | |
| 193 | // sets items from the parsed array of keys as objects, recurses to setKeys in case there are still items to be handled |
| 194 | // otherwise, it just sets the original value set by the user |