Skip to content

Commit

Permalink
Improve JSON stringify in support.html.
Browse files Browse the repository at this point in the history
This improves the code in support.html that stringifies a JavaScript
object.  Before, it assumed a specific layout. This adds a more
general stringify method that will support nested objects correctly.

Change-Id: Ie59747d0ce634ef028c4819b92fc2fd61552f724
  • Loading branch information
TheModMaker committed May 23, 2016
1 parent c7c2b41 commit e105db3
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions support.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,34 @@
}
}

/**
* Converts the given object to a formatted string that can be printed.
* It uses the given indent to indent each line.
*
* We cannot simply use JSON.stringify since it is not supported on older
* browsers; so this is a version that is supported on all browsers.
*/
function stringify(obj, indent) {
var ret = '{\n';
for (var key in obj) {
var val = obj[key];
ret += indent + ' ' + key + ': ';
if (typeof(val) == 'object')
ret += stringify(val, indent + ' ');
else
ret += val.toString();
ret += ',\n';
}
ret += indent + '}';
return ret;
}

function doTest() {
shaka.polyfill.installAll();
shaka.Player.support().then(function(support) {
var userAgent = navigator.userAgent;
var formatted = userAgent + '\n' + shaka.Player.version + '\n\n';

var indent = ' ';
formatted += '{\n';
for (var topLevelKey in support) {
formatted += indent + topLevelKey + ': ';
if (typeof(support[topLevelKey]) == 'boolean') {
formatted += support[topLevelKey].toString();
} else {
var obj = support[topLevelKey];
formatted += '{\n';
for (var subKey in obj) {
formatted += indent + indent + subKey + ': ';
formatted += obj[subKey].toString();
formatted += '\n';
}
formatted += indent + '}';
}
formatted += '\n';
}
formatted += '}\n';
formatted += stringify(support, '');

var output = document.getElementById('output');

Expand Down

0 comments on commit e105db3

Please sign in to comment.