hughsk / hughsk/flat

Support for cyclic objects

Open
#161 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.8k
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Any thoughts on supporting cyclic objects?

Currently, this doesn't work.

```ts
let ob = {}
ob.a = ob
ob.key = {key2: 2}

flat(ob) // Uncaught RangeError: Maximum call stack size exceeded
```

Performance would probably take a hit... So, only under a flag? Or even better, under a separate function to keep it tree shakeable.

I think it would make sense to not only support it, but also make the effort to find the shortest path from the root.

My proposal would be to walk through the object level by level, write all non-cyclic references to the output object with concatenated keys. Also keep a reference in a weak map to every object that has been walked. When discovering a new object, before walking through it, check the set, and if found there simply ignore the key, as everything inside has already been discovered by a shorter path.

Keywords: cyclic, self-referencing, recursive, circular

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.