MCPcopy Index your code
hub / github.com/react/react / copyWithRename

Function copyWithRename

packages/react-devtools-shared/src/backend/utils/index.js:77–99  ·  view source on GitHub ↗
(
  obj: Object | Array<any>,
  oldPath: Array<string | number>,
  newPath: Array<string | number>,
  index: number = 0,
)

Source from the content-addressed store, hash-verified

75// This function expects paths to be the same except for the final value.
76// e.g. ['path', 'to', 'foo'] and ['path', 'to', 'bar']
77export 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
101export function copyWithSet(
102 obj: Object | Array<any>,

Callers 2

renamePathFunction · 0.90
renamePathFunction · 0.90

Calls 1

isArrayFunction · 0.85

Tested by

no test coverage detected