objs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. module.exports = {
  11. // This function exposes a private one so that the private one can refer to
  12. // itself without having to worry about the `this` moving target.
  13. leanCopy: leanCopy,
  14. // This function will export the working function or an innocuous one
  15. // depending on
  16. // whether the platform supports object freezing or not.
  17. freeze: Object.freeze ? freeze : doNotFreeze,
  18. // This function will merge multiple objects into a single object.
  19. merge: merge
  20. };
  21. /**
  22. * Copies an object's properties into another, leaving out functions and
  23. * prototypes.
  24. *
  25. * The function can be used to return objects, or to modify an existing object,
  26. * which would then be passed as the second parameter.
  27. *
  28. * @function leanCopy
  29. * @private
  30. * @param {Object}
  31. * src The object to copy from
  32. * @param {Object}
  33. * dest The object to copy to
  34. * @returns {Object} An object with the same scalar properties as the source,
  35. * but stripped of everything else.
  36. */
  37. function leanCopy(src, dest) {
  38. if ('undefined' === typeof dest) {
  39. // Make sure there's a destination object.
  40. dest = Object.create(null);
  41. }
  42. for (var propertyName in src) {
  43. // Check whether the property is not inherited. Alternatively, check that
  44. // the `hasOwnProperty` method exists, otherwise the property is not
  45. // likely to be inherited.
  46. if (!src.hasOwnProperty || src.hasOwnProperty(propertyName)) {
  47. var property = src[propertyName];
  48. if (typeof property === 'object') {
  49. // Copy onto a new prototype-less object, lean as they come.
  50. dest[propertyName] = leanCopy(property, Object.create(null));
  51. } else if (typeof property !== 'function') {
  52. dest[propertyName] = property;
  53. }
  54. }
  55. }
  56. // Return a lean copy of the source object.
  57. return dest;
  58. }
  59. /**
  60. * Prohibits an object from being frozen.
  61. *
  62. * @function doNotFreeze
  63. * @private
  64. * @param {Object}
  65. * target The object not to freeze.
  66. * @returns {Object} The object, unfrozen.
  67. */
  68. function doNotFreeze(target) {
  69. return target;
  70. }
  71. /**
  72. * Protects an object and all its contents against modification.
  73. *
  74. * @function freeze
  75. * @private
  76. * @param {Object}
  77. * target The object to freeze.
  78. * @returns {Object} The object, after being frozen.
  79. */
  80. function freeze(target) {
  81. // Get all property names
  82. var propNames = Object.getOwnPropertyNames(target);
  83. // Freeze the object's properties.
  84. propNames.forEach(function(name) {
  85. var property = target[name];
  86. // Freeze the properties' properties.
  87. if (typeof property === 'object' && property !== null) {
  88. freeze(property);
  89. }
  90. });
  91. // Freeze the object itself.
  92. return Object.freeze(target);
  93. }
  94. /**
  95. * Merges all of the objects provided as input into a single object.
  96. *
  97. * <p>
  98. * <b>NOTE</b> This function is a system independent version of the function
  99. * <code>Object.assign</code>.
  100. *
  101. * @function merge
  102. * @private
  103. * @returns {Object} The merged objects.
  104. */
  105. function merge() {
  106. var resObj = {};
  107. for (var i = 0; i < arguments.length; i += 1) {
  108. var obj = arguments[i], keys = Object.keys(obj);
  109. for (var j = 0; j < keys.length; j += 1) {
  110. resObj[keys[j]] = obj[keys[j]];
  111. }
  112. }
  113. return resObj;
  114. }