1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 import { Injectable } from "@angular/core";
\r
18 import { toInteger } from "@ng-bootstrap/ng-bootstrap/util/util";
\r
24 export class ToHtml {
\r
27 convert(json:any = [{name: 'Adam', age: 23}, {name: 'Raj', age: 22}, {name: 'Justin', age: 5}], tabs = 0){
\r
30 if(typeof json === 'string'){
\r
31 json = JSON.parse(json);
\r
33 for(let i = 0; i < tabs; i++){
\r
34 tabHtml += ' ';
\r
36 for(let key in json){
\r
37 if(json.hasOwnProperty(key)){
\r
38 if(typeof json[key] === "object"){
\r
39 html += tabHtml + '<b><u>' + key + ':</u></b><br/>';
\r
40 if(json.constructor === Array && toInteger(key) > 0){
\r
43 html += this.convert(json[key], ++tabs);
\r
45 html += tabHtml + '<b><u>' + key + ':</u></b>' + '<br/>';
\r
46 if(typeof json[key] === 'string'){
\r
47 json[key] = json[key].replace(/\\n/g, '<br/>' + tabHtml);
\r
49 html += tabHtml + json[key] + '<br/>';
\r
58 return str.replace(/\\n/g, '<br/>');
\r