(obj, path, value)
| 98 | * - for arrays: inserts the value at the specified index, instead of replacing it |
| 99 | */ |
| 100 | export function insertInMutative(obj, path, value) { |
| 101 | if (!obj) { |
| 102 | return; |
| 103 | } |
| 104 | if (path.length === 0) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | let current = obj || {}; |
| 109 | for (let i = 0; i < path.length - 1; i++) { |
| 110 | const key = path[i]; |
| 111 | if (!(key in current) || typeof current[key] !== 'object') { |
| 112 | current[key] = typeof path[i + 1] === 'number' ? [] : {}; |
| 113 | } |
| 114 | current = current[key]; |
| 115 | } |
| 116 | |
| 117 | const key = path[path.length - 1]; |
| 118 | if (Array.isArray(current) && typeof key === 'number') { |
| 119 | current.splice(key, 0, value); |
| 120 | } else { |
| 121 | current[key] = value; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | export function assocInMutative(obj, path, value) { |
| 126 | if (!obj) { |
no outgoing calls
no test coverage detected