source: node_modules/json-stable-stringify/index.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1'use strict';
2
3var jsonStringify = (typeof JSON !== 'undefined' ? JSON : require('jsonify')).stringify;
4
5var isArray = require('isarray');
6var objectKeys = require('object-keys');
7var callBind = require('call-bind');
8var callBound = require('call-bind/callBound');
9
10var $join = callBound('Array.prototype.join');
11var $push = callBound('Array.prototype.push');
12
13var strRepeat = function repeat(n, char) {
14 var str = '';
15 for (var i = 0; i < n; i += 1) {
16 str += char;
17 }
18 return str;
19};
20
21var defaultReplacer = function (parent, key, value) { return value; };
22
23module.exports = function stableStringify(obj) {
24 var opts = arguments.length > 1 ? arguments[1] : void undefined;
25 var space = (opts && opts.space) || '';
26 if (typeof space === 'number') { space = strRepeat(space, ' '); }
27 var cycles = !!opts && typeof opts.cycles === 'boolean' && opts.cycles;
28 var replacer = opts && opts.replacer ? callBind(opts.replacer) : defaultReplacer;
29
30 var cmpOpt = typeof opts === 'function' ? opts : opts && opts.cmp;
31 var cmp = cmpOpt && function (node) {
32 var get = cmpOpt.length > 2 && function get(k) { return node[k]; };
33 return function (a, b) {
34 return cmpOpt(
35 { key: a, value: node[a] },
36 { key: b, value: node[b] },
37 get ? { __proto__: null, get: get } : void undefined
38 );
39 };
40 };
41
42 var seen = [];
43 return (function stringify(parent, key, node, level) {
44 var indent = space ? '\n' + strRepeat(level, space) : '';
45 var colonSeparator = space ? ': ' : ':';
46
47 if (node && node.toJSON && typeof node.toJSON === 'function') {
48 node = node.toJSON();
49 }
50
51 node = replacer(parent, key, node);
52
53 if (node === undefined) {
54 return;
55 }
56 if (typeof node !== 'object' || node === null) {
57 return jsonStringify(node);
58 }
59 if (isArray(node)) {
60 var out = [];
61 for (var i = 0; i < node.length; i++) {
62 var item = stringify(node, i, node[i], level + 1) || jsonStringify(null);
63 $push(out, indent + space + item);
64 }
65 return '[' + $join(out, ',') + indent + ']';
66 }
67
68 if (seen.indexOf(node) !== -1) {
69 if (cycles) { return jsonStringify('__cycle__'); }
70 throw new TypeError('Converting circular structure to JSON');
71 } else { $push(seen, node); }
72
73 var keys = objectKeys(node).sort(cmp && cmp(node));
74 var out = [];
75 for (var i = 0; i < keys.length; i++) {
76 var key = keys[i];
77 var value = stringify(node, key, node[key], level + 1);
78
79 if (!value) { continue; }
80
81 var keyValue = jsonStringify(key)
82 + colonSeparator
83 + value;
84
85 $push(out, indent + space + keyValue);
86 }
87 seen.splice(seen.indexOf(node), 1);
88 return '{' + $join(out, ',') + indent + '}';
89
90 }({ '': obj }, '', obj, 0));
91};
Note: See TracBrowser for help on using the repository browser.