All files / src/utils json.ts

100% Statements 29/29
100% Branches 14/14
100% Functions 1/1
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43          1x 26x 11x 11x 5x 1x   4x 4x 7x 7x 3x   7x   4x 4x   6x 6x 1x   5x 5x 10x   10x 10x 5x   10x   5x 5x     15x  
/*---------------------------------------------------------------------------------------------
*  Copyright (c) Microsoft Corporation. All rights reserved.
*  Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
 
export function stringifyObject(obj: any, indent: string, stringifyLiteral: (val: any) => string): string {
	if (obj !== null && typeof obj === 'object') {
		const newIndent = indent + '\t';
		if (Array.isArray(obj)) {
			if (obj.length === 0) {
				return '[]';
			}
			let result = '[\n';
			for (let i = 0; i < obj.length; i++) {
				result += newIndent + stringifyObject(obj[i], newIndent, stringifyLiteral);
				if (i < obj.length - 1) {
					result += ',';
				}
				result += '\n';
			}
			result += indent + ']';
			return result;
		} else {
			const keys = Object.keys(obj);
			if (keys.length === 0) {
				return '{}';
			}
			let result = '{\n';
			for (let i = 0; i < keys.length; i++) {
				const key = keys[i];
 
				result += newIndent + JSON.stringify(key) + ': ' + stringifyObject(obj[key], newIndent, stringifyLiteral);
				if (i < keys.length - 1) {
					result += ',';
				}
				result += '\n';
			}
			result += indent + '}';
			return result;
		}
	}
	return stringifyLiteral(obj);
}