Source: polyfill/Map.js

  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Watson Analytics
  4. //
  5. // (C) Copyright IBM Corp. 2015
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or
  8. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. module.exports = ( function( ObjectPolyfill, EntryStore )
  10. {
  11. "use strict";
  12. // Putting this in a var allows uglify to minify the code much more
  13. var SET_SIZE = !( ( __ES__ >= 5 ) || ObjectPolyfill.propertyGetSetSupport );
  14. /**
  15. * @classdesc Iterator for the Map polyfill.
  16. * @class module:barejs/polyfill.Map~MapIterator
  17. * @extends module:barejs/polyfill.EntryStore.Iterator
  18. */
  19. function MapIterator( _kind, _store )
  20. {
  21. EntryStore.Iterator.call( this, _kind, _store );
  22. }
  23. MapIterator.prototype = Object.create( EntryStore.Iterator.prototype,
  24. /** @lends module:barejs/polyfill.Map~MapIterator */
  25. {
  26. constructor: { writable: true, value: MapIterator }
  27. } );
  28. /**
  29. * @classdesc Mimics the implementation of a native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map}.
  30. * Has an O(n) lookup time for non String or Number keys, so cannot compete with a native Map (Which is supposed to have an O(1) lookup time).
  31. * @class module:barejs/polyfill.Map
  32. */
  33. function Map()
  34. {
  35. ObjectPolyfill.defineProperty( this, "_store", { value: new EntryStore( arguments[0], true ) } );
  36. /*istanbul ignore if: NodeJS has property support*/
  37. if ( SET_SIZE )
  38. this.size = this._store.size;
  39. }
  40. ObjectPolyfill.defineProperties( Map.prototype,
  41. /** @lends module:barejs/polyfill.Map# */
  42. {
  43. _store: { value: null },
  44. /**
  45. * The size of the Map, which represents the number of entries currently in the Map.
  46. * @member {number}
  47. * @readonly
  48. */
  49. size: ( SET_SIZE ?
  50. /*istanbul ignore next*/ { writable: true, value: null } :
  51. { "get": function size(){ return this._store && this._store.size; } } ),
  52. /**
  53. * Set a value for the specified key in the Map.
  54. * @function
  55. * @param _key The object to use as key.
  56. * @param _value The value to add.
  57. * @returns {module:barejs/polyfill.Map} The Map (for chaining)
  58. */
  59. "set": { value: function set( _key, _value )
  60. {
  61. this._store.set( _key, _value );
  62. /*istanbul ignore if: NodeJS has property support*/
  63. if ( SET_SIZE )
  64. this.size = this._store.size;
  65. return this;
  66. } },
  67. /**
  68. * Get the value for the specified key
  69. * @function
  70. * @param {object} _key The object to use as key.
  71. * @returns The value, or undefined if the key is not known.
  72. */
  73. "get": { value: function get( _key )
  74. {
  75. var entry = this._store.entries[this._store.indexOf( _key )];
  76. return entry && entry[1];
  77. } },
  78. /**
  79. * Check if the Map has an entry for the specified key
  80. * @function
  81. * @param {object} _key The object to use as key.
  82. * @returns {boolean} True if there is an entry for the key, false otherwise
  83. */
  84. has: { value: function has( _key )
  85. {
  86. return this._store.indexOf( _key ) >= 0;
  87. } },
  88. /**
  89. * Remove the value for the specified key
  90. * @function
  91. * @param {object} _key The object to use as key.
  92. * @returns {boolean} True if the entry was deleted, false otherwise
  93. */
  94. "delete": { value: function _delete( _key )
  95. {
  96. var removed = this._store.remove( _key );
  97. /*istanbul ignore if: NodeJS has property support*/
  98. if ( SET_SIZE )
  99. this.size = this._store.size;
  100. return removed;
  101. } },
  102. clear: { value: function clear()
  103. {
  104. this._store.clear();
  105. /*istanbul ignore if: NodeJS has property support*/
  106. if ( SET_SIZE )
  107. this.size = this._store.size;
  108. } },
  109. /**
  110. * Iterate the Map
  111. * @function
  112. * @param {function} _callback The callback function.
  113. * @param [_thisArg] Optional: context to call the callback in.
  114. */
  115. forEach: { value: function forEach( _callback/*, _thisArg*/ )
  116. {
  117. this._store.forEach( this, _callback, arguments[1] );
  118. } },
  119. /**
  120. * Get a key iterator
  121. * @function
  122. * @returns {module:barejs/polyfill.Map~MapIterator} A key iterator
  123. */
  124. keys: { value: function keys()
  125. {
  126. return new MapIterator( "key", this._store );
  127. } },
  128. /**
  129. * Get a value iterator
  130. * @function
  131. * @returns {module:barejs/polyfill.Map~MapIterator} A value iterator
  132. */
  133. values: { value: function values()
  134. {
  135. return new MapIterator( "value", this._store );
  136. } },
  137. /**
  138. * Get an entry iterator
  139. * @function
  140. * @returns {module:barejs/polyfill.Map~MapIterator} A entry iterator
  141. */
  142. entries: { value: function entries()
  143. {
  144. return new MapIterator( "entry", this._store );
  145. } }
  146. } );
  147. ObjectPolyfill.setIterator( Map.prototype, Map.prototype.entries );
  148. return Map;
  149. }( require( "./Object" ), require( "./EntryStore" ) ) );