-
Notifications
You must be signed in to change notification settings - Fork 28
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
Empty object fix #20
Empty object fix #20
Conversation
- Display empty objects and arrays without formatting
This reverts commit 2714179.
- Display empty objects and arrays without formatting
I meant to make a feature branch before hand for this PR. This one at least has fixes for #19 as well as the empty object check. |
@@ -87,6 +87,12 @@ listen((port, msg) => { | |||
port.postMessage(['NOT JSON', 'technically JSON but not an object or array']); | |||
port.disconnect(); | |||
return; | |||
} | |||
// If there's an empty object or array, return JSON as is | |||
else if (Object.entries(obj).length === 0 || obj.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't obj.length === 0
be sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion. obj.length === 0
would only work for the array since arrays have a length
property, but not objects. (This can also be verified by trying object.hasOwnProperty('length');
Since [Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
returns an array of key value pairs, we can use that to see if an object has any content. (you could do this with .keys()
too. I tend to use .entries()
for semantics).
Looks good to me 👍 |
Thanks for the PR! I admit I missed the GitHub notifications coming up on this repo, or perhaps they're not configured correctly. I'll check and try to stay on top of this in the future. @acupoftee, how does this manifest in the browser when there's an empty object or array? Could you include a screenshot for ease of review? From a code point of view I'm happy with the change, I just want to check how it displays. |
Thanks! In the future I may loop back to make it so that the formatter still instantiates in this situation (without hanging) but this is much better than a hang. I'll look at releasing a new version shortly. |
Sounds awesome. Thank you for reviewing and for your work on this version! |
This PR addresses Issues #10 and #18
My approach for this iteration was to add a separate check for empty objects and arrays in
background.js
before the text is passed to the tokenizer:This will return the empty object or array without the styling to prevent formatting bug.