Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handled unhandled exceptions when flattening objects #185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Bluebberies
Copy link

Problems

  1. In a real-world application, an API response could include null values for certain fields or undefined values might appear in object fields that were not properly set. If the response is processed by the flatten function, it would result in a crash or an unhandled exception error.

Fix:
Added a check for undefined or null targets at the start of the flatten function.

  1. Using objects that contain metadata or configuration as non-enumerable properties or use symbols to define special properties, flattening fails to include those fields. This could lead to missing data in some cases.

Fix
i resolved this by using Object.getOwnPropertySymbols(object) to retrieve all symbol keys from the object. For each symbol, the corresponding value is accessed, and a new key is constructed by concatenating the previous key with the symbol, using the specified delimiter. This ensures that both symbol-based and enumerable properties are included in the flattened output.

  1. Improper Handling of Circular References. Circular references are common in large datasets, for instance, when an object has child objects that reference the parent, causing self-referential structures. The flatten function does not handle circular references. If an object contains a reference to itself, the flatten function will enter an infinite loop, causing a RangeError: Maximum call stack size exceeded.

Fix
Added a WeakSet to keep track of visited objects during flattening

Ensured all test cases pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant