map.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. /* eslint-disable @typescript-eslint/no-explicit-any */
  3. // We have an ES6 Map available, return the native instance
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.Map = void 0;
  6. /** @public */
  7. var bsonMap;
  8. exports.Map = bsonMap;
  9. var check = function (potentialGlobal) {
  10. // eslint-disable-next-line eqeqeq
  11. return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
  12. };
  13. // https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
  14. function getGlobal() {
  15. // eslint-disable-next-line no-undef
  16. return (check(typeof globalThis === 'object' && globalThis) ||
  17. check(typeof window === 'object' && window) ||
  18. check(typeof self === 'object' && self) ||
  19. check(typeof global === 'object' && global) ||
  20. Function('return this')());
  21. }
  22. var bsonGlobal = getGlobal();
  23. if (Object.prototype.hasOwnProperty.call(bsonGlobal, 'Map')) {
  24. exports.Map = bsonMap = bsonGlobal.Map;
  25. }
  26. else {
  27. // We will return a polyfill
  28. exports.Map = bsonMap = /** @class */ (function () {
  29. function Map(array) {
  30. if (array === void 0) { array = []; }
  31. this._keys = [];
  32. this._values = {};
  33. for (var i = 0; i < array.length; i++) {
  34. if (array[i] == null)
  35. continue; // skip null and undefined
  36. var entry = array[i];
  37. var key = entry[0];
  38. var value = entry[1];
  39. // Add the key to the list of keys in order
  40. this._keys.push(key);
  41. // Add the key and value to the values dictionary with a point
  42. // to the location in the ordered keys list
  43. this._values[key] = { v: value, i: this._keys.length - 1 };
  44. }
  45. }
  46. Map.prototype.clear = function () {
  47. this._keys = [];
  48. this._values = {};
  49. };
  50. Map.prototype.delete = function (key) {
  51. var value = this._values[key];
  52. if (value == null)
  53. return false;
  54. // Delete entry
  55. delete this._values[key];
  56. // Remove the key from the ordered keys list
  57. this._keys.splice(value.i, 1);
  58. return true;
  59. };
  60. Map.prototype.entries = function () {
  61. var _this = this;
  62. var index = 0;
  63. return {
  64. next: function () {
  65. var key = _this._keys[index++];
  66. return {
  67. value: key !== undefined ? [key, _this._values[key].v] : undefined,
  68. done: key !== undefined ? false : true
  69. };
  70. }
  71. };
  72. };
  73. Map.prototype.forEach = function (callback, self) {
  74. self = self || this;
  75. for (var i = 0; i < this._keys.length; i++) {
  76. var key = this._keys[i];
  77. // Call the forEach callback
  78. callback.call(self, this._values[key].v, key, self);
  79. }
  80. };
  81. Map.prototype.get = function (key) {
  82. return this._values[key] ? this._values[key].v : undefined;
  83. };
  84. Map.prototype.has = function (key) {
  85. return this._values[key] != null;
  86. };
  87. Map.prototype.keys = function () {
  88. var _this = this;
  89. var index = 0;
  90. return {
  91. next: function () {
  92. var key = _this._keys[index++];
  93. return {
  94. value: key !== undefined ? key : undefined,
  95. done: key !== undefined ? false : true
  96. };
  97. }
  98. };
  99. };
  100. Map.prototype.set = function (key, value) {
  101. if (this._values[key]) {
  102. this._values[key].v = value;
  103. return this;
  104. }
  105. // Add the key to the list of keys in order
  106. this._keys.push(key);
  107. // Add the key and value to the values dictionary with a point
  108. // to the location in the ordered keys list
  109. this._values[key] = { v: value, i: this._keys.length - 1 };
  110. return this;
  111. };
  112. Map.prototype.values = function () {
  113. var _this = this;
  114. var index = 0;
  115. return {
  116. next: function () {
  117. var key = _this._keys[index++];
  118. return {
  119. value: key !== undefined ? _this._values[key].v : undefined,
  120. done: key !== undefined ? false : true
  121. };
  122. }
  123. };
  124. };
  125. Object.defineProperty(Map.prototype, "size", {
  126. get: function () {
  127. return this._keys.length;
  128. },
  129. enumerable: false,
  130. configurable: true
  131. });
  132. return Map;
  133. }());
  134. }
  135. //# sourceMappingURL=map.js.map