Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. var f = require('util').format,
  3. require_optional = require('require_optional');
  4. // Set property function
  5. var setProperty = function(obj, prop, flag, values) {
  6. Object.defineProperty(obj, prop.name, {
  7. enumerable: true,
  8. set: function(value) {
  9. if (typeof value !== 'boolean') throw new Error(f('%s required a boolean', prop.name));
  10. // Flip the bit to 1
  11. if (value === true) values.flags |= flag;
  12. // Flip the bit to 0 if it's set, otherwise ignore
  13. if (value === false && (values.flags & flag) === flag) values.flags ^= flag;
  14. prop.value = value;
  15. },
  16. get: function() {
  17. return prop.value;
  18. }
  19. });
  20. };
  21. // Set property function
  22. var getProperty = function(obj, propName, fieldName, values, func) {
  23. Object.defineProperty(obj, propName, {
  24. enumerable: true,
  25. get: function() {
  26. // Not parsed yet, parse it
  27. if (values[fieldName] == null && obj.isParsed && !obj.isParsed()) {
  28. obj.parse();
  29. }
  30. // Do we have a post processing function
  31. if (typeof func === 'function') return func(values[fieldName]);
  32. // Return raw value
  33. return values[fieldName];
  34. }
  35. });
  36. };
  37. // Set simple property
  38. var getSingleProperty = function(obj, name, value) {
  39. Object.defineProperty(obj, name, {
  40. enumerable: true,
  41. get: function() {
  42. return value;
  43. }
  44. });
  45. };
  46. // Shallow copy
  47. var copy = function(fObj, tObj) {
  48. tObj = tObj || {};
  49. for (var name in fObj) tObj[name] = fObj[name];
  50. return tObj;
  51. };
  52. var debugOptions = function(debugFields, options) {
  53. var finaloptions = {};
  54. debugFields.forEach(function(n) {
  55. finaloptions[n] = options[n];
  56. });
  57. return finaloptions;
  58. };
  59. var retrieveBSON = function() {
  60. var BSON = require('bson');
  61. BSON.native = false;
  62. try {
  63. var optionalBSON = require_optional('bson-ext');
  64. if (optionalBSON) {
  65. optionalBSON.native = true;
  66. return optionalBSON;
  67. }
  68. } catch (err) {} // eslint-disable-line
  69. return BSON;
  70. };
  71. // Throw an error if an attempt to use Snappy is made when Snappy is not installed
  72. var noSnappyWarning = function() {
  73. throw new Error(
  74. 'Attempted to use Snappy compression, but Snappy is not installed. Install or disable Snappy compression and try again.'
  75. );
  76. };
  77. // Facilitate loading Snappy optionally
  78. var retrieveSnappy = function() {
  79. var snappy = null;
  80. try {
  81. snappy = require_optional('snappy');
  82. } catch (error) {} // eslint-disable-line
  83. if (!snappy) {
  84. snappy = {
  85. compress: noSnappyWarning,
  86. uncompress: noSnappyWarning,
  87. compressSync: noSnappyWarning,
  88. uncompressSync: noSnappyWarning
  89. };
  90. }
  91. return snappy;
  92. };
  93. exports.setProperty = setProperty;
  94. exports.getProperty = getProperty;
  95. exports.getSingleProperty = getSingleProperty;
  96. exports.copy = copy;
  97. exports.debugOptions = debugOptions;
  98. exports.retrieveBSON = retrieveBSON;
  99. exports.retrieveSnappy = retrieveSnappy;