UNPKG

3.81 kBJavaScriptView Raw
1var utils = require('./utils');
2var util = require('util');
3var $ = utils.getLocaleString;
4
5exports.traverse = function(obj, indent) {
6 if(typeof(obj) != 'string') {
7 for (var i in obj) {
8 var item = obj[i];
9 if (typeof(item) != 'object') {
10 showKeyValue.apply(null,[i, item, indent]);
11 continue;
12 } else {
13 if (item !== null) {
14 if (i === 'tags') {
15 var tagsObj = JSON.stringify(item);
16 showKeyValue.apply(null,[i, tagsObj]);
17 continue;
18 }
19 if(!(item instanceof Array)) {
20 cli.output.header(utils.capitalizeFirstLetter(getHumanReadableFromCamelCase(i)));
21 module.exports.traverse(item, 2);
22 } else {
23 cli.output.header(utils.capitalizeFirstLetter(getHumanReadableFromCamelCase(i)));
24 for(var j in item) {
25 module.exports.traverse(item[j], 2);
26 }
27 }
28 }
29 }
30 }
31 } else {
32 cli.output.list([utils.capitalizeFirstLetter(getHumanReadableFromCamelCase(obj))], 2, false);
33 }
34};
35
36exports.removeEmptyObjects = function(test) {
37 var self = this;
38 for (var i in test) {
39 if (typeof (test[i]) === 'object' && Object.getOwnPropertyNames(test[i]).length === 0) {
40 delete test[i];
41 } else if (typeof test[i] === 'object') {
42 self.removeEmptyObjects(test[i]);
43 }
44 }
45};
46
47exports.removeEmptyArrays = function(test) {
48 var self = this;
49 for (var i in test) {
50 if (test[i] instanceof Array) {
51 // Perform clean up after removeEmptyObjects call
52 for(var j = 0; j < test[i].length; j++) {
53 if(typeof(test[i][j]) == 'undefined') {
54 test[i].splice(j, 1);
55 }
56 }
57 if(test[i].length === 0) {
58 delete test[i];
59 }
60 } else if (typeof test[i] === 'object') {
61 self.removeEmptyArrays(test[i]);
62 }
63 }
64};
65
66exports.generateResourceId = function(subscriptionId, resourceGroup, appGatewayName, resourceType, resourceName) {
67 var id = '';
68 id += '/subscriptions/';
69 id += encodeURIComponent(subscriptionId);
70 id += '/resourceGroups/';
71 id += encodeURIComponent(resourceGroup);
72 id += '/providers/';
73 id += 'Microsoft.Network';
74 id += '/applicationGateways/';
75 id += encodeURIComponent(appGatewayName);
76 id += util.format($('/%s'), resourceType);
77 id += util.format($('/%s'), resourceName);
78 return id;
79};
80
81exports.findIndexByKeyValue = function(inputArray, key, value) {
82 for (var i = 0; i < inputArray.length; i++) {
83 if (inputArray[i][key] == value) {
84 return i;
85 }
86 }
87 return -1;
88};
89
90function getHumanReadableFromCamelCase (inName) {
91 if (!inName)
92 {
93 return inName;
94 }
95
96 var varName = inName;
97 var outName = '';
98
99 var i = 0;
100 while (i < varName.length) {
101 if (i === 0 || varName[i] === varName[i].toUpperCase()) {
102 if (i > 0) {
103 outName += ' ';
104 }
105
106 var abbrWords =['VM', 'IP', 'RM', 'OS', 'NAT', 'IDs', 'DNS', 'VNet', 'ASN', 'SubNet', 'TLSv1_0', 'TLSv1_1', 'TLSv1_2'];
107 var matched = false;
108 var matchedAbbr = '';
109 abbrWords.every(function(item) {
110 if (varName.substring(i).lastIndexOf(item, 0) === 0) {
111 matched = true;
112 matchedAbbr = item;
113 return false;
114 }
115 return true;
116 });
117
118 if (matched) {
119 outName += matchedAbbr;
120 i = i + matchedAbbr.length;
121 }
122 else
123 {
124 var j = i + 1;
125 while ((j < varName.length) && varName[j] == varName[j].toLowerCase())
126 {
127 j++;
128 }
129 outName += varName.substring(i, j);
130 i = j;
131 }
132 }
133 else
134 {
135 i++;
136 }
137 }
138
139 return outName;
140}
141
142function showKeyValue(key, value, indent) {
143 cli.output.nameValue(utils.capitalizeFirstLetter(getHumanReadableFromCamelCase(key)), value, indent);
144}