Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(jsonFilter): add optional arg to define custom indentation #9771

Closed
wants to merge 11 commits into from

Conversation

doshprompt
Copy link
Contributor

also change toJson function to accomodate passing in of number of spaces

@@ -969,7 +969,8 @@ function toJsonReplacer(key, value) {
*/
function toJson(obj, pretty) {
if (typeof obj === 'undefined') return undefined;
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
var spaces = pretty ? (angular.isNumber(pretty) ? new Array(pretty + 1).join(' ') : ' ') : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little far-fetched, but you can't use pretty-printing with 0-space indentation.
Just saying...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right, I didn't think of that edge case, will push a slight refactor.

it is  better to use an explicit number (instead of true),
so that the default behaviour of jsonFilter does not change
when / if the default indentation of toJson changes.
due to missing matching open parens
missing matching close parens
@caitp
Copy link
Contributor

caitp commented Oct 24, 2014

there are 9 commits for this, wow o_o

return function(object) {
return toJson(object, true);
return function(object, spacing) {
if (isUndefined(spacing)) spacing = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (isUndefined(spacing)) {
  spacing = 2;
}

@caitp
Copy link
Contributor

caitp commented Oct 24, 2014

It basically looks good to me if you clean up the nits

@caitp caitp added this to the 1.3.x milestone Oct 24, 2014
@caitp caitp self-assigned this Oct 24, 2014
@doshprompt
Copy link
Contributor Author

Should I move the parseInt logic from the toJson function into the filter or leave it as is?

@caitp
Copy link
Contributor

caitp commented Oct 30, 2014

@doshprompt, parseint isn't needed at all. what would we use that for?

@@ -969,7 +969,7 @@ function toJsonReplacer(key, value) {
*/
function toJson(obj, pretty) {
if (typeof obj === 'undefined') return undefined;
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
return JSON.stringify(obj, toJsonReplacer, pretty ? parseInt(pretty) || 2 : null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly this should just be JSON.stringify(obj, toJsonReplacer, pretty); --- browsers don't care if pretty is null or undefined or the empty string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea is to keep backward compatibility by defaulting to 2 spaces when pretty is truthy, while allowing arbitrary indentations as well.
(Although, the way it is implemented, won't allow 0 indentation.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old stuff was wrong too --- if anything it should be pretty === true ? 2 : pretty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need parseInt in here anywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the previous implementation was wrong, but I don't see how we can retain backward compatibility without parseInt (or a check for whether pretty is a number).

Unless we don't care retaining backward compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt isn't needed for backwards compatibility, it wasn't used before

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realistically, nobody is passing objects or NaN or other weird stuff to this, and if they are, who cares? it's not like it's a critical part of anyone's app

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I was clear before, so here is what I mean:

Old implementation:

pretty -> falsy: Create "ugly" JSON.
pretty -> truthy: Create pretty JSON with 2 spaces indentation.

New implementation:

pretty -> falsy: Create "ugly" JSON.
pretty -> truthy:
    pretty -> number (say X): Create pretty JSON with X spaces indentation.
    pretty -> non-numeric: Create pretty JSON with 2 spaces indentation (default).

The parseInt() is used for detecting true as well as other truthy, non-numeric values (although the latter would be rare).
I agree that it would be probably better to allow the user to pass anything as pretty (e.g. custom indentation string) and default to 2 spaces only if pretty === true, but it would be a breaking change (in the sense that it would work differently than the previous version for specific inputs).

That said (and since it is indeed not expected to be a critical aspect of anyone's app), if you think we should go with pretty === true ? 2 : pretty it sounds good to me.

@doshprompt:
Could you update the PR with the above change ?
It would be also nice to update the docs (for toJson() and json filter).

I don't know how we manage to make long pointless discussions on simple things (but somehow we do 😃).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt() isn't needed for detecting numbers. But regardless, we don't need to worry about the "truthy vs true" thing, because it was a dumb idea to begin with and it's not going to matter intensely to anyone who uses it

caitp pushed a commit to caitp/angular.js that referenced this pull request Dec 2, 2014
also change toJson function to accomodate passing in of number of spaces

Closes angular#9771
@caitp caitp closed this in 1191edb Dec 2, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants