react / wstein / node_modules / browserify / node_modules / shasum / node_modules / json-stable-stringify / node_modules / jsonify / lib / stringify.js
80556 viewsvar cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,1escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,2gap,3indent,4meta = { // table of character substitutions5'\b': '\\b',6'\t': '\\t',7'\n': '\\n',8'\f': '\\f',9'\r': '\\r',10'"' : '\\"',11'\\': '\\\\'12},13rep;1415function quote(string) {16// If the string contains no control characters, no quote characters, and no17// backslash characters, then we can safely slap some quotes around it.18// Otherwise we must also replace the offending characters with safe escape19// sequences.2021escapable.lastIndex = 0;22return escapable.test(string) ? '"' + string.replace(escapable, function (a) {23var c = meta[a];24return typeof c === 'string' ? c :25'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);26}) + '"' : '"' + string + '"';27}2829function str(key, holder) {30// Produce a string from holder[key].31var i, // The loop counter.32k, // The member key.33v, // The member value.34length,35mind = gap,36partial,37value = holder[key];3839// If the value has a toJSON method, call it to obtain a replacement value.40if (value && typeof value === 'object' &&41typeof value.toJSON === 'function') {42value = value.toJSON(key);43}4445// If we were called with a replacer function, then call the replacer to46// obtain a replacement value.47if (typeof rep === 'function') {48value = rep.call(holder, key, value);49}5051// What happens next depends on the value's type.52switch (typeof value) {53case 'string':54return quote(value);5556case 'number':57// JSON numbers must be finite. Encode non-finite numbers as null.58return isFinite(value) ? String(value) : 'null';5960case 'boolean':61case 'null':62// If the value is a boolean or null, convert it to a string. Note:63// typeof null does not produce 'null'. The case is included here in64// the remote chance that this gets fixed someday.65return String(value);6667case 'object':68if (!value) return 'null';69gap += indent;70partial = [];7172// Array.isArray73if (Object.prototype.toString.apply(value) === '[object Array]') {74length = value.length;75for (i = 0; i < length; i += 1) {76partial[i] = str(i, value) || 'null';77}7879// Join all of the elements together, separated with commas, and80// wrap them in brackets.81v = partial.length === 0 ? '[]' : gap ?82'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :83'[' + partial.join(',') + ']';84gap = mind;85return v;86}8788// If the replacer is an array, use it to select the members to be89// stringified.90if (rep && typeof rep === 'object') {91length = rep.length;92for (i = 0; i < length; i += 1) {93k = rep[i];94if (typeof k === 'string') {95v = str(k, value);96if (v) {97partial.push(quote(k) + (gap ? ': ' : ':') + v);98}99}100}101}102else {103// Otherwise, iterate through all of the keys in the object.104for (k in value) {105if (Object.prototype.hasOwnProperty.call(value, k)) {106v = str(k, value);107if (v) {108partial.push(quote(k) + (gap ? ': ' : ':') + v);109}110}111}112}113114// Join all of the member texts together, separated with commas,115// and wrap them in braces.116117v = partial.length === 0 ? '{}' : gap ?118'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :119'{' + partial.join(',') + '}';120gap = mind;121return v;122}123}124125module.exports = function (value, replacer, space) {126var i;127gap = '';128indent = '';129130// If the space parameter is a number, make an indent string containing that131// many spaces.132if (typeof space === 'number') {133for (i = 0; i < space; i += 1) {134indent += ' ';135}136}137// If the space parameter is a string, it will be used as the indent string.138else if (typeof space === 'string') {139indent = space;140}141142// If there is a replacer, it must be a function or an array.143// Otherwise, throw an error.144rep = replacer;145if (replacer && typeof replacer !== 'function'146&& (typeof replacer !== 'object' || typeof replacer.length !== 'number')) {147throw new Error('JSON.stringify');148}149150// Make a fake root object containing our value under the key of ''.151// Return the result of stringifying the value.152return str('', {'': value});153};154155156