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