This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 3 files changed +16
-6
lines changed
3 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -964,12 +964,13 @@ function toJsonReplacer(key, value) {
964
964
* stripped since angular uses this notation internally.
965
965
*
966
966
* @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).
968
969
* @returns {string|undefined } JSON-ified string representing `obj`.
969
970
*/
970
971
function toJson ( obj , pretty ) {
971
972
if ( typeof obj === 'undefined' ) return undefined ;
972
- return JSON . stringify ( obj , toJsonReplacer , pretty ? ' ' : null ) ;
973
+ return JSON . stringify ( obj , toJsonReplacer , pretty === true ? 2 : pretty ) ;
973
974
}
974
975
975
976
Original file line number Diff line number Diff line change @@ -501,25 +501,31 @@ function dateFilter($locale) {
501
501
* the binding is automatically converted to JSON.
502
502
*
503
503
* @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.
504
505
* @returns {string } JSON string.
505
506
*
506
507
*
507
508
* @example
508
509
<example>
509
510
<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>
511
513
</file>
512
514
<file name="protractor.js" type="protractor">
513
515
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}/);
515
518
});
516
519
</file>
517
520
</example>
518
521
*
519
522
*/
520
523
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 ) ;
523
529
} ;
524
530
}
525
531
Original file line number Diff line number Diff line change @@ -210,6 +210,9 @@ describe('filters', function() {
210
210
it ( 'should do basic filter' , function ( ) {
211
211
expect ( filter ( 'json' ) ( { a :"b" } ) ) . toEqual ( toJson ( { a :"b" } , true ) ) ;
212
212
} ) ;
213
+ it ( 'should allow custom indentation' , function ( ) {
214
+ expect ( filter ( 'json' ) ( { a :"b" } , 4 ) ) . toEqual ( toJson ( { a :"b" } , 4 ) ) ;
215
+ } ) ;
213
216
} ) ;
214
217
215
218
describe ( 'lowercase' , function ( ) {
You can’t perform that action at this time.
0 commit comments