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 #10297

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,19 @@ function toJsonReplacer(key, value) {
* stripped since angular uses this notation internally.
*
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
* @param {boolean|number=} pretty If set to true, the JSON output will contain newlines and whitespace.
* If set to an integer, the JSON output will contain that many spaces per indentation (the default is 2).
* @returns {string|undefined} JSON-ified string representing `obj`.
*/
function toJson(obj, pretty) {
if (typeof obj === 'undefined') return undefined;
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
if (typeof pretty !== "number") {
Copy link
Member

Choose a reason for hiding this comment

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

I always prefer to use the helpers (isNumber()).
(And even if you choose not to, using single quotes is more consistent.)

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 true --- I dunno if we need to do this or not anyway though, like I said it's a pretty low-impact breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pretty = !!pretty;
}
if (pretty === true) {
pretty = 2;
}
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 if VMs make such optimizations anyway, but (if not) you can skip unnecessary execution of the first if, by reordering like this:

if (pretty === true) {
    pretty = 2;
} else if (!isNumber(pretty)) {
    pretty = !!pretty;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

[very minor nit]
I think that

if (typeof pretty !== "number") {
  pretty = pretty ? 2 : null;
}

is easier to read

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, that's better --- thanks =)

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


Expand Down
14 changes: 10 additions & 4 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,25 +501,31 @@ function dateFilter($locale) {
* the binding is automatically converted to JSON.
*
* @param {*} object Any JavaScript object (including arrays and primitive types) to filter.
* @param {number=} spacing The number of spaces to use per indentation, defaults to 2.
* @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) {
if (isUndefined(spacing)) {
spacing = 2;
}
return toJson(object, spacing);
};
}

Expand Down
12 changes: 10 additions & 2 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,17 @@ describe('angular', function() {

it('should format objects pretty', function() {
expect(toJson({a: 1, b: 2}, true)).
toBeOneOf('{\n "a": 1,\n "b": 2\n}', '{\n "a":1,\n "b":2\n}');
toBe('{\n "a": 1,\n "b": 2\n}');
expect(toJson({a: {b: 2}}, true)).
toBeOneOf('{\n "a": {\n "b": 2\n }\n}', '{\n "a":{\n "b":2\n }\n}');
toBe('{\n "a": {\n "b": 2\n }\n}');
expect(toJson({a: 1, b: 2}, false)).
toBe('{"a":1,"b":2}');
expect(toJson({a: 1, b: 2}, 0)).
toBe('{"a":1,"b":2}');
expect(toJson({a: 1, b: 2}, 1)).
toBe('{\n "a": 1,\n "b": 2\n}');
expect(toJson({a: 1, b: 2}, {})).
toBe('{\n "a": 1,\n "b": 2\n}');
});


Expand Down
3 changes: 3 additions & 0 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ describe('filters', function() {
it('should do basic filter', function() {
expect(filter('json')({a:"b"})).toEqual(toJson({a:"b"}, true));
});
it('should allow custom indentation', function() {
expect(filter('json')({a:"b"}, 4)).toEqual(toJson({a:"b"}, 4));
});
});

describe('lowercase', function() {
Expand Down