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

Commit 1191edb

Browse files
doshpromptcaitp
authored andcommitted
feat(jsonFilter): add optional arg to define custom indentation
also change toJson function to accomodate passing in of number of spaces Closes #9771
1 parent c8c9bbc commit 1191edb

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/Angular.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,13 @@ function toJsonReplacer(key, value) {
964964
* stripped since angular uses this notation internally.
965965
*
966966
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
967-
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
967+
* @param {boolean|number=} pretty If set to true, the JSON output will contain newlines and whitespace.
968+
* If set to an integer, the JSON output will contain that many spaces per indentation (the default is 2).
968969
* @returns {string|undefined} JSON-ified string representing `obj`.
969970
*/
970971
function toJson(obj, pretty) {
971972
if (typeof obj === 'undefined') return undefined;
972-
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
973+
return JSON.stringify(obj, toJsonReplacer, pretty === true ? 2 : pretty);
973974
}
974975

975976

src/ng/filter/filters.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -501,25 +501,31 @@ function dateFilter($locale) {
501501
* the binding is automatically converted to JSON.
502502
*
503503
* @param {*} object Any JavaScript object (including arrays and primitive types) to filter.
504+
* @param {number=} spacing The number of spaces to use per indentation, defaults to 2.
504505
* @returns {string} JSON string.
505506
*
506507
*
507508
* @example
508509
<example>
509510
<file name="index.html">
510-
<pre>{{ {'name':'value'} | json }}</pre>
511+
<pre id="default-spacing">{{ {'name':'value'} | json }}</pre>
512+
<pre id="custom-spacing">{{ {'name':'value'} | json:4 }}</pre>
511513
</file>
512514
<file name="protractor.js" type="protractor">
513515
it('should jsonify filtered objects', function() {
514-
expect(element(by.binding("{'name':'value'}")).getText()).toMatch(/\{\n "name": ?"value"\n}/);
516+
expect(element(by.id('default-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
517+
expect(element(by.id('custom-spacing')).getText()).toMatch(/\{\n "name": ?"value"\n}/);
515518
});
516519
</file>
517520
</example>
518521
*
519522
*/
520523
function jsonFilter() {
521-
return function(object) {
522-
return toJson(object, true);
524+
return function(object, spacing) {
525+
if (isUndefined(spacing)) {
526+
spacing = 2;
527+
}
528+
return toJson(object, spacing);
523529
};
524530
}
525531

test/ng/filter/filtersSpec.js

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ describe('filters', function() {
210210
it('should do basic filter', function() {
211211
expect(filter('json')({a:"b"})).toEqual(toJson({a:"b"}, true));
212212
});
213+
it('should allow custom indentation', function() {
214+
expect(filter('json')({a:"b"}, 4)).toEqual(toJson({a:"b"}, 4));
215+
});
213216
});
214217

215218
describe('lowercase', function() {

0 commit comments

Comments
 (0)