From 9900b4e663c982d604a87491c503eccaeae2df89 Mon Sep 17 00:00:00 2001 From: Rahul Doshi Date: Fri, 24 Oct 2014 05:28:30 +0200 Subject: [PATCH 1/3] feat(jsonFilter): add optional arg to define custom indentation also change toJson function to accomodate passing in of number of spaces Closes #9771 --- src/Angular.js | 5 +++-- src/ng/filter/filters.js | 14 ++++++++++---- test/ng/filter/filtersSpec.js | 3 +++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index fc0b378369eb..3d73a667d8b7 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -964,12 +964,13 @@ 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); + return JSON.stringify(obj, toJsonReplacer, pretty === true ? 2 : pretty); } diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 1d42415c8916..9474918b8277 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -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 -
{{ {'name':'value'} | json }}
+
{{ {'name':'value'} | json }}
+
{{ {'name':'value'} | json:4 }}
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}/); });
* */ function jsonFilter() { - return function(object) { - return toJson(object, true); + return function(object, spacing) { + if (isUndefined(spacing)) { + spacing = 2; + } + return toJson(object, spacing); }; } diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index e5d56316388e..977055bf1b81 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -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() { From 1c9c35883fe34192feb5968afa5b8d92ede16669 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 2 Dec 2014 15:39:08 -0500 Subject: [PATCH 2/3] test(toJson): add extra test cases for new `pretty` behaviour --- test/AngularSpec.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 4349fb2a656f..97436ea67003 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -1189,11 +1189,17 @@ describe('angular', function() { }); - it('should format objects pretty', function() { + iit('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}'); }); From bc6de6944c352430e1386ed946fbd0025e8797ac Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 2 Dec 2014 15:43:03 -0500 Subject: [PATCH 3/3] refactor(toJson): remove breaking change from previous CL --- src/Angular.js | 8 +++++++- test/AngularSpec.js | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 3d73a667d8b7..5da082d28adc 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -970,7 +970,13 @@ function toJsonReplacer(key, value) { */ function toJson(obj, pretty) { if (typeof obj === 'undefined') return undefined; - return JSON.stringify(obj, toJsonReplacer, pretty === true ? 2 : pretty); + if (typeof pretty !== "number") { + pretty = !!pretty; + } + if (pretty === true) { + pretty = 2; + } + return JSON.stringify(obj, toJsonReplacer, pretty); } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 97436ea67003..e63789100947 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -1189,7 +1189,7 @@ describe('angular', function() { }); - iit('should format objects pretty', function() { + it('should format objects pretty', function() { expect(toJson({a: 1, b: 2}, true)). toBe('{\n "a": 1,\n "b": 2\n}'); expect(toJson({a: {b: 2}}, true)). @@ -1200,6 +1200,8 @@ describe('angular', function() { 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}'); });