Deep Clone with Cycles and Maps

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write cloneDeep(value), a deep clone for messier data. Copy plain objects, arrays, Dates, Maps and Sets at every depth: a Map's keys stay as they are and its values are cloned, and a Set's members are cloned. Return primitives and functions as they are. The data may contain cycles (an object that contains itself, directly or further down) and shared references (one object reached by two paths). Clone each original object only once, so the copy has the same shape as the original: its cycles point at the copies, and objects shared in the original are shared in the copy. Don't change the input.

Your code must pass

  • Copies nested objects, arrays and Dates
  • Copies Maps and Sets, cloning their values
  • Copies an object that contains itself
  • Keeps shared references shared
  • Handles cycles through arrays, Maps and Sets