BVB Source Codes

mongo-express Show utils.js Source code

Return Download mongo-express: download utils.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. const _ = require('lodash');
  4.  
  5. // Given some size in bytes, returns it in a converted, friendly size
  6. // credits: http://stackoverflow.com/users/1596799/aliceljm
  7. exports.bytesToSize = function bytesToSize(bytes) {
  8.   if (bytes === 0) return '0 Byte';
  9.   var k = 1000;
  10.   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  11.   var i = Math.floor(Math.log(bytes) / Math.log(k));
  12.   return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
  13. };
  14.  
  15. exports.colsToGrid = function (cols) {
  16.   // Generate list of GridFS buckets
  17.   // takes databases, filters by having suffix of '.files' and also a corresponding '.chunks' in the DB list, then returns just the prefix name.
  18.  
  19.   // cols comes in an object of all databases and all their collections
  20.   // return an object of all databases and all potential GridFS x.files & x.chunks
  21.  
  22.   var rets = _.clone(cols);
  23.  
  24.   _.each(rets, (val, key) => {
  25.     rets[key] = _.map(
  26.       _.filter(rets[key], col =>
  27.         col.toString().substr(-6) === '.files' && _.intersection(rets[key], [col.toString().slice(0, -6) + '.chunks'])
  28.       ),
  29.       col => col.toString().slice(0, -6)
  30.     ).sort();
  31.   });
  32.  
  33.   return rets;
  34. };
  35.  
  36. exports.deepmerge = function (target, src) {
  37.   var array = Array.isArray(src);
  38.   var dst = array ? [] : {};
  39.  
  40.   if (array) {
  41.     target = target || [];
  42.     dst = dst.concat(target);
  43.     src.forEach(function (e, i) {
  44.       if (typeof dst[i] === 'undefined') {
  45.         dst[i] = e;
  46.       } else if (typeof e === 'object') {
  47.         dst[i] = exports.deepmerge(target[i], e);
  48.       } else {
  49.         if (target.indexOf(e) === -1) {
  50.           dst.push(e);
  51.         }
  52.       }
  53.     });
  54.   } else {
  55.     if (target && typeof target === 'object') {
  56.       Object.keys(target).forEach(function (key) {
  57.         dst[key] = target[key];
  58.       });
  59.     }
  60.  
  61.     Object.keys(src).forEach(function (key) {
  62.       if (typeof src[key] !== 'object' || !src[key]) {
  63.         dst[key] = src[key];
  64.       } else {
  65.         if (!target[key]) {
  66.           dst[key] = src[key];
  67.         } else {
  68.           dst[key] = exports.deepmerge(target[key], src[key]);
  69.         }
  70.       }
  71.     });
  72.   }
  73.  
  74.   return dst;
  75. };
  76.  
  77. exports.roughSizeOfObject = function (object) {
  78.  
  79.   var objectList = [];
  80.   var recurse = function (value) {
  81.     var bytes = 0;
  82.  
  83.     if (typeof value === 'boolean') {
  84.       bytes = 4;
  85.     } else if (typeof value === 'string') {
  86.       bytes = value.length * 2;
  87.     } else if (typeof value === 'number') {
  88.       bytes = 8;
  89.     } else if (typeof value === 'object' && objectList.indexOf(value) === -1) {
  90.       objectList[objectList.length] = value;
  91.  
  92.       for (var i in value) {
  93.         bytes += 8; // an assumed existence overhead
  94.         bytes += recurse(value[i]);
  95.       }
  96.     }
  97.  
  98.     return bytes;
  99.   };
  100.  
  101.   return recurse(object);
  102. };
  103.  
  104.  
  105. exports.buildCollectionURL = function (base, dbName, collectionName) {
  106.   return base + 'db/' + dbName + '/' + encodeURIComponent(collectionName);
  107. };
  108.  
downloadutils.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