Initial commit
[yaffs-website] / node_modules / json-stable-stringify / index.js
1 var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
2
3 module.exports = function (obj, opts) {
4     if (!opts) opts = {};
5     if (typeof opts === 'function') opts = { cmp: opts };
6     var space = opts.space || '';
7     if (typeof space === 'number') space = Array(space+1).join(' ');
8     var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
9     var replacer = opts.replacer || function(key, value) { return value; };
10
11     var cmp = opts.cmp && (function (f) {
12         return function (node) {
13             return function (a, b) {
14                 var aobj = { key: a, value: node[a] };
15                 var bobj = { key: b, value: node[b] };
16                 return f(aobj, bobj);
17             };
18         };
19     })(opts.cmp);
20
21     var seen = [];
22     return (function stringify (parent, key, node, level) {
23         var indent = space ? ('\n' + new Array(level + 1).join(space)) : '';
24         var colonSeparator = space ? ': ' : ':';
25
26         if (node && node.toJSON && typeof node.toJSON === 'function') {
27             node = node.toJSON();
28         }
29
30         node = replacer.call(parent, key, node);
31
32         if (node === undefined) {
33             return;
34         }
35         if (typeof node !== 'object' || node === null) {
36             return json.stringify(node);
37         }
38         if (isArray(node)) {
39             var out = [];
40             for (var i = 0; i < node.length; i++) {
41                 var item = stringify(node, i, node[i], level+1) || json.stringify(null);
42                 out.push(indent + space + item);
43             }
44             return '[' + out.join(',') + indent + ']';
45         }
46         else {
47             if (seen.indexOf(node) !== -1) {
48                 if (cycles) return json.stringify('__cycle__');
49                 throw new TypeError('Converting circular structure to JSON');
50             }
51             else seen.push(node);
52
53             var keys = objectKeys(node).sort(cmp && cmp(node));
54             var out = [];
55             for (var i = 0; i < keys.length; i++) {
56                 var key = keys[i];
57                 var value = stringify(node, key, node[key], level+1);
58
59                 if(!value) continue;
60
61                 var keyValue = json.stringify(key)
62                     + colonSeparator
63                     + value;
64                 ;
65                 out.push(indent + space + keyValue);
66             }
67             seen.splice(seen.indexOf(node), 1);
68             return '{' + out.join(',') + indent + '}';
69         }
70     })({ '': obj }, '', obj, 0);
71 };
72
73 var isArray = Array.isArray || function (x) {
74     return {}.toString.call(x) === '[object Array]';
75 };
76
77 var objectKeys = Object.keys || function (obj) {
78     var has = Object.prototype.hasOwnProperty || function () { return true };
79     var keys = [];
80     for (var key in obj) {
81         if (has.call(obj, key)) keys.push(key);
82     }
83     return keys;
84 };