BVB Source Codes

mongo-express Show json.js Source code

Return Download mongo-express: download json.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var mongodb = require('mongodb');
  4.  
  5. //Original code from official JSON.stringify function
  6. //Modified to accept BSON data types
  7.  
  8. /*exported f */
  9. // function f(n) {
  10. //   // Format integers to have at least two digits.
  11. //   return n < 10 ? '0' + n : n;
  12. // }
  13.  
  14. /*
  15. if (typeof Date.prototype.toJSON !== 'function') {
  16.  
  17.     Date.prototype.toJSON = function (key) {
  18.  
  19.         return isFinite(this.valueOf())
  20.             ? this.getUTCFullYear()     + '-' +
  21.                 f(this.getUTCMonth() + 1) + '-' +
  22.                 f(this.getUTCDate())      + 'T' +
  23.                 f(this.getUTCHours())     + ':' +
  24.                 f(this.getUTCMinutes())   + ':' +
  25.                 f(this.getUTCSeconds())   + 'Z'
  26.             : null;
  27.     };
  28.  
  29.     String.prototype.toJSON      =
  30.         Number.prototype.toJSON  =
  31.         Boolean.prototype.toJSON = function (key) {
  32.             return this.valueOf();
  33.         };
  34. }
  35. */
  36.  
  37. /*exported cx */
  38. // var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  39. // eslint-disable-next-line
  40. var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  41. var gap;
  42. var indent;
  43. var meta = {    // table of character substitutions
  44.   '\b': '\\b',
  45.   '\t': '\\t',
  46.   '\n': '\\n',
  47.   '\f': '\\f',
  48.   '\r': '\\r',
  49.   '"': '\\"',
  50.   '\\': '\\\\',
  51. };
  52. var rep;
  53.  
  54. function quote(string) {
  55.  
  56.   // If the string contains no control characters, no quote characters, and no
  57.   // backslash characters, then we can safely slap some quotes around it.
  58.   // Otherwise we must also replace the offending characters with safe escape
  59.   // sequences.
  60.  
  61.   escapable.lastIndex = 0;
  62.   return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
  63.     var c = meta[a];
  64.     return typeof c === 'string' ?
  65.         c :
  66.         '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  67.   }) + '"' : '"' + string + '"';
  68. }
  69.  
  70. function str(key, holder) {
  71.  
  72.   // Produce a string from holder[key].
  73.  
  74.   var i;          // The loop counter.
  75.   var k;          // The member key.
  76.   var v;          // The member value.
  77.   var length;
  78.   var mind = gap;
  79.   var partial;
  80.   var value = holder[key];
  81.  
  82.   // If the value has a toJSON method, call it to obtain a replacement value.
  83.  
  84.   //Custom code to handle BSON data types
  85.   if (value instanceof mongodb.ObjectID) {
  86.     return 'ObjectID("' + value + '")';
  87.   } else if (value instanceof mongodb.Timestamp) {
  88.     return 'Timestamp(' + value.high_ + ', ' + value.low_ + ')';
  89.   } else if (value instanceof Date) {
  90.     return 'ISODate("' + value.toJSON() + '")';
  91.   } else if (value instanceof mongodb.DBRef) {
  92.     if (value.db === '') {
  93.       return 'DBRef("' + value.namespace + '", "' + value.oid + '")';
  94.     } else {
  95.       return 'DBRef("' + value.namespace + '", "' + value.oid + '", "' + value.db + '")';
  96.     }
  97.   } else if (value instanceof mongodb.Code) {
  98.     return 'Code("' + value.code + '")';
  99.   } else if (value instanceof mongodb.MinKey) {
  100.     return 'MinKey()';
  101.   } else if (value instanceof mongodb.MaxKey) {
  102.     return 'MaxKey()';
  103.   } else if (value instanceof mongodb.Symbol) {
  104.     return 'Symbol("' + value + '")';
  105.   } else if (value instanceof mongodb.Binary) {
  106.     return 'Binary("' + value.toJSON(key) + '", ' + value.sub_type +   ')';
  107.   }
  108.  
  109.   if (value && typeof value === 'object' &&
  110.           typeof value.toJSON === 'function') {
  111.     value = value.toJSON(key);
  112.   }
  113.  
  114.   // If we were called with a replacer function, then call the replacer to
  115.   // obtain a replacement value.
  116.  
  117.   if (typeof rep === 'function') {
  118.     value = rep.call(holder, key, value);
  119.   }
  120.  
  121.   // What happens next depends on the value's type.
  122.   switch (typeof value) {
  123.     case 'string':
  124.       return quote(value);
  125.  
  126.     case 'number':
  127.  
  128.       // JSON numbers must be finite. Encode non-finite numbers as null.
  129.  
  130.       return isFinite(value) ? String(value) : 'null';
  131.  
  132.     case 'boolean':
  133.     case 'null':
  134.  
  135.       // If the value is a boolean or null, convert it to a string. Note:
  136.       // typeof null does not produce 'null'. The case is included here in
  137.       // the remote chance that this gets fixed someday.
  138.  
  139.       return String(value);
  140.  
  141.     // If the type is 'object', we might be dealing with an object or an array or
  142.     // null.
  143.  
  144.     case 'object':
  145.  
  146.       // Due to a specification blunder in ECMAScript, typeof null is 'object',
  147.       // so watch out for that case.
  148.  
  149.       if (!value) {
  150.         return 'null';
  151.       }
  152.  
  153.       // Make an array to hold the partial results of stringifying this object value.
  154.  
  155.       gap += indent;
  156.       partial = [];
  157.  
  158.       // Is the value an array?
  159.  
  160.       if (Object.prototype.toString.apply(value) === '[object Array]') {
  161.  
  162.         // The value is an array. Stringify every element. Use null as a placeholder
  163.         // for non-JSON values.
  164.  
  165.         length = value.length;
  166.         for (i = 0; i < length; i += 1) {
  167.           partial[i] = str(i, value) || 'null';
  168.         }
  169.  
  170.         // Join all of the elements together, separated with commas, and wrap them in
  171.         // brackets.
  172.  
  173.         v = partial.length === 0 ?
  174.             '[]' :
  175.             gap ?
  176.             '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
  177.             '[' + partial.join(',') + ']';
  178.         gap = mind;
  179.         return v;
  180.       }
  181.  
  182.       // If the replacer is an array, use it to select the members to be stringified.
  183.  
  184.       if (rep && typeof rep === 'object') {
  185.         length = rep.length;
  186.         for (i = 0; i < length; i += 1) {
  187.           if (typeof rep[i] === 'string') {
  188.             k = rep[i];
  189.             v = str(k, value);
  190.             if (v) {
  191.               partial.push(quote(k) + (gap ? ': ' : ':') + v);
  192.             }
  193.           }
  194.         }
  195.       } else {
  196.  
  197.         // Otherwise, iterate through all of the keys in the object.
  198.  
  199.         for (k in value) {
  200.           if (Object.prototype.hasOwnProperty.call(value, k)) {
  201.             v = str(k, value);
  202.             if (v) {
  203.               partial.push(quote(k) + (gap ? ': ' : ':') + v);
  204.             }
  205.           }
  206.         }
  207.       }
  208.  
  209.       // Join all of the member texts together, separated with commas,
  210.       // and wrap them in braces.
  211.  
  212.       v = partial.length === 0 ?
  213.           '{}' :
  214.           gap ?
  215.           '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
  216.           '{' + partial.join(',') + '}';
  217.       gap = mind;
  218.       return v;
  219.  
  220.     default:
  221.       console.log('Unexpected Object Type', typeof value, value);
  222.       break;
  223.   }
  224. }
  225.  
  226. // If the JSON object does not yet have a stringify method, give it one.
  227.  
  228. exports.stringify = function (value, replacer, space) {
  229.  
  230.   // The stringify method takes a value and an optional replacer, and an optional
  231.   // space parameter, and returns a JSON text. The replacer can be a function
  232.   // that can replace values, or an array of strings that will select the keys.
  233.   // A default replacer method can be provided. Use of the space parameter can
  234.   // produce text that is more easily readable.
  235.  
  236.   var i;
  237.   gap = '';
  238.   indent = '';
  239.  
  240.   // If the space parameter is a number, make an indent string containing that
  241.   // many spaces.
  242.  
  243.   if (typeof space === 'number') {
  244.     for (i = 0; i < space; i += 1) {
  245.       indent += ' ';
  246.     }
  247.  
  248.   // If the space parameter is a string, it will be used as the indent string.
  249.  
  250.   } else if (typeof space === 'string') {
  251.     indent = space;
  252.   }
  253.  
  254.   // If there is a replacer, it must be a function or an array.
  255.   // Otherwise, throw an error.
  256.  
  257.   rep = replacer;
  258.   if (replacer && typeof replacer !== 'function' &&
  259.           (typeof replacer !== 'object' ||
  260.           typeof replacer.length !== 'number')) {
  261.     throw new Error('JSON.stringify');
  262.   }
  263.  
  264.   // Make a fake root object containing our value under the key of ''.
  265.   // Return the result of stringifying the value.
  266.  
  267.   return str('', { '': value });
  268. };
  269.  
downloadjson.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top