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.

map.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. // We have an ES6 Map available, return the native instance
  3. if (typeof global.Map !== 'undefined') {
  4. module.exports = global.Map;
  5. module.exports.Map = global.Map;
  6. } else {
  7. // We will return a polyfill
  8. var Map = function(array) {
  9. this._keys = [];
  10. this._values = {};
  11. for (var i = 0; i < array.length; i++) {
  12. if (array[i] == null) continue; // skip null and undefined
  13. var entry = array[i];
  14. var key = entry[0];
  15. var value = entry[1];
  16. // Add the key to the list of keys in order
  17. this._keys.push(key);
  18. // Add the key and value to the values dictionary with a point
  19. // to the location in the ordered keys list
  20. this._values[key] = { v: value, i: this._keys.length - 1 };
  21. }
  22. };
  23. Map.prototype.clear = function() {
  24. this._keys = [];
  25. this._values = {};
  26. };
  27. Map.prototype.delete = function(key) {
  28. var value = this._values[key];
  29. if (value == null) return false;
  30. // Delete entry
  31. delete this._values[key];
  32. // Remove the key from the ordered keys list
  33. this._keys.splice(value.i, 1);
  34. return true;
  35. };
  36. Map.prototype.entries = function() {
  37. var self = this;
  38. var index = 0;
  39. return {
  40. next: function() {
  41. var key = self._keys[index++];
  42. return {
  43. value: key !== undefined ? [key, self._values[key].v] : undefined,
  44. done: key !== undefined ? false : true
  45. };
  46. }
  47. };
  48. };
  49. Map.prototype.forEach = function(callback, self) {
  50. self = self || this;
  51. for (var i = 0; i < this._keys.length; i++) {
  52. var key = this._keys[i];
  53. // Call the forEach callback
  54. callback.call(self, this._values[key].v, key, self);
  55. }
  56. };
  57. Map.prototype.get = function(key) {
  58. return this._values[key] ? this._values[key].v : undefined;
  59. };
  60. Map.prototype.has = function(key) {
  61. return this._values[key] != null;
  62. };
  63. Map.prototype.keys = function() {
  64. var self = this;
  65. var index = 0;
  66. return {
  67. next: function() {
  68. var key = self._keys[index++];
  69. return {
  70. value: key !== undefined ? key : undefined,
  71. done: key !== undefined ? false : true
  72. };
  73. }
  74. };
  75. };
  76. Map.prototype.set = function(key, value) {
  77. if (this._values[key]) {
  78. this._values[key].v = value;
  79. return this;
  80. }
  81. // Add the key to the list of keys in order
  82. this._keys.push(key);
  83. // Add the key and value to the values dictionary with a point
  84. // to the location in the ordered keys list
  85. this._values[key] = { v: value, i: this._keys.length - 1 };
  86. return this;
  87. };
  88. Map.prototype.values = function() {
  89. var self = this;
  90. var index = 0;
  91. return {
  92. next: function() {
  93. var key = self._keys[index++];
  94. return {
  95. value: key !== undefined ? self._values[key].v : undefined,
  96. done: key !== undefined ? false : true
  97. };
  98. }
  99. };
  100. };
  101. // Last ismaster
  102. Object.defineProperty(Map.prototype, 'size', {
  103. enumerable: true,
  104. get: function() {
  105. return this._keys.length;
  106. }
  107. });
  108. module.exports = Map;
  109. module.exports.Map = Map;
  110. }