( obj: Object | Array<any>, oldPath: Array<string | number>, newPath: Array<string | number>, index: number = 0, )
| 75 | // This function expects paths to be the same except for the final value. |
| 76 | // e.g. ['path', 'to', 'foo'] and ['path', 'to', 'bar'] |
| 77 | export function copyWithRename( |
| 78 | obj: Object | Array<any>, |
| 79 | oldPath: Array<string | number>, |
| 80 | newPath: Array<string | number>, |
| 81 | index: number = 0, |
| 82 | ): Object | Array<any> { |
| 83 | const oldKey = oldPath[index]; |
| 84 | const updated = isArray(obj) ? obj.slice() : {...obj}; |
| 85 | if (index + 1 === oldPath.length) { |
| 86 | const newKey = newPath[index]; |
| 87 | // $FlowFixMe[incompatible-use] number or string is fine here |
| 88 | updated[newKey] = updated[oldKey]; |
| 89 | if (isArray(updated)) { |
| 90 | updated.splice(((oldKey: any): number), 1); |
| 91 | } else { |
| 92 | delete updated[oldKey]; |
| 93 | } |
| 94 | } else { |
| 95 | // $FlowFixMe[incompatible-use] number or string is fine here |
| 96 | updated[oldKey] = copyWithRename(obj[oldKey], oldPath, newPath, index + 1); |
| 97 | } |
| 98 | return updated; |
| 99 | } |
| 100 | |
| 101 | export function copyWithSet( |
| 102 | obj: Object | Array<any>, |
no test coverage detected