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
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return JSON.stringify(obj, toJsonReplacer, spaces);
}


Expand Down
11 changes: 7 additions & 4 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,25 +503,28 @@ function dateFilter($locale) {
* the binding is automatically converted to JSON.
*
* @param {*} object Any JavaScript object (including arrays and primitive types) to filter.
* @param {number=} the number of spaces to use per indentation, defaults to 2.
Copy link
Member

Choose a reason for hiding this comment

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

This needs a param name (i.e. spacing) before the description.
Also, if the above change is implemented (allowing anything as spacing) the type and description should be modified accordingly.

* @returns {string} JSON string.
*
*
* @example
<example>
<file name="index.html">
<pre>{{ {'name':'value'} | json }}</pre>
<pre id="default-spacing">{{ {'name':'value'} | json }}</pre>
<pre id="custom-spacing">{{ {'name':'value'} | json:4 }}</pre>
</file>
<file name="protractor.js" type="protractor">
it('should jsonify filtered objects', function() {
expect(element(by.binding("{'name':'value'}")).getText()).toMatch(/\{\n "name": ?"value"\n}/);
expect(element(by.id('default-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
expect(element(by.id('custom-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
});
</file>
</example>
*
*/
function jsonFilter() {
return function(object) {
return toJson(object, true);
return function(object, spacing) {
return toJson(object, spacing || true);
Copy link
Member

Choose a reason for hiding this comment

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

No 0-space indentation here either.
BTW, it ight be 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll fix that too now.

};
}

Expand Down