8000 Support for cyclic objects · Issue #161 · hughsk/flat · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Support for cyclic objects #161
Open
@maximilianMairinger

Description

@maximilianMairinger

Any thoughts on supporting cyclic objects?

Currently, this doesn't work.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0