map.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. var global_1 = require("./utils/global");
  7. /** @public */
  8. var bsonMap;
  9. exports.Map = bsonMap;
  10. var bsonGlobal = global_1.getGlobal();
  11. if (bsonGlobal.Map) {
  12. exports.Map = bsonMap = bsonGlobal.Map;
  13. }
  14. else {
  15. // We will return a polyfill
  16. exports.Map = bsonMap = /** @class */ (function () {
  17. function Map(array) {
  18. if (array === void 0) { array = []; }
  19. this._keys = [];
  20. this._values = {};
  21. for (var i = 0; i < array.length; i++) {
  22. if (array[i] == null)
  23. continue; // skip null and undefined
  24. var entry = array[i];
  25. var key = entry[0];
  26. var value = entry[1];
  27. // Add the key to the list of keys in order
  28. this._keys.push(key);
  29. // Add the key and value to the values dictionary with a point
  30. // to the location in the ordered keys list
  31. this._values[key] = { v: value, i: this._keys.length - 1 };
  32. }
  33. }
  34. Map.prototype.clear = function () {
  35. this._keys = [];
  36. this._values = {};
  37. };
  38. Map.prototype.delete = function (key) {
  39. var value = this._values[key];
  40. if (value == null)
  41. return false;
  42. // Delete entry
  43. delete this._values[key];
  44. // Remove the key from the ordered keys list
  45. this._keys.splice(value.i, 1);
  46. return true;
  47. };
  48. Map.prototype.entries = function () {
  49. var _this = this;
  50. var index = 0;
  51. return {
  52. next: function () {
  53. var key = _this._keys[index++];
  54. return {
  55. value: key !== undefined ? [key, _this._values[key].v] : undefined,
  56. done: key !== undefined ? false : true
  57. };
  58. }
  59. };
  60. };
  61. Map.prototype.forEach = function (callback, self) {
  62. self = self || this;
  63. for (var i = 0; i < this._keys.length; i++) {
  64. var key = this._keys[i];
  65. // Call the forEach callback
  66. callback.call(self, this._values[key].v, key, self);
  67. }
  68. };
  69. Map.prototype.get = function (key) {
  70. return this._values[key] ? this._values[key].v : undefined;
  71. };
  72. Map.prototype.has = function (key) {
  73. return this._values[key] != null;
  74. };
  75. Map.prototype.keys = function () {
  76. var _this = this;
  77. var index = 0;
  78. return {
  79. next: function () {
  80. var key = _this._keys[index++];
  81. return {
  82. value: key !== undefined ? key : undefined,
  83. done: key !== undefined ? false : true
  84. };
  85. }
  86. };
  87. };
  88. Map.prototype.set = function (key, value) {
  89. if (this._values[key]) {
  90. this._values[key].v = value;
  91. return this;
  92. }
  93. // Add the key to the list of keys in order
  94. this._keys.push(key);
  95. // Add the key and value to the values dictionary with a point
  96. // to the location in the ordered keys list
  97. this._values[key] = { v: value, i: this._keys.length - 1 };
  98. return this;
  99. };
  100. Map.prototype.values = function () {
  101. var _this = this;
  102. var index = 0;
  103. return {
  104. next: function () {
  105. var key = _this._keys[index++];
  106. return {
  107. value: key !== undefined ? _this._values[key].v : undefined,
  108. done: key !== undefined ? false : true
  109. };
  110. }
  111. };
  112. };
  113. Object.defineProperty(Map.prototype, "size", {
  114. get: function () {
  115. return this._keys.length;
  116. },
  117. enumerable: false,
  118. configurable: true
  119. });
  120. return Map;
  121. }());
  122. }
  123. //# sourceMappingURL=map.js.map