From 1191edba4eaa15f675fa4ed047949a150843971b Mon Sep 17 00:00:00 2001 From: Rahul Doshi Date: Fri, 24 Oct 2014 05:28:30 +0200 Subject: [PATCH] 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 fc0b3783..3d73a667 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 1d42415c..9474918b 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 e5d56316..977055bf 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() {