-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.html
96 lines (87 loc) · 3.72 KB
/
index.html
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html>
<head>
<title>JSON.prune.js : a pruning stringifier</title>
<script src=JSON.prune.js></script>
<style>
pre {margin-left:20px}
body {margin-bottom: 200px}
</style>
</head>
<body>
<h2>What it is</h2>
<p>
A pruning <code>JSON.stringify</code> for the very specific cases where you need to be able to <code>stringify</code> big or recursive javascript objects and don't really need the result to be complete.
</p>
<code><pre>
var json = JSON.stringify(window); // this fails
var json = JSON.prune(window); // this builds a JSON valid string from a pruned version of the recursive, deep, and not totally accessible window object
var prunedWindow = JSON.parse(JSON.prune(window)); // this builds a lighter and not recursive version of window
</pre></code>
<p>
It's totally useless for at least 99% js developpers.
</p>
<p>
<code>JSON.prune.log</code> is a proxy over <code>console.log</code> deep cloning the objects before logging them, in order to avoid the delay problem encountered on non primitive objects logging (<a href=http://stackoverflow.com/questions/14296722/on-object-parameter-is-not-properly-set-in-javascript>see here for example</a>).
</p>
<code><pre>
JSON.prune.log('some object:', window);
</pre></code>
<p>
You should not use it frequently, only when you really need to see the objects how they were at logging time.
</p>
<h2>Include it</h2>
You may <a href=JSON.prune.js>download JSON.prune.js</a> and include it from your site.
<br>Or you may include the file directly from my server :
<code><pre>
<script src=http://dystroy.org/JSON.prune/.js></script>
</pre></code>
<h2>Links</h2>
<ul>
<li><a href=https://github.com/Canop/JSON.prune>JSON.prune on github</a></li>
<li><a href=http://stackoverflow.com/q/13861254/263525>Related Stack Overflow question</a></li>
<li><a href=https://github.com/douglascrockford/JSON-js/blob/master/json2.js>Crockford's code on which JSON.prune is based</a></li>
</ul>
<h2>Test it in the console</h2>
Open the console and type lines like the following ones :
<code><pre>
JSON.prune({a:1, b:[1, 2, 3]});
JSON.prune(window);
JSON.prune(window.location,{inheritedProperties: true}); // without this setting, FireFox and IE only show an empty object
var a=[]; for (var i=0;i<1000;i++) {a.push(i<2?1:a[i-2]+a[i-1])} JSON.prune(a, 0, 200);
</pre></code>
<h2>Test it in the page</h2>
<br><input checked type=radio name=opts id=std><label for=std>Options : <code>{}</code> This is the standard recommended iteration.</label>
<br><input type=radio name=opts id=inheritedProperties><label for=inheritedProperties>Options : <code>{inheritedProperties: true}</code> This also dumps enumerable inherited properties of objects.</label>
<br><input type=radio name=opts id=allProperties><label for=allProperties>Options : <code>{allProperties: true}</code> This also dumps non enumerable and inherited properties of objects. This might chocke your browser on some host objects. This is not recommended.</label>
<br><br>
<div id=buttons></div>
<code><pre id=result>
</pre></code>
<script>
var tests = [
"'Hello'",
"var a={num:NaN,arr:[]};a.in=a;a",
"window",
"console",
"window.location",
"Array.apply(0,Array(1e3)).map(Math.random)"
];
var $ = document.getElementById.bind(document);
for (var i=0; i<tests.length; i++) {
(function(str){
var butt = document.createElement('input');
butt.setAttribute('type','button');
butt.setAttribute('value',tests[i]);
butt.onclick = function(){
$('result').innerHTML = JSON.prune(eval(str), {
inheritedProperties: !!$('inheritedProperties').checked,
allProperties: !!$('allProperties').checked
});
};
$('buttons').appendChild(butt);
})(tests[i]);
}
</script>
</body>
</html>